node.jsuuidadonis.jshashids

AdonisJs: Using Hashids


Had already someone used Hashids in AdonisJs?

Being more specific, in a Model, to return a property hashid in an object

I'm working on a migration from Laravel to Adonis. In Laravel, it's possible just with a couple of code lines in each Model, like this:

use Hashids;

class Menu extends Model
{
    use \OwenIt\Auditing\Auditable;

    protected $appends = ['hashid'];

    public function getHashidAttribute()
    {
        return Hashids::encode($this->attributes['id']);
    }
}

I installed this NPM package: https://www.npmjs.com/package/adonis-hashids, and I tried to figure out how to use like the Laravel way


Solution

  • I'd used Computed Properties (https://adonisjs.com/docs/4.1/database-getters-setters#_computed_properties)

    class Menu extends Model {
      static get computed () {
        return ['hashids']
      }
      getHashids({ id }) {
        return Hashids.encode(id)
      }
    }