Skip to content

Instantly share code, notes, and snippets.

@benfurfie
Last active December 15, 2022 09:27
Show Gist options
  • Save benfurfie/ef360ee7057a4ba3b9c72b0f6a63754c to your computer and use it in GitHub Desktop.
Save benfurfie/ef360ee7057a4ba3b9c72b0f6a63754c to your computer and use it in GitHub Desktop.
Adonis JS Cheatsheet

Adonis Framework Cheatsheet

Commands

Create a model, controller and migration from the command line

adonis make:model Model -mc

Lucid

Don't return a user's password in query

Sometimes you want to be able to query a model, but not return data in that model. A good example of this is when querying a user model. You really don't want to be returning the password, hashed or not.

To do this, open the relevant model file (e.g. app/Models/User.js) and add the following:

static get hidden () {
  return ['password']
}

Whatever you pass into the return object (as individual strings) will not be returned in any query.

Validation

Adonis does not include validation out of the box. You need to install it using the command:

adonis install @adonisjs/validator

You also need to add the package to the Service Provider:

const providers = [
  '@adonisjs/validator/providers/ValidatorProvider'
]

Adonis uses a library called Indicative to provided validation rules. All available rules are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment