Documentation / @zerotal/auth / Role
Class: Role
Defined in: auth/src/rbac/Role.ts:10
A role groups permissions and is assigned to models (users) via the
polymorphic model_roles pivot. Roles own permissions through the
role_permissions pivot.
Extends
Constructors
Constructor
new Role():
Role
Returns
Role
Inherited from
Attributes & mass assignment
table
statictable:string
Defined in: orm/src/model/BaseModel.ts:569
Database table this model maps to. Usually set for you by the @table("…")
decorator; assign directly to override.
Inherited from
primaryKey
staticprimaryKey:string="id"
Defined in: orm/src/model/BaseModel.ts:576
Primary-key column name. Defaults to id.
Inherited from
casts?
staticoptionalcasts?:Record<string,"boolean"|"json"|"date"|"array"|"datetime"|"integer"|"float"|"enum"|"immutable_datetime"|`decimal:${number}`| {get?: (dbValue) =>unknown;set?: (jsValue) =>unknown; } |CastContract<unknown> |undefined>
Defined in: orm/src/model/BaseModel.ts:604
Per-attribute cast map applied on read/write — string shorthands
("boolean", "json", "array", "date", "datetime", "integer",
"float", "decimal:2", "enum", "immutable_datetime") or a custom
cast object with get/set. Merged across the prototype chain.
Inherited from
reactiveCasts
staticreactiveCasts:boolean=true
Defined in: orm/src/model/BaseModel.ts:621
Wrap json/array cast columns in a reactive proxy so mutating them in place
(user.meta.count = 99) marks the column dirty. Defaults to true.
Off, the failure is silent and looks like success: _applyRow stores the same object
reference in the instance and in _original, and $dirty() compares with !==, so
user.meta.count = 99; await user.save() issues no UPDATE and reports no error. The
proxy is allocated only for columns actually cast to json/array.
Set false for a model where that cost is measurable and every write to a JSON column
replaces the whole value (user.meta = { ...user.meta, count: 99 }), which dirty
tracking sees either way.
Inherited from
fillable?
staticoptionalfillable?:string[]
Defined in: orm/src/model/BaseModel.ts:663
Allowlist of camelCase field names accepted by create() / fill().
When set, any key not in this list is rejected with MassAssignmentError.
Cannot be used together with guarded.
Inherited from
guarded?
staticoptionalguarded?:string[]
Defined in: orm/src/model/BaseModel.ts:672
Denylist of camelCase field names blocked from create() / fill().
When set, listed keys are rejected; all other keys are accepted.
Cannot be used together with fillable.
Inherited from
unguarded
staticunguarded:boolean=false
Defined in: orm/src/model/BaseModel.ts:685
Disable mass-assignment protection for this model — every attribute passed
to fill() / create() is accepted.
Models guard by default: when neither fillable nor guarded is
declared, fill() rejects every attribute (throwing MassAssignmentError)
so an unexpected key from a request body can never reach the database. Set
this to true only for models whose writes never come from user input.
Inherited from
unguard()
staticunguard():void
Defined in: orm/src/model/BaseModel.ts:705
Turn off mass-assignment guarding process-wide (trusted contexts only).
Returns
void
Inherited from
reguard()
staticreguard():void
Defined in: orm/src/model/BaseModel.ts:714
Restore mass-assignment guarding process-wide.
Returns
void
Inherited from
withoutGuard()
staticwithoutGuard<T>(callback):Promise<T>
Defined in: orm/src/model/BaseModel.ts:724
Run callback with mass-assignment guarding disabled process-wide,
restoring the previous setting afterwards (even on throw).
Type Parameters
T
T
Parameters
callback
() => T | Promise<T>
Returns
Promise<T>
Inherited from
id
id:
number
Defined in: orm/src/model/BaseModel.ts:819
Primary-key value. Populated after save inserts a new row, or when the instance is hydrated from the database.
Inherited from
fill()
fill(
data):this
Defined in: orm/src/model/BaseModel.ts:1235
Mass-assign fields, respecting fillable / guarded protection.
Call this instead of Object.assign when data comes from user input.
Guarded by default: a model that declares neither fillable nor
guarded (and is not unguarded) rejects every attribute with a
MassAssignmentError, so a stray key from a request body can never reach
the database. Declare fillable to allow specific columns, or use
forceFill for trusted, framework-internal writes.
Accepts UpdatePayload<this> — a partial of the model's writable,
non-relation, non-auto-managed columns. Passing id, createdAt,
relations, or methods is a compile-time error.
Parameters
data
UpdatePayload<this>
Returns
this
Example
post.fill(ctx.body<UpdatePayload<Post>>());
Throws
when data contains a key not permitted by
this model's fillable / guarded configuration.
Inherited from
forceFill()
forceFill(
data):this
Defined in: orm/src/model/BaseModel.ts:1277
Mass-assign fields bypassing fillable / guarded protection.
Use only for trusted data you construct yourself (framework-internal
writes, factories, seeders) — never for request input.
Parameters
data
Record<string, unknown>
Returns
this
Example
// Trusted, non-user data:
role.forceFill({ name, guard });
Inherited from
isDirty()
isDirty(
column?):boolean
Defined in: orm/src/model/BaseModel.ts:2167
True when the model (or a specific column) has unsaved changes since it
was loaded or last saved.
Parameters
column?
string
Returns
boolean
Inherited from
$dirty()
$dirty():
Record<string,unknown>
Defined in: orm/src/model/BaseModel.ts:2182
Return a map of changed columns to their current values — the set that a subsequent save would write.
Returns
Record<string, unknown>
Inherited from
markDirty()
markDirty(
property):this
Defined in: orm/src/model/BaseModel.ts:2202
Force a property to be treated as dirty so it is included in the next save, even if its value is reference-equal to the loaded snapshot (e.g. an in-place mutation of a JSON column).
Parameters
property
keyof Role
Returns
this
Inherited from
Comparison
is()
is(
other):boolean
Defined in: orm/src/model/BaseModel.ts:1900
True when other is the same model class with the same primary key.
Parameters
other
BaseModel | null | undefined
Returns
boolean
Inherited from
isNot()
isNot(
other):boolean
Defined in: orm/src/model/BaseModel.ts:1915
Inverse of is.
Parameters
other
BaseModel | null | undefined
Returns
boolean
Inherited from
Lifecycle & hooks
dispatchesEvents?
staticoptionaldispatchesEvents?:Record<string, (model) =>object>
Defined in: orm/src/model/BaseModel.ts:654
Maps lifecycle events to event classes that are dispatched on the app
event bus when they fire (via $dispatchesEvents). Keys: creating, created,
updating, updated, saving, saved, deleting, deleted, retrieved. Each event
class is constructed with the model instance and emitted (no-op if no bus is bound).
Example
static dispatchesEvents = { created: OrderPlaced, deleted: OrderCancelled };
Inherited from
observe()
staticobserve<T>(this,ObserverClass):void
Defined in: orm/src/model/BaseModel.ts:806
Register an observer class for this model. The observer's lifecycle methods (creating, created, updating, …) are wired into the HookRegistry automatically.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
ObserverClass
() => ModelObserver<T>
Returns
void
Example
User.observe(UserObserver); // call once at boot in a ServiceProvider
Inherited from
Other
resolve()
staticresolve(name,guard?):Promise<Role>
Defined in: auth/src/rbac/Role.ts:31
Find an existing role by name (+ guard) or create it.
Parameters
name
string
guard?
string = "web"
Returns
Promise<Role>
resolveId()
staticresolveId(name,guard?):Promise<number>
Defined in: auth/src/rbac/Role.ts:39
Resolve a name (+ guard) to a role id, cached. Creates if missing.
Parameters
name
string
guard?
string = "web"
Returns
Promise<number>
idsFor()
staticidsFor(items,guard?):Promise<number[]>
Defined in: auth/src/rbac/Role.ts:50
Resolve a mixed list of names / ids / instances to role ids.
Parameters
items
(string | number | Role)[]
guard?
string = "web"
Returns
Promise<number[]>
name
name:
string
Defined in: auth/src/rbac/Role.ts:11
guard
guard:
string
Defined in: auth/src/rbac/Role.ts:12
label?
optionallabel?:string|null
Defined in: auth/src/rbac/Role.ts:14
Display name. Optional, so resolve() can create a role from a name alone.
permissions
permissions:
ManyToMany<Permission>
Defined in: auth/src/rbac/Role.ts:21
givePermissionTo()
givePermissionTo(...
permissions):Promise<Role>
Defined in: auth/src/rbac/Role.ts:63
Grant one or more permissions to this role (names are created if missing).
Parameters
permissions
...(string | number | Permission)[]
Returns
Promise<Role>
revokePermissionTo()
revokePermissionTo(...
permissions):Promise<Role>
Defined in: auth/src/rbac/Role.ts:77
Revoke one or more permissions from this role.
Parameters
permissions
...(string | number | Permission)[]
Returns
Promise<Role>
syncPermissions()
syncPermissions(
permissions):Promise<Role>
Defined in: auth/src/rbac/Role.ts:88
Replace this role's permissions with exactly the given set.
Parameters
permissions
(string | number | Permission)[]
Returns
Promise<Role>
permissionNames()
permissionNames():
Promise<string[]>
Defined in: auth/src/rbac/Role.ts:94
Names of the permissions attached to this role.
Returns
Promise<string[]>
hasPermission()
hasPermission(
name):Promise<boolean>
Defined in: auth/src/rbac/Role.ts:105
Does this role include the given permission (exact match)?
Parameters
name
string
Returns
Promise<boolean>
__isZerotalModel
readonly__isZerotalModel:true
Defined in: orm/src/model/BaseModel.ts:561
Phantom nominal brand, used ONLY at the type level to detect relation
properties (see ColumnKeys in payload.ts). Detecting relations by this one
marker — rather than structurally via extends BaseModel — avoids forcing TS
to re-resolve a related model's fill(data: UpdatePayload<this>) signature,
which is itself defined in terms of ColumnKeys. That structural feedback loop
is what makes mutually-referential models (A.b: B, B.a: A) trip TS2615
("circularly references itself in mapped type").
declare => purely type-level, no runtime field is emitted and instances
never carry it. The leading underscore keeps it out of column/serialization
key sets automatically.
Inherited from
Persistence
hashable?
staticoptionalhashable?:string[]
Defined in: orm/src/model/BaseModel.ts:794
Fields that are automatically hashed with Bun.password.hash() (bcrypt)
before every INSERT and whenever the field changes on UPDATE.
The hash is applied transparently in save() — the plaintext value is
never written to the database. Use Bun.password.verify() to check
a plaintext candidate against the stored hash.
Example
static hashable = ['password'];
// Verify later:
const ok = await Bun.password.verify(candidate, user.password);
Inherited from
massPrune
staticmassPrune:boolean=false
Defined in: orm/src/model/BaseModel.ts:913
When true, prune() permanently deletes rows (forceDelete) rather than
soft-deleting them.
Inherited from
prunable()?
staticoptionalprunable<T>(this):ModelQueryBuilder<T>
Defined in: orm/src/model/BaseModel.ts:925
Override to return the query selecting records eligible for pruning.
Implement this to make a model "prunable" (used by prune() and a
scheduled model:prune task).
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
Returns
Example
static prunable() { return this.query().where('created_at', '<', cutoff); }
Inherited from
prune()
staticprune<T>(this,chunkSize?):Promise<number>
Defined in: orm/src/model/BaseModel.ts:934
Delete prunable records in chunks. Returns the number of records pruned.
Honours massPrune (permanent delete) vs. soft delete.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
chunkSize?
number = 1000
Returns
Promise<number>
Throws
when the model does not define a static prunable() method.
Inherited from
create()
staticcreate<T>(this,data):Promise<T>
Defined in: orm/src/model/BaseModel.ts:1208
Mass-assign data (respecting fillable / guarded) onto a new
instance and save it, returning the persisted model.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
data
Returns
Promise<T>
Throws
when data contains a non-fillable key.
Example
const user = await User.create({ name: "Ada", email: "ada@example.com" });
Inherited from
forceCreate()
staticforceCreate<T>(this,data):Promise<T>
Defined in: orm/src/model/BaseModel.ts:1291
Like create, but bypasses mass-assignment protection. Use only for trusted data (framework-internal writes, seeders), never for request input.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
data
Record<string, unknown>
Returns
Promise<T>
Inherited from
firstOrCreate()
staticfirstOrCreate<T>(this,search,create?):Promise<T>
Defined in: orm/src/model/BaseModel.ts:1307
Return the first row matching search, or create one from
search merged with create.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
search
create?
Partial<InsertPayload<T>>
Returns
Promise<T>
Throws
when a created key is not fillable.
Inherited from
updateOrCreate()
staticupdateOrCreate<T>(this,search,values?):Promise<T>
Defined in: orm/src/model/BaseModel.ts:1333
Update the first row matching search, or create it. Returns the model.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
search
values?
Partial<InsertPayload<T>>
Returns
Promise<T>
Example
await User.updateOrCreate({ email }, { name, lastSeenAt: new Date() });
Throws
when an updated/created key is not fillable.
Inherited from
firstOrNew()
staticfirstOrNew<T>(this,search,values?):Promise<T>
Defined in: orm/src/model/BaseModel.ts:1360
Return the first row matching search, or a new unsaved instance
filled with search + values.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
search
values?
Partial<InsertPayload<T>>
Returns
Promise<T>
Throws
when a filled key is not fillable.
Inherited from
findOrNew()
staticfindOrNew<T>(this,id):Promise<T>
Defined in: orm/src/model/BaseModel.ts:1381
Find by primary key, or return a new unsaved instance if not found.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
id
string | number
Returns
Promise<T>
Inherited from
createMany()
staticcreateMany<T>(this,records):Promise<T[]>
Defined in: orm/src/model/BaseModel.ts:1398
Create multiple rows one at a time, returning the saved model instances.
Each row goes through the full save() path — casts, hashing, timestamps,
and observer/hook events all fire per row.
For large bulk loads where per-row events are not needed, prefer bulkInsert which issues a single multi-row INSERT.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
records
InsertPayload<T>[]
Returns
Promise<T[]>
Throws
when any record contains a non-fillable key.
Inherited from
bulkInsert()
staticbulkInsert<T>(this,records):Promise<number>
Defined in: orm/src/model/BaseModel.ts:1416
Insert many rows in a single multi-row INSERT, returning the number of
rows written. Applies casts and timestamps but bypasses per-row save()
lifecycle hooks — the fast path for bulk loads. Use createMany when
you need hooks/observers per row.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
records
InsertPayload<T>[]
Returns
Promise<number>
the number of rows inserted (0 for an empty input).
Inherited from
saveMany()
staticsaveMany<T>(this,models):Promise<T[]>
Defined in: orm/src/model/BaseModel.ts:1470
Persist multiple already-built instances (each via save).
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
models
T[]
Returns
Promise<T[]>
Inherited from
upsert()
staticupsert<T>(this,data,conflictKeys,updateCols?):Promise<void>
Defined in: orm/src/model/BaseModel.ts:1503
INSERT a row; update specified columns when the unique constraint fires.
- PostgreSQL / SQLite:
ON CONFLICT (conflictKeys) DO UPDATE SET … - MySQL:
ON DUPLICATE KEY UPDATE … = VALUES(…)
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
data
Row data (camelCase keys are converted to snake_case)
conflictKeys
keyof T & string[]
Columns that define the conflict constraint (ignored on MySQL)
updateCols?
keyof T & string[]
Columns to overwrite on conflict (defaults to all non-conflict cols)
Returns
Promise<void>
Example
await User.upsert({ email: 'a@b.com', name: 'Alice' }, ['email'], ['name']);
Inherited from
save()
save():
Promise<Role>
Defined in: orm/src/model/BaseModel.ts:1594
Persist this instance: INSERT when new, or UPDATE of only the dirty
columns when it already exists. Applies casts, hashes hashable
fields, maintains timestamps, and runs the save/create/update lifecycle
hooks. On insert the new primary key (and full row) are read back onto the
instance. Returns this.
Returns
Promise<Role>
Example
const user = new User();
user.fill({ name: "Ada", email: "ada@example.com" });
await user.save();
Inherited from
delete()
delete():
Promise<void>
Defined in: orm/src/model/BaseModel.ts:1754
Delete this record. For soft-delete models this sets deleted_at (the row
stays in the table but is hidden from default queries); otherwise it issues
a hard DELETE. Runs the before/after delete lifecycle hooks.
Returns
Promise<void>
Inherited from
fresh()
fresh():
Promise<Role>
Defined in: orm/src/model/BaseModel.ts:1834
Re-read this record from the database and return it as a new instance, leaving the current one untouched. Use refresh to mutate in place.
Returns
Promise<Role>
Throws
when the row no longer exists.
Inherited from
refresh()
refresh():
Promise<Role>
Defined in: orm/src/model/BaseModel.ts:1852
Reload this instance's attributes from the database, mutating it in place.
Unlike fresh() (which returns a new instance) this updates this.
Returns
Promise<Role>
Throws
when the row no longer exists.
Inherited from
replicate()
replicate(
except?):this
Defined in: orm/src/model/BaseModel.ts:1870
Copy this model into a new unsaved instance. The primary key and
timestamps are not copied; pass except to omit additional columns.
Parameters
except?
string[]
Returns
this
Inherited from
increment()
increment(
column,amount?):Promise<Role>
Defined in: orm/src/model/BaseModel.ts:1924
Atomically increment a column in the DB and on this instance.
Parameters
column
"id" | "fill" | "name" | "delete" | "append" | "toJSON" | "load" | "__isZerotalModel" | "createdAt" | "updatedAt" | "forceFill" | "save" | "loadMissing" | "fresh" | "refresh" | "replicate" | "touch" | "is" | "isNot" | "increment" | "decrement" | "loadCount" | "loadSum" | "loadAvg" | "loadMin" | "loadMax" | "makeHidden" | "makeVisible" | "associate" | "dissociate" | "isDirty" | "$dirty" | "markDirty" | "guard" | "permissions" | "label" | "givePermissionTo" | "revokePermissionTo" | "syncPermissions" | "permissionNames" | "hasPermission"
amount?
number = 1
Returns
Promise<Role>
Inherited from
decrement()
decrement(
column,amount?):Promise<Role>
Defined in: orm/src/model/BaseModel.ts:1933
Atomically decrement a column in the DB and on this instance.
Parameters
column
"id" | "fill" | "name" | "delete" | "append" | "toJSON" | "load" | "__isZerotalModel" | "createdAt" | "updatedAt" | "forceFill" | "save" | "loadMissing" | "fresh" | "refresh" | "replicate" | "touch" | "is" | "isNot" | "increment" | "decrement" | "loadCount" | "loadSum" | "loadAvg" | "loadMin" | "loadMax" | "makeHidden" | "makeVisible" | "associate" | "dissociate" | "isDirty" | "$dirty" | "markDirty" | "guard" | "permissions" | "label" | "givePermissionTo" | "revokePermissionTo" | "syncPermissions" | "permissionNames" | "hasPermission"
amount?
number = 1
Returns
Promise<Role>
Inherited from
Querying
connection?
staticoptionalconnection?:string
Defined in: orm/src/model/BaseModel.ts:776
Optional named connection (registered via registerConnection). When set, queries for this model resolve to that connection instead of the default.
Inherited from
addGlobalScope()
staticaddGlobalScope<T>(this,name,callback):void
Defined in: orm/src/model/BaseModel.ts:855
Register a named global scope applied to every query for this model. The callback receives the query builder and constrains it (e.g. a tenant filter). Remove it later with removeGlobalScope.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
name
string
callback
Returns
void
Inherited from
removeGlobalScope()
staticremoveGlobalScope<T>(this,name):void
Defined in: orm/src/model/BaseModel.ts:874
Remove a previously registered global scope by name.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
name
string
Returns
void
Inherited from
registerConnection()
staticregisterConnection(name,conn,dialect?):void
Defined in: orm/src/model/BaseModel.ts:884
Register a named connection that models can select via static connection.
Stored on the current OrmContext (execution-scoped).
Parameters
name
string
conn
dialect?
Dialect
Returns
void
Inherited from
query()
staticquery<T>(this):ModelQueryBuilder<T>
Defined in: orm/src/model/BaseModel.ts:986
Start a new query builder for this model — the entry point for building
where/orderBy/with/etc. chains. Applies the soft-delete scope
(deleted_at IS NULL) when the model uses soft deletes.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
Returns
Example
const posts = await Post.query().where("published", true).orderBy("createdAt", "desc").get();
Inherited from
where()
Call Signature
staticwhere<T>(this,column,value):ModelQueryBuilder<T>
Defined in: orm/src/model/BaseModel.ts:1014
Start a query constrained by a WHERE clause. Pass (column, value) for
equality or (column, operator, value) for any other comparison.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
column
string
value
unknown
Returns
Example
const recent = await Post.where("views", ">=", 100).get();
Inherited from
Call Signature
staticwhere<T>(this,column,operator,value):ModelQueryBuilder<T>
Defined in: orm/src/model/BaseModel.ts:1019
Start a query constrained by a WHERE clause. Pass (column, value) for
equality or (column, operator, value) for any other comparison.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
column
string
operator
value
unknown
Returns
Example
const recent = await Post.where("views", ">=", 100).get();
Inherited from
whereIn()
staticwhereIn<T>(this,column,values):ModelQueryBuilder<T>
Defined in: orm/src/model/BaseModel.ts:1042
Start a query constrained by WHERE column IN (values).
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
column
string
values
unknown[]
Returns
Inherited from
orderBy()
staticorderBy<T>(this,column,direction?):ModelQueryBuilder<T>
Defined in: orm/src/model/BaseModel.ts:1055
Start a query ordered by column (default direction "asc").
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
column
string
direction?
OrderDirection = "asc"
Returns
Inherited from
latest()
staticlatest<T>(this,column?):ModelQueryBuilder<T>
Defined in: orm/src/model/BaseModel.ts:1068
Start a query ordered newest-first by column (default "created_at").
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
column?
string = "created_at"
Returns
Inherited from
oldest()
staticoldest<T>(this,column?):ModelQueryBuilder<T>
Defined in: orm/src/model/BaseModel.ts:1080
Start a query ordered oldest-first by column (default "created_at").
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
column?
string = "created_at"
Returns
Inherited from
first()
staticfirst<T>(this):Promise<T|null>
Defined in: orm/src/model/BaseModel.ts:1092
Fetch the first row, or null when none match.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
Returns
Promise<T | null>
Inherited from
firstOrFail()
staticfirstOrFail<T>(this):Promise<T>
Defined in: orm/src/model/BaseModel.ts:1102
Fetch the first row, or throw when none match.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
Returns
Promise<T>
Throws
when no row matches.
Inherited from
count()
staticcount<T>(this):Promise<number>
Defined in: orm/src/model/BaseModel.ts:1111
Count all rows (subject to any global/soft-delete scopes).
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
Returns
Promise<number>
Inherited from
find()
staticfind<T>(this,id):Promise<T|null>
Defined in: orm/src/model/BaseModel.ts:1120
Find a single row by primary key, or null when not found.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
id
string | number
Returns
Promise<T | null>
Inherited from
findOrFail()
staticfindOrFail<T>(this,id):Promise<T>
Defined in: orm/src/model/BaseModel.ts:1137
Find a single row by primary key, or throw when not found.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
id
string | number
Returns
Promise<T>
Throws
when no row has the given primary key.
Example
const user = await User.findOrFail(ctx.integer("id"));
Inherited from
findBy()
staticfindBy<T>(this,column,value):Promise<T|null>
Defined in: orm/src/model/BaseModel.ts:1152
Find the first row where column equals value (the column name is
converted to snake_case), or null when none match.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
column
string
value
unknown
Returns
Promise<T | null>
Inherited from
all()
staticall<T>(this):Promise<T[]>
Defined in: orm/src/model/BaseModel.ts:1165
Fetch every row for this model (subject to global/soft-delete scopes).
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
Returns
Promise<T[]>
Inherited from
paginate()
staticpaginate<T>(this,perPage?,page?,pageName?):Promise<PaginateResult<T>>
Defined in: orm/src/model/BaseModel.ts:1188
Fetch one page of rows (subject to global/soft-delete scopes).
The page comes from the request in flight — the ?page= query string, or whatever a
server-driven view registered instead — so a controller or a Flow page reads
Post.paginate(10) and gets the page the user is actually on. Pass page to override.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
perPage?
number = 15
Rows per page. Defaults to 15.
page?
number
1-based page. Omit to use the request's current page.
pageName?
string = "page"
Which paginator to read, so one page can drive several. Defaults to "page".
Returns
Promise<PaginateResult<T>>
Example
const posts = await Post.paginate(10); // ?page= (or the view's page)
const invoices = await Invoice.paginate(10, undefined, "invoices"); // a second paginator
Inherited from
findMany()
staticfindMany<T>(this,ids):Promise<T[]>
Defined in: orm/src/model/BaseModel.ts:1480
Fetch many rows by an array of primary keys.
Type Parameters
T
T extends BaseModel
Parameters
this
ModelCtor<T>
ids
(string | number)[]
Returns
Promise<T[]>
Inherited from
scope()
staticscope<Args>(fn): (...args) =>ScopeApplicator
Defined in: orm/src/model/BaseModel.ts:1572
Define a named query scope with typed arguments. Assign to a static property; apply via .withScopes(s => s.active()).
Type Parameters
Args
Args extends unknown[]
Parameters
fn
(query, ...args) => void
Returns
(...args) => ScopeApplicator
Example
static active = BaseModel.scope((q) => q.where('active', 1));
static byScore = BaseModel.scope((q, min: number) => q.where('score', '>=', min));
Inherited from
Relationships
load()
load(
relations):Promise<Role>
Defined in: orm/src/model/BaseModel.ts:1793
Eager-load the given relations onto this already-fetched model instance. If a relation is already loaded, it is reloaded.
Parameters
relations
string[]
Returns
Promise<Role>
Example
const post = await Post.find(1);
await post.load(['comments', 'tags']);
// post.comments is now populated
Inherited from
loadMissing()
loadMissing(
relations):Promise<Role>
Defined in: orm/src/model/BaseModel.ts:1815
Like load(), but skips relations that are already loaded on this instance.
Parameters
relations
string[]
Returns
Promise<Role>
Example
await post.loadMissing(['comments']); // no-op if comments already loaded
Inherited from
loadCount()
loadCount(
relations):Promise<Role>
Defined in: orm/src/model/BaseModel.ts:1962
Load relation COUNT(s) onto this instance (sets <rel>Count).
Parameters
relations
string | string[]
Returns
Promise<Role>
Inherited from
loadCount()
staticloadCount<T>(this,models,relations):Promise<T[]>
Defined in: orm/src/model/BaseModel.ts:1990
Load relation COUNT(s) for an array of already-fetched models in a SINGLE
query (no N+1), setting <rel>Count on each — prefer this over calling the
instance loadCount() in a loop.
Type Parameters
T
T extends BaseModel
Parameters
this
typeof BaseModel
models
T[]
relations
string | string[]
Returns
Promise<T[]>
Example
const posts = await Post.all();
await Post.loadCount(posts, "comments");
posts[0]!.commentsCount;
Inherited from
loadSum()
loadSum(
relation,column):Promise<Role>
Defined in: orm/src/model/BaseModel.ts:2044
Load SUM(column) over a relation onto this instance (sets <rel>Sum<Column>).
Parameters
relation
string
column
string
Returns
Promise<Role>
Inherited from
loadAvg()
loadAvg(
relation,column):Promise<Role>
Defined in: orm/src/model/BaseModel.ts:2052
Load AVG(column) over a relation onto this instance (sets <rel>Avg<Column>).
Parameters
relation
string
column
string
Returns
Promise<Role>
Inherited from
loadMin()
loadMin(
relation,column):Promise<Role>
Defined in: orm/src/model/BaseModel.ts:2060
Load MIN(column) over a relation onto this instance (sets <rel>Min<Column>).
Parameters
relation
string
column
string
Returns
Promise<Role>
Inherited from
loadMax()
loadMax(
relation,column):Promise<Role>
Defined in: orm/src/model/BaseModel.ts:2068
Load MAX(column) over a relation onto this instance (sets <rel>Max<Column>).
Parameters
relation
string
column
string
Returns
Promise<Role>
Inherited from
associate()
associate(
relation,model):this
Defined in: orm/src/model/BaseModel.ts:2116
Set this model's belongsTo foreign key to model and cache the relation.
Does not persist — call save() afterwards.
Parameters
relation
string
model
Returns
this
Example
comment.associate('post', post);
await comment.save();
Throws
when relation is not a belongsTo relation on this model.
Inherited from
dissociate()
dissociate(
relation):this
Defined in: orm/src/model/BaseModel.ts:2143
Clear this model's belongsTo foreign key and cached relation.
Parameters
relation
string
Returns
this
Throws
when relation is not a belongsTo relation on this model.
Inherited from
Route binding
implicitBinding?
staticoptionalimplicitBinding?:boolean
Defined in: orm/src/model/BaseModel.ts:630
Whether this model participates in implicit route-model binding (default: true).
When on, a route param matching the model name auto-resolves to a loaded instance
(e.g. :user -> User.findOrFail(value)). Set to false to opt out.
Inherited from
implicitBindingKey?
staticoptionalimplicitBindingKey?:string
Defined in: orm/src/model/BaseModel.ts:641
Override which route param this model claims for implicit binding. By default a model
named User binds the :user param; set this to claim a different key.
Example
static implicitBindingKey = "author"; // any :author param resolves via this model
Inherited from
Serialization
hidden
statichidden:string[] =[]
Defined in: orm/src/model/BaseModel.ts:746
Fields to exclude from toJSON() and therefore from any
JSON.stringify() output — API responses, cache serialisation, etc.
List camelCase property names. Nested models serialise independently
via their own toJSON(), so a parent's hidden list does not
propagate to relations.
Example
static hidden = ['password', 'rememberToken'];
Inherited from
visible
staticvisible:string[] =[]
Defined in: orm/src/model/BaseModel.ts:754
Allow-list for serialization. When set (non-empty), toJSON() includes
ONLY these keys (plus appends). Takes precedence over hidden.
Inherited from
appends
staticappends:string[] =[]
Defined in: orm/src/model/BaseModel.ts:768
Computed accessor names to include in toJSON() output. Each name should
resolve to a getter (or plain property) on the instance.
Example
class User extends BaseModel {
static appends = ['fullName'];
get fullName() { return `${this.first} ${this.last}`; }
}
Inherited from
makeHidden()
makeHidden(...
keys):this
Defined in: orm/src/model/BaseModel.ts:2077
Hide additional keys from toJSON() for this instance only.
Parameters
keys
...string[]
Returns
this
Inherited from
makeVisible()
makeVisible(...
keys):this
Defined in: orm/src/model/BaseModel.ts:2088
Reveal keys that are hidden by the class hidden list, for this instance only.
Parameters
keys
...string[]
Returns
this
Inherited from
append()
append(...
keys):this
Defined in: orm/src/model/BaseModel.ts:2099
Add computed accessor name(s) to this instance's toJSON() output.
Parameters
keys
...string[]
Returns
this
Inherited from
toJSON()
toJSON():
Record<string,unknown>
Defined in: orm/src/model/BaseModel.ts:2220
Called automatically by JSON.stringify — returns a plain object containing only user-facing data:
• All column values and loaded relation instances (enumerable own props) • Excludes internal ORM state: _original, _exists, _forcedDirty, zerotal* • Excludes any remaining getter guards (unloaded relation lazy-load traps)
Nested model instances (e.g. post.author) also have toJSON(), so JSON.stringify recurses correctly through the full object graph.
Returns
Record<string, unknown>
Inherited from
Soft deletes
softDeletes
staticsoftDeletes:boolean=false
Defined in: orm/src/model/BaseModel.ts:594
Whether this model uses soft deletes. Flipped to true by the SoftDeletes
mixin; when set, delete sets deleted_at and queries scope
WHERE deleted_at IS NULL.
Inherited from
Timestamps
timestamps
statictimestamps:boolean=true
Defined in: orm/src/model/BaseModel.ts:585
When true (default), created_at / updated_at are set automatically on
insert and updated_at is bumped on every update. Toggle off, or wrap a
write in withoutTimestamps, to suppress this.
Inherited from
createdAt?
optionalcreatedAt?:Date
Defined in: orm/src/model/BaseModel.ts:826
Creation timestamp, set on insert when timestamps is enabled.
Inherited from
updatedAt?
optionalupdatedAt?:Date
Defined in: orm/src/model/BaseModel.ts:833
Last-update timestamp, bumped on every save when timestamps is enabled.
Inherited from
withoutTimestamps()
staticwithoutTimestamps<R>(callback):Promise<R>
Defined in: orm/src/model/BaseModel.ts:897
Run a callback with automatic timestamp updates disabled for this model. Restores the previous setting afterwards (even on throw).
Type Parameters
R
R
Parameters
callback
() => R | Promise<R>
Returns
Promise<R>
Example
await User.withoutTimestamps(() => user.save());
Inherited from
touch()
touch():
Promise<Role>
Defined in: orm/src/model/BaseModel.ts:1887
Bump updated_at to now and persist. No-op when timestamps are disabled.
Returns
Promise<Role>