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-resourcefulsh
pnpm add @nhtio/lucid-resourcefulsh
yarn add @nhtio/lucid-resourcefulImplementation
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
- Learn more about the
withResourcefulmixin configuration options in the Configuration Documentation - Learn more about the decorators and how to use them in the Decorators Documentation
- Learn more about the property type definitions and how to use them in the Data Type Definitions Documentation
- Learn more about building validation schemas for use with the mixin and other methods in the Validation Documentation
- Learn more about how Lucid Resourceful handles errors and the kinds of errors thrown in the Error handling Documentation
- Learn more about how to use the Resourceful CRUD operations in the CRUD operations Documentation
- Learn more about how to use the OpenAPI functionality in the OpenAPI functionality Documentation
- Learn more about the various helpers and utilities provided by Lucid Resourceful in the Helpers and Utilities Documentation