All right, so that was like all about Adonis.js. Now we can start diving into some code. So we are going to play a scenario here in which we are trying to build a simplistic version of Product Hunt to showcase all the amazing open-source projects on the internet, and we are going to call it OS Hunt. Now, of course, we are not going to build the entire application, but we will cover all the touch points to see what it takes to build an app like this in Adonis.js.
So first things first, we have to create a new project, and we can do that by running this command, npm init adonis and the name of the project. And after running this command, we will get a new Adonis.js application, which comes with a SQL ORM, support for authentication, file uploads, password hashing, basically all the things we need to kickstart our development. Also, this is how the folder structure of a brand-new Adonis.js application looks like, where everything has its own place where it lives, and you don't have to figure it out by your own.
Okay, so the very first thing we want to do right after creating our application is to see how quickly we can knock out the CRUD operations of our application. And in our case, it's going to be basically creating, reading, updating, and deleting projects. All right, so everything starts with routing, where routes are essentially the HTTP entry points of your application. And this is how you can define them. So in our case, we roughly need these seven endpoints to manage our projects. Very first, we will have an endpoint to list all the projects. And then we got our two endpoints to display the form to create a new project and then handle the submission to basically save that project in the database. And then we want to show a project by its slug, then another couple of endpoints to manage updating a project, and finally an endpoint to delete the project. Once we have defined our routes, we can create a controller for it so that we don't have to write all the logic in this one massive routes file.
And we can create a controller by using this command called node ace make controller, and then the name of the controller we want to create. And once we have created our controller, we can go back to our routes file and we can replace all these inline callbacks with the controller references. So here we are saying, we want to use the project's controller for all these endpoints, but we want to use different methods to handle different routes. And this is how essentially a controller looks like where it's a vanilla JavaScript class with all the methods we want to create to handle different HTTP requests. Now, before we start putting any logic inside our controllers, we need a way to interact with our database so that we can fetch projects from there and we can also save our projects inside the database. And this is where we will move to our next layer of MVC called models.
So models are basically a way for you to interact with the database and migrations is a way to create and alter database tables or like create indexes and drop indexes and all that stuff. So you can create a new model along with a migration using this command, which is node ace make model, the name of the model and dash M flag to basically say you also want a migration along with the model. Once you have the migration file, you can essentially write the instructions you want to create a new table. In our case, we need a table with a primary key. That's the ID, the name of the project, its unique slug, description of the project and the URL that someone can click and go to the project website. And this is how a model looks like, where we will be like using the migration to just create the table. But in our application to interact with that table, we will be using a model and the models are represented as JavaScript classes again.
Comments