CRUD Operations
Share:
AdonisJS is a highly robust and efficient Node.js framework that excels in creating web servers. One of its principal uses is developing server-side web applications with a potentially clean and maintainable codebase with a scalable structure. This tutorial chapter will focus on comprehending CRUD (Create, Read, Update, Delete) operations with AdonisJS, which are the four basic functions of persistent storage. To bring the concepts closer to home, we will work on a fictitious project of a movie database, wherein CRUD operations come into play.
Let's start by creating a new AdonisJS application. Ensure the AdonisJS CLI is installed; if not, install it globally using NPM:
npm install -g @adonisjs/cli
Now, with the CLI installed, create a new AdonisJS application:
adonis new movie-database
This command will generate a basic AdonisJS application in a movie-database
directory. Enter the directory and start the server:
cd movie-database
adonis serve --dev
Now, let’s dive into movie characters CRUD operations in AdonisJS.
Before we proceed, we need to set up our database. In this example, we'll use SQLite for simplicity. Edit the .env
file in the root directory:
DB_CONNECTION=sqlite
Now, let's create the migration for the characters
table:
adonis make:migration characters
A new file will be created under the database/migrations
directory. Open the aforementioned file and define the fields required:
'use strict'
const Schema = use('Schema')
class CharactersTableSchema extends Schema {
up () {
this.create('characters', (table) => {
table.increments()
table.string('name', 80).notNullable()
table.string('movie_title', 80).notNullable()
table.timestamps()
})
}
down () {
this.drop('characters')
}
}
module.exports = CharactersTableSchema
To create the characters
table in the database, run the migrations:
adonis migration:run
Next, create a model for the characters
table:
adonis make:model Character
This command generates a Character.js
file in the app/Models
directory. With the model in place, the next step is to create a controller to manage our data logic:
adonis make:controller Character
The controller will be created inside app/Controllers/Http/
directory. Now, let's define the CRUD operations inside the CharacterController
:
'Use strict'
const Character = use('App/Models/Character')
class CharacterController {
async index({ response }) {
let characters = await Character.all()
return response.json(characters)
}
async store({ request, response }) {
const characterInfo = request.only(['name', 'movie_title'])
const character = new Character()
character.name = characterInfo.name
character.movie_title = characterInfo.movie_title
await character.save()
return response.status(201).json(character)
}
async update({ params, request, response }) {
const characterInfo = request.only(['name', 'movie_title'])
const character = await Character.find(params.id)
if (!character) {
return response.status(404).json({ data: 'Character not found' })
}
character.name = characterInfo.name
character.movie_title = characterInfo.movie_title
await character.save()
return response.status(200).json(character)
}
async delete({ params, response }) {
const character = await Character.find(params.id)
if (!character) {
return response.status(404).json({ data: 'Character not found' })
}
await character.delete()
return response.status(204).json(null)
}
}
module.exports = CharacterController
With the Controller ready, we need to define our routes. Open the start/routes.js
file and define your API routes:
Route.group(() => {
Route.resource('characters', 'CharacterController')
}).prefix('api')
Great! With the server running, you can now perform the CREATE, READ, UPDATE, and DELETE operations on the characters
resource.
To interact with our API, you can use REST clients like Postman. You can proceed to create a new character (POST request to '/api/characters'), get a list of all characters (GET request to '/api/characters'), update a character's details (PUT request to '/api/characters/:id'), and delete a character (DELETE request to '/api/characters/:id').
This guide serves as a basic introduction to setting up and working with CRUD operations in AdonisJS. Get creative with it and explore the many functionalities AdonisJS has to offer.
0 Comment
Sign up or Log in to leave a comment