node package manager

adonis-hashids

adonis-hashids

npm version npm Build Status dependencies Status devDependencies Status JavaScript Style Guide

Hashids Provider for AdonisJs framework.

Installation

In order to use adonis-hashids

npm install adonis-hashids --save

Setup

Once you have installed the provider from the npm, make sure to follow the below steps to setup the provider.

bootstrap/app.js
const providers = [
  ...,
  'adonis-hashids/providers/HashidsProvider'
]

Also, for registering commands.

bootstrap/app.js
const aceProviders = [
  ...,
  'adonis-hashids/providers/CommandsProvider'
]
 
const commands = [
  ...,
  'Adonis/Commands/Hashids:Config'
]

Also, it is a good practice to setup an alias to avoid typing the complete namespace.

bootstrap/app.js
const aliases = {
  ...,
  Hashids: 'Adonis/Addons/Hashids'
}

Then, for generating a config file.

./ace hashids:config

Configuration

Adonis Hashids generate YouTube-like ids from numbers. Use Hashids when you do not want to expose your database ids to the user. The hashids configuration is located at config/hashids.js. In this file you may specify which hashids connection you would like used by default throughout your application.

The hashids configuration file also contains various other options, which are documented within the file so make sure to read over these options. By default Adonis Hashids is configured to use the default connection.

Usage

Using default connection

// Initialize 
const Hashids = use('Hashids')
 
Hashids.encode(1)
// OY 
 
Hashids.decode('OY')
// [ 1 ] 

Combination of ids

// Initialize 
const Hashids = use('Hashids')
 
Hashids.encode(1, 2, 3)
// or Array 
Hashids.encode([1, 2, 3])
// will ouput wzs9cr 
 
Hashids.decode('wzs9cr')
// [ 1, 2, 3 ] 

Encode hex

// Initialize 
const Hashids = use('Hashids')
 
Hashids.encodeHex('507f1f77bcf86cd799439011')
// Nrao6rxKbziryRrXR1zD 
 
Hashids.decodeHex('Nrao6rxKbziryRrXR1zD')
// 507f1f77bcf86cd799439011 

Using other connection

// Initialize 
const Hashids = use('Hashids')
 
Hashids.connection('alternative').encode(1)
// OY 
 
Hashids.connection('alternative').decode('OY')
// [ 1 ] 

Changelog

CHANGELOG

Credits

Thanks to the community of AdonisJs.

Copyright and License

Copyright (c) 2016 Carlson Orozco, MIT License