Skip to content

Quickstart

Lucid Resourceful is designed to be easily installed and implemented within an AdonisJS project.

⚠️ Prerequisites ⚠️

Lucid Resourceful is designed to work with the Lucid ORM within an AdonisJS project. The minimum compatible version for @adonis/lucid is 21.7.0.

Installation

Install @nhtio/lucid-resourceful directly from your preferred package manager using one of the following commands:

sh
npm i @nhtio/lucid-resourceful
sh
pnpm add @nhtio/lucid-resourceful
sh
yarn add @nhtio/lucid-resourceful

Implementation

In the file where your model is defined, add the imports for the withResourceful mixin and relevant decorators:

typescript
import { DateTime } from 'luxon'
import { compose } from '@adonisjs/core/helpers'
import { BaseModel, column } from '@adonisjs/lucid/orm'
import { BaseModel } from '@adonisjs/lucid/orm'
import { withResourceful, resourcefulColumn } from '@nhtio/lucid-resourceful'
import { ResourcefulUnsignedIntegerType } from '@nhtio/lucid-resourceful/definitions'

export default class User extends BaseModel {
  @column({ isPrimary: true })
  declare id: number

  @column.dateTime({ autoCreate: true })
  declare createdAt: DateTime

  @column.dateTime({ autoCreate: true, autoUpdate: true })
  declare updatedAt: DateTime
}
typescript
import { DateTime } from 'luxon'
import { compose } from '@adonisjs/core/helpers'
import { BaseModel } from '@adonisjs/lucid/orm'
import { withResourceful, resourcefulColumn } from '@nhtio/lucid-resourceful'
import { ResourcefulUnsignedIntegerType } from '@nhtio/lucid-resourceful/definitions' 

export default class User extends BaseModel {
  @column({ isPrimary: true })
  declare id: number

  @column.dateTime({ autoCreate: true })
  declare createdAt: DateTime

  @column.dateTime({ autoCreate: true, autoUpdate: true })
  declare updatedAt: DateTime
}

Apply the mixin to the model class:

typescript
import { DateTime } from 'luxon'
import { compose } from '@adonisjs/core/helpers'
import { BaseModel } from '@adonisjs/lucid/orm'
import { withResourceful, resourcefulColumn } from '@nhtio/lucid-resourceful'
import { ResourcefulUnsignedIntegerType } from '@nhtio/lucid-resourceful/definitions' 

export default class User extends BaseModel { 
export default class User extends compose(BaseModel, withResourceful({ name: 'User' })) { 
  @column({ isPrimary: true })
  declare id: number

  @column.dateTime({ autoCreate: true })
  declare createdAt: DateTime

  @column.dateTime({ autoCreate: true, autoUpdate: true })
  declare updatedAt: DateTime
}
typescript
import { DateTime } from 'luxon'
import { compose } from '@adonisjs/core/helpers'
import { BaseModel } from '@adonisjs/lucid/orm'
import { withResourceful, resourcefulColumn } from '@nhtio/lucid-resourceful'
import { ResourcefulUnsignedIntegerType } from '@nhtio/lucid-resourceful/definitions' 

export default class User extends compose(BaseModel, withResourceful({ name: 'User' })) {
  @column({ isPrimary: true })
  declare id: number

  @column.dateTime({ autoCreate: true })
  declare createdAt: DateTime

  @column.dateTime({ autoCreate: true, autoUpdate: true })
  declare updatedAt: DateTime
}

Finally, update the column decorators:

typescript
import { DateTime } from 'luxon'
import { compose } from '@adonisjs/core/helpers'
import { BaseModel } from '@adonisjs/lucid/orm'
import { withResourceful, resourcefulColumn } from '@nhtio/lucid-resourceful'
import { ResourcefulUnsignedIntegerType } from '@nhtio/lucid-resourceful/definitions' 

export default class User extends compose(BaseModel, withResourceful({ name: 'User' })) {
  @column({ isPrimary: true }) 
  @resourcefulColumn({ isPrimary: true, type: ResourcefulUnsignedIntegerType({ readOnly: true }) }) 
  declare id: number

  @column.dateTime({ autoCreate: true }) 
  @resourcefulColumn.dateTime({ autoCreate: true }) 
  declare createdAt: DateTime

  @column.dateTime({ autoCreate: true, autoUpdate: true }) 
  @resourcefulColumn.dateTime({ autoCreate: true, autoUpdate: true }) 
  declare updatedAt: DateTime
}
typescript
import { DateTime } from 'luxon'
import { compose } from '@adonisjs/core/helpers'
import { BaseModel } from '@adonisjs/lucid/orm'
import { withResourceful, resourcefulColumn } from '@nhtio/lucid-resourceful'
import { ResourcefulUnsignedIntegerType } from '@nhtio/lucid-resourceful/definitions' 

export default class User extends compose(BaseModel, withResourceful({ name: 'User' })) {
  @resourcefulColumn({ isPrimary: true, type: ResourcefulUnsignedIntegerType({ readOnly: true }) })
  declare id: number

  @resourcefulColumn.dateTime({ autoCreate: true })
  declare createdAt: DateTime

  @resourcefulColumn.dateTime({ autoCreate: true, autoUpdate: true })
  declare updatedAt: DateTime
}

Next Steps