Skip to content

Resourceful Decorators ​

Lucid Resourceful provides a comprehensive set of decorators that enhance AdonisJS Lucid models with metadata-driven functionality. These decorators add validation, access control, OpenAPI schema generation, and type safety to your model properties and relationships.

Overview ​

The resourceful decorators extend the standard Lucid decorators with additional capabilities:

  • Type Validation: Built-in Joi schema validation for all property types
  • Access Control: Field-level read/write permissions via ACL filters
  • OpenAPI Integration: Automatic schema generation for API documentation
  • Data Transformation: Automatic prepare/consume functions for type safety
  • Metadata Storage: Rich metadata for runtime introspection

Core Decorators ​

Column Decorators ​

The @resourcefulColumn decorator and its type-specific variants handle database columns with enhanced functionality:

  • @resourcefulColumn - Generic column decorator with custom type definition
  • @resourcefulColumn.string - String columns with automatic string handling
  • @resourcefulColumn.number - Numeric columns with number validation
  • @resourcefulColumn.integer - Integer columns with integer validation
  • @resourcefulColumn.bigint - BigInt columns with bigint handling
  • @resourcefulColumn.unsignedint - Unsigned integer columns
  • @resourcefulColumn.boolean - Boolean columns with boolean conversion
  • @resourcefulColumn.date - Date columns with date handling
  • @resourcefulColumn.dateTime - DateTime columns with timestamp handling
  • @resourcefulColumn.binary - Binary/buffer columns
  • @resourcefulColumn.object - JSON object columns
  • @resourcefulColumn.array - JSON array columns

Computed Accessor Decorators ​

The @resourcefulComputed decorator and its variants handle computed properties:

  • @resourcefulComputed - Generic computed accessor with custom type
  • @resourcefulComputed.string - String computed accessors
  • @resourcefulComputed.number - Number computed accessors
  • @resourcefulComputed.integer - Integer computed accessors
  • @resourcefulComputed.bigint - BigInt computed accessors
  • @resourcefulComputed.unsignedint - Unsigned integer computed accessors
  • @resourcefulComputed.boolean - Boolean computed accessors
  • @resourcefulComputed.date - Date computed accessors
  • @resourcefulComputed.dateTime - DateTime computed accessors
  • @resourcefulComputed.binary - Binary computed accessors
  • @resourcefulComputed.object - Object computed accessors
  • @resourcefulComputed.array - Array computed accessors

Relationship Decorators ​

The relationship decorators provide enhanced relationship handling with resourceful features:

Common Configuration Options ​

All resourceful decorators support a common set of configuration options:

Type Definition ​

typescript
type: ResourcefulDataType // Required - defines the field type and validation

Access Control ​

typescript
readAccessControlFilters: ResourcefulAccessControlFilter[]  // Controls read access
writeAccessControlFilters: ResourcefulAccessControlFilter[] // Controls write access
clientWritable?: boolean // Defaults to true; false blocks client writes in resourceful routes while allowing internal writes

Server-owned fields ​

Set clientWritable: false on a column, computed accessor, or relationship to reject client writes (including null) using the existing write-access rejection behavior (the payload validation layer may reject the field before the access check):

typescript
@resourcefulColumn.string({ clientWritable: false })
declare syncStatus: string

The default is true, preserving existing behavior. This applies to generated REST and OData create/update/bulk payloads, relationship sync, and consumers explicitly calling the $onResourcefulCreate, $onResourcefulUpdate, or bulk resourceful payload APIs. It is not a Lucid save hook: internal Model.create, merge, and save remain unrestricted, even with an HTTP context in scope. $meta.create/$meta.update and OpenAPI omit these fields from writable schemas; readable fields remain in read schemas with readOnly: true. For relationships, this controls syncing the relationship, not direct writes to the related model or foreign-key column; mark those columns separately if needed.

Filtering and sorting ​

typescript
filterable?: boolean // Defaults to true; allows filtering and bare-term search
sortable?: boolean   // Defaults to true; allows sorting

A column with serializeAs: null is invisible to the API: as far as the client is concerned, the property does not exist. It is not exposed in serialized responses and is never filterable, sortable, or searchable, regardless of filterable or sortable. The filtering rule also applies to relationship paths.

Metadata ​

typescript
nullable: boolean           // Whether the field can be null
description?: string        // Field description for documentation
default?: any              // Default value
deprecated?: boolean       // Mark field as deprecated
example?: string           // Example value for documentation
externalDocs?: ExternalDocumentationObject // External documentation link

Validation Scoping ​

typescript
validationScoper?: ValidationScoper // Dynamic validation modification

Quick Examples ​

Basic Column Usage ​

typescript
import { BaseModel } from '@adonisjs/lucid/orm'
import { compose } from '@adonisjs/core/helpers'
import { withResourceful, resourcefulColumn } from '@nhtio/lucid-resourceful'
import { ResourcefulStringType, 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.string({ 
    type: ResourcefulStringType({ minLength: 2, maxLength: 100 }),
    nullable: false
  })
  declare name: string

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

  declare updatedAt: DateTime
}

Access Control Example ​

typescript
@resourcefulColumn.string({
  type: ResourcefulStringType(),
  nullable: false,
  readAccessControlFilters: [
    (ctx) => ctx.auth.user?.role === 'admin'
  ],
  writeAccessControlFilters: [
    (ctx) => ctx.auth.user?.role === 'admin'

  ]
})
declare sensitiveField: string

Relationship Example ​

typescript
@resourcefulHasMany(() => Post, { 
  foreignKey: 'user_id',
  readAccessControlFilters: [
    (ctx) => !!ctx.auth.user
  ]
})
declare posts: HasMany<typeof Post>

API Reference ​

For complete API documentation, see:

Data Types ​

All decorators require a type parameter that must be a ResourcefulDataType instance. See the Data Type Definitions for details on available data types and their configuration options.

Next Steps ​