AdonisJS v5- A Simple API CRUD Application
Sep 30, 2021Being a Laravel Developer for over 6+ years, my quest for an equivalent framework in the Node.js community ended with Adonisjs-a fully featured Web Framework for Node.js developers.
In this article, I'll help you get started with Adonisjs by performing an API CRUD operation. We're going to develop a simple Post module.
First, we'll install the adonisjs version 5.
npm init adonis-ts-app crud
After successful installation, run the following command to start the application
node ace serve --watch
When you hit http://localhost:3333, you'll see the Adonisjs homepage.
We also have to install the @adonisjs/lucid package for database support.
npm i @adonisjs/lucid
After installing the package, we should configure the database we're willing to use. I've chosen MySQL
node ace configure @adonisjs/lucid
Based on the database selection, adonisjs will automatically copy the envirnomental variables to .env file like the following.
Let's create a migration and model file. In adonisjs, you can perform both by a simple command.
node ace make:model Post -m
Open the migration file under the database folder and add the necessary columns.
-
database
-
migrations
- 1632933589444_posts.ts
-
migrations
Once the migration file is ready, let's check the migration status before we migrate.
node ace migration:status
As you can see, our migration file is pending, so we can go ahead and run the migration
node ace migration:run
Perfect! We now have the table migrated and set up. Let's focus on the model file.
Here, as you can see, we've to explicitly mention all the columns as properties on the model file or else typescript compiler would throw Property not exists on Post model.
Now let's configure the necessary routes on routes.ts file.
Above code will create all the post module routes for us. Kindly note, I've added apiOnly() at the end of the line, which means both CREATE and SHOW routes will be ignored.
Now our routes, model, and migration files are ready, let's create a controller file.
node ace make:controller Post
We'll add relevant methods to PostsController now
Listing
When we hit the route /posts on GET method, we should receive all the posts. So, let us add the index method on the PostsController and pull all the records from the Post model. Kindly note for the tutorial, I've retrieved all the records, however, in a real-world scenario, it is wise to paginate your results.
Create
When we try to create a post by hitting /posts on POST method, first we should validate whether we are receiving all the params from client. If not, then we have to validate the params and throw errors accordingly. Adonisjs provides an easy inbuilt Validator which comes handy for scenarios like this.
Here, we are trying to validate both the title and content parameters. If any one of the fields fails validation, then we'll throw a validation error like this.
When we provide proper data, we'll create the post record
Show
For retriving a post resource, we've to hit the show API with post identifier passed on the URL i.e, /posts/1 on GET method.
On the show method we've to validate whether the given identifier in the URL exists on the database or not. If not, we've to throw 404 error.
But if we do have the record on the database, then we'll return the resource to the client.
Update
For updating a post resource, we've to perform two checks.
- 1. Validate the input params
- 2. Make sure the post resource exists on the database
So, borrowing some of the code from store() and show() method, our update method will typically look like the following. Again, we can optimize the code by eliminating the code duplication of validation and post resource check but keeping them simple for now.
Destroy
Similar to show() method, we've to first check the resource availability before we can delete the resource.
Our final PostsController will look like this,
That's it. Now we've a fully functional Post module built on Adonisjs.
Happy Coding.