Tartalomjegyzék

< AdonisJS

AdonisJS - Lucid

A Lucidról

A Lucid egy ORM, ami alapból elérhető az AdonisJS-ben.

Leírások:

Telepítés

node ace add @adonisjs/lucid

Modell létrehozása

node ace make:model Emplyoee
import { DateTime } from 'luxon'
import { BaseModel, column } from '@adonisjs/lucid/orm'
 
export default class Employee extends BaseModel {
  @column({ isPrimary: true })
  declare id: number
 
  @column()
  declare name: string
 
  @column()
  declare city: string
 
  @column()
  declare salary: number
 
  @column.dateTime({ autoCreate: true })
  declare createdAt: DateTime
 
  @column.dateTime({ autoCreate: true, autoUpdate: true })
  declare updatedAt: DateTime
}

Migrációs fájl létrehozása

node ace make:migration users

Példa migrációs fájl:

import { BaseSchema } from '@adonisjs/lucid/schema'
 
export default class extends BaseSchema {
  protected tableName = 'employees'
 
  async up() {
    this.schema.createTable(this.tableName, (table) => {
      table.increments('id')
      table.string('name').notNullable()
      table.string('city').nullable()
      table.double('salary').nullable()
 
      table.timestamp('created_at')
      table.timestamp('updated_at')
    })
  }
 
  async down() {
    this.schema.dropTable(this.tableName)
  }
}

A migráció futtatása:

node ace migration:run