@fcoded/adonis-hashids
v1.0.2
Published
A package to generate YouTube-like IDs for AdonisJS Lucid models
Downloads
7
Maintainers
Readme
adonis-hashids
Hashids for AdonisJS 5
This package generates YouTube-like IDs for AdonisJS Lucid models using the
hashids.js
package. hashids.js is small JavaScript library to generate YouTube-like ids from numbers. Use it when you don't want to expose your database ids to the user.
Prerequisites
Adonis Lucid to be installed and configured
The db table must have a column named
hashid
of typestring
and it must benullable
.
Installation
npm i @fcoded/adonis-hashids or yarn add @fcoded/adonis-hashids
Configuration
node ace configure @fcoded/adonis-hashids
The configuration file is in config/hashids.ts
. The default salt
will be the project name, the default minLength
is 12 and it uses the default alphabet
from the hashids.js package.
Using the Hashids Mixin
Apply the mixin on the model you.
import { compose } from '@ioc:Adonis/Core/Helpers'
import { column, BaseModel } from '@ioc:Adonis/Lucid/Orm'
import { LucidHashIds } from '@ioc:Adonis/Adons/LucidHashIds'
export default class User extends compose(BaseModel, LucidHashIds){
}
The mixin will update the hashid
column after the recorded is created using the afterCreate
Model hook
Also, if you are using Route model binding
package, the routeLookupKey
will be set to the hashid
by default.