Node.js version 20.12.2 or higher
npm 8.x or higher (or yarn/pnpm)
Supported databases:
PostgreSQL 10+
MySQL 5.7+
SQLite 3.8.8+
Microsoft SQL Server 2012+
Set up a Node.js project
mkdir lucid-project cd lucid-project npm init -yInstall Lucid Core
npm install @adonisjs/lucidConfigure Lucid
npx @adonisjs/lucid configureUpdate package.json Add the following to your package.json:
{ "type": "module", "scripts": { "start": "node ace serve --watch", "build": "node ace build --production" } }Create database configuration file Create a file
config/database.ts:import { defineConfig } from '@adonisjs/lucid' export default defineConfig({ connection: 'postgres', connections: { postgres: { driver: 'postgres', host: process.env.DB_HOST, port: Number(process.env.DB_PORT), user: process.env.DB_USER, password: process.env.DB_PASSWORD, database: process.env.DB_NAME, } } })Set up environment variables Create a
.envfile:DB_CONNECTION=postgres DB_HOST=127.0.0.1 DB_PORT=5432 DB_USER=lucid DB_PASSWORD=your_password DB_NAME=lucid_db
Create a model Create a file
app/Models/User.ts:import { BaseModel, column } from '@adonisjs/lucid/orm' export default class User extends BaseModel { @column({ isPrimary: true }) public id: number @column() public email: string @column() public name: string }Create a migration
node ace make:migration create_users_tableEdit the created migration file:
import { BaseSchema } from '@adonisjs/lucid/schema' export default class extends BaseSchema { protected tableName = 'users' public async up() { this.schema.createTable(this.tableName, (table) => { table.increments('id') table.string('email').notNullable().unique() table.string('name') table.timestamp('created_at', { useTz: true }) table.timestamp('updated_at', { useTz: true }) }) } public async down() { this.schema.dropTable(this.tableName) } }Run the migration
node ace migration:runUse the model in your code Create a file
app/Controllers/UsersController.ts:import User from '../Models/User' export default class UsersController { public async index() { return await User.all() } public async store({ request }) { const userData = request.only(['email', 'name']) return await User.create(userData) } }Set up routes In the
start/routes.tsfile:import Route from '@adonisjs/core/route' import UsersController from '../app/Controllers/UsersController' Route.get('/users', [UsersController, 'index']) Route.post('/users', [UsersController, 'store'])

