Saturday, 1 April 2023

Install Adonisjs for using API

1. Install Adnonis

npm init adonis-ts-app@latest hello-world

- select project structure: api

- setup eslint: false

 - run app

node ace serve --watch

- show command

node ace

2. Create controller

- node ace make:controller HomeController

# app/Controllers/Http/HomeController.ts 
export default class HomeController {
public async index() {
return {hello:"Adonis API"}
}
}

- edit route.ts

import Route from '@ioc:Adonis/Core/Route'

Route.get('/', async () => {
return { hello: 'world' }
})
Route.get('/home', 'HomeController.index')

- you can access: http://localhost:3333/home

3. Create migration

- install @adonisjs/lucid@alpha 

# install 
npm i @adonisjs/lucid@alpha
 
# using
node ace invoke @adonisjs/lucid 

- create model & migration

# create model 
node ace make:model Todo
 
# create migration
node ace make:migration Todos 
 
# run migrate
node ace migration:run

4. Debugs database

# generate a preloader file for event listeners.
node ace make:prldfile events 

- edit file ./start/events.ts

import Event from '@ioc:Adonis/Core/Event'

Event.on('db:query', function ({ sql, bindings }) {
console.log(sql, bindings)
}) 

- run debug in adonis

node ace serve --watch --node-args="--inspect" 
 
# can use package.json
"scripts": {
....
"debug": "node ace serve --watch --node-args=\"--inspect\"",
=> can run: yarn debug 
 

>> see more Github url 

Reference:
- https://docs.adonisjs.com/guides/introduction

Thank you. 

No comments:

Post a Comment

Golang Advanced Interview Q&A