Custom Exceptions in AdonisJS

AdonisJS provides streamlined ways to raise exceptions in your controller or service code, and raise them in a controlled way to the caller. To create a custom exception - adonis make:exception RecordNotFoundException This will create - √ create app\Exceptions\RecordNotFoundException.js Edit the exception to introduce your own errors. "use strict"; const { LogicalException } = require("@adonisjs/generic-exceptions"); class RecordNotFoundException extends LogicalException { /** * Handle this exception by itself */ handle(error, { response }) { return response.status(404).json({ error: "Record is not found. Check other universes.", }); } } module.exports = RecordNotFoundException; We are trying to raise a 404 with a specific exception message in the above code block. ...

Track Created By / Modified By for Records in AdonisJS

AdonisJS provided a quick way to track created and updated times, but does not provide shortcuts to track the user who created or last updated the record. Doing so is quite easy. Modify the migration script to include created_by_id and updated_by_id. // todoschema.js class TodoSchema extends Schema { up() { this.create("todos", (table) => { table.increments(); // id table.timestamps(); // created_at and updated_at columns table .integer("created_by_id") .unsigned() .references("id") .inTable("users"); table .integer("updated_by_id") .unsigned() .references("id") .inTable("users"); }); } down() { this.drop("todos"); } } module.exports = TodoSchema; Both the user id columns are references to the users table, which is included out of the box in Adonis. ...

The Right Way to Receive Parameters in AdonisJS Controller

Quickly and easily pass parameters to controller methods from your request. See Start creating your backend application in AdonisJS to get started You can quickly scaffold a controller in Adonis using the following command - adonis make:controller Todo --type http If you want to receive parameters from input, you would do the following - class AccountController { async create({ request, auth, params, addParams }) { const data = request.all(); /* .. .. */ } By destructuring assignment to variables within the function argument itself, you are doing two things - ...

Create a functional backend in AdonisJS under 5 minutes

AdonisJS provides a really quick way to scaffold your way to a great back-end application. Let us start creating a simple to-do app called ‘ado’ in this post. We will see how we can create this with simple commands and have your API ready in no time. Install AdonisJS Download and install Node if you don’t have it in your system. Install AdonisJS CLI npm i -g @adonisjs/cli Download and install Insomnia REST client. This will be used to test the back-end APIs that we will be creating in AdonisJS ...

Why should you use AdonisJS framework?

AdonisJS is a great server-side framework built on Express. It’s opinionated way of doing things just makes sense, clears up the initial development problems, and makes development & maintenance easier. I have completed ~3 projects on AdonisJS so far after switching roles to a web application developer last year. After using FeathersJS, Laravel and a bit of .NET, I find myself increasingly becoming comfortable with what AdonisJS can offer. Here’s why you should use AdonisJS (my opinions - I am often wrong) - ...