Skip to content

Function: withResourceful() ​

ts
function withResourceful(
  options: Partial<ResourcefulMixinOptions>,
): <Model>(superclass: Model) => Model & ResourcefulModel;

Creates a mixin that adds resourceful CRUD functionality to Lucid models.

This function implements the mixin pattern to enhance AdonisJS Lucid models with metadata-driven CRUD operations, field-level access control, query scoping, and OpenAPI schema generation capabilities. The resulting model gains static methods for handling HTTP requests with built-in validation, pagination, filtering, and security features.

The mixin validates and normalizes configuration options, creates isolated metadata maps for each model class to prevent inheritance conflicts, and establishes an event emitter for monitoring ACL and validation operations.

Parameters ​

ParameterTypeDescription
optionsPartial<ResourcefulMixinOptions>Configuration object for customizing mixin behavior

Returns ​

A mixin function that accepts a Lucid model class and returns the enhanced model

ts
<Model>(superclass: Model): Model & ResourcefulModel;

Type Parameters ​

Type Parameter
Model extends NormalizeConstructor<LucidModel>

Parameters ​

ParameterType
superclassModel

Returns ​

Model & ResourcefulModel

Throws ​

When the provided options fail validation

Example ​

typescript
import { BaseModel, column } from "@ioc:Adonis/Lucid/Orm";
import { withResourceful, resourceful } from "lucid-resourceful";

class User extends withResourceful({
  name: "User",
  readRequiredForWrite: true,
  accessControlFilters: {
    read: [(ctx) => ctx.auth.user?.id === ctx.params.id],
    update: [(ctx) => ctx.auth.user?.isAdmin],
  },
})(BaseModel) {
  @column({ isPrimary: true })
  @resourceful({ type: "number", nullable: false })
  public id: number;

  @column()
  @resourceful({ type: "string", nullable: false })
  public name: string;
}

// Generated controller with CRUD operations
const UserController = User.generateController();

See ​