Skip to content

Interface: LucidModel ​

Shape of the model static properties. The $ prefix is to denote special properties from the base model.

@note: Since the interface name appears next to the inherited model methods, we have to choose a succinct name

Extended by ​

Constructors ​

Constructor ​

ts
new LucidModel(): LucidRow;

Returns ​

LucidRow

Properties ​

PropertyModifierTypeDescription
$adapterpublicAdapterContractAdapter to work as a bridge between query builder and the model
$columnsDefinitionspublicMap<string, ModelColumnOptions>A map of defined columns
$computedDefinitionspublicMap<string, ComputedOptions>A map of computed properties
$hookspublicHooks<any>Reference to hooks
$keyspublic{ attributesToColumns: ModelKeysContract; attributesToSerialized: ModelKeysContract; columnsToAttributes: ModelKeysContract; columnsToSerialized: ModelKeysContract; serializedToAttributes: ModelKeysContract; serializedToColumns: ModelKeysContract; }A copy of internal keys mapping. One should be able to resolve between all key versions
$keys.attributesToColumnspublicModelKeysContract-
$keys.attributesToSerializedpublicModelKeysContract-
$keys.columnsToAttributespublicModelKeysContract-
$keys.columnsToSerializedpublicModelKeysContract-
$keys.serializedToAttributespublicModelKeysContract-
$keys.serializedToColumnspublicModelKeysContract-
$relationsDefinitionspublicMap<string, RelationshipsContract>A map of defined relationships
bootedreadonlybooleanWhether or not model has been booted. After this model configurations are ignored
connection?publicstringCustom database connection to use
namingStrategypublicNamingStrategyContractNaming strategy to use
primaryKeypublicstringThe primary key for finding unique referencing to a model
selfAssignPrimaryKeypublicbooleanSelf assign the primary instead of relying on the database to return it back
tablepublicstringDatabase table to use
transactionpublicTransactionFnReturns transaction client from the model. It is same as calling "db.transaction"

Methods ​

$addColumn() ​

ts
$addColumn(name: string, options: Partial<ColumnOptions>): ColumnOptions;

Managing columns

Parameters ​

ParameterType
namestring
optionsPartial<ColumnOptions>

Returns ​

ColumnOptions


$addComputed() ​

ts
$addComputed(name: string, options: Partial<ComputedOptions>): ComputedOptions;

Managing computed columns

Parameters ​

ParameterType
namestring
optionsPartial<ComputedOptions>

Returns ​

ComputedOptions


$addRelation() ​

ts
$addRelation(
   name: string,
   type: "hasOne" | "hasMany" | "belongsTo" | "manyToMany" | "hasManyThrough",
   relatedModel: () => LucidModel,
   options: ModelRelationOptions): void;

Managing relationships

Parameters ​

ParameterType
namestring
type"hasOne" | "hasMany" | "belongsTo" | "manyToMany" | "hasManyThrough"
relatedModel() => LucidModel
optionsModelRelationOptions

Returns ​

void


$createFromAdapterResult() ​

ts
$createFromAdapterResult<T>(
   this: T,
   result?: ModelObject,
   sideloadAttributes?: ModelObject,
   options?: ModelAdapterOptions):
  | InstanceType<T>
  | null;

Creating model from adapter results

Type Parameters ​

Type Parameter
T extends LucidModel

Parameters ​

ParameterType
thisT
result?ModelObject
sideloadAttributes?ModelObject
options?ModelAdapterOptions

Returns ​

| InstanceType<T> | null


$createMultipleFromAdapterResult() ​

ts
$createMultipleFromAdapterResult<T>(
   this: T,
   results: ModelObject[],
   sideloadAttributes?: ModelObject,
   options?: ModelAdapterOptions): InstanceType<T>[];

Creating multiple model instances from an array of adapter result

Type Parameters ​

Type Parameter
T extends LucidModel

Parameters ​

ParameterType
thisT
resultsModelObject[]
sideloadAttributes?ModelObject
options?ModelAdapterOptions

Returns ​

InstanceType<T>[]


$defineProperty() ​

ts
$defineProperty<Model, Prop>(
   this: Model,
   propertyName: Prop,
   defaultValue: Model[Prop],
   strategy: "inherit" | "define" | (value: Model[Prop]) => Model[Prop]): void;

Define a static property on the model using the inherit or define strategy.

Inherit strategy will clone the property from the parent model and will set it on the current model

Type Parameters ​

Type Parameter
Model extends LucidModel
Prop extends string | number | symbol

Parameters ​

ParameterType
thisModel
propertyNameProp
defaultValueModel[Prop]
strategy"inherit" | "define" | (value: Model[Prop]) => Model[Prop]

Returns ​

void


$getColumn() ​

ts
$getColumn(name: string):
  | ModelColumnOptions
  | undefined;

Parameters ​

ParameterType
namestring

Returns ​

| ModelColumnOptions | undefined


$getComputed() ​

ts
$getComputed(name: string): ComputedOptions | undefined;

Parameters ​

ParameterType
namestring

Returns ​

ComputedOptions | undefined


$getRelation() ​

Call Signature ​

ts
$getRelation<Model, Name>(this: Model, name: Name): NonNullable<InstanceType<Model>[Name]> extends ModelRelations<LucidModel, LucidModel> ? NonNullable<NonNullable<InstanceType<Model>[Name]>>["client"]["relation"] : RelationshipsContract;

Get relationship declaration

Type Parameters ​
Type Parameter
Model extends LucidModel
Name extends string | number | symbol
Parameters ​
ParameterType
thisModel
nameName
Returns ​

NonNullable<InstanceType<Model>[Name]> extends ModelRelations<LucidModel, LucidModel> ? NonNullable<NonNullable<InstanceType<Model>[Name]>>["client"]["relation"] : RelationshipsContract

Call Signature ​

ts
$getRelation<Model>(this: Model, name: string): RelationshipsContract;
Type Parameters ​
Type Parameter
Model extends LucidModel
Parameters ​
ParameterType
thisModel
namestring
Returns ​

RelationshipsContract


$hasColumn() ​

ts
$hasColumn(name: string): boolean;

Parameters ​

ParameterType
namestring

Returns ​

boolean


$hasComputed() ​

ts
$hasComputed(name: string): boolean;

Parameters ​

ParameterType
namestring

Returns ​

boolean


$hasRelation() ​

ts
$hasRelation(name: string): boolean;

Find if a relationship exists

Parameters ​

ParameterType
namestring

Returns ​

boolean


after() ​

Call Signature ​

ts
after<Model>(
   this: Model,
   event: "fetch",
   handler: HooksHandler<InstanceType<Model>[], "fetch">): void;

Register an after hook

Type Parameters ​
Type Parameter
Model extends LucidModel
Parameters ​
ParameterType
thisModel
event"fetch"
handlerHooksHandler<InstanceType<Model>[], "fetch">
Returns ​

void

Call Signature ​

ts
after<Model>(
   this: Model,
   event: "paginate",
   handler: HooksHandler<ModelPaginatorContract<InstanceType<Model>>, "paginate">): void;
Type Parameters ​
Type Parameter
Model extends LucidModel
Parameters ​
ParameterType
thisModel
event"paginate"
handlerHooksHandler<ModelPaginatorContract<InstanceType<Model>>, "paginate">
Returns ​

void

Call Signature ​

ts
after<Model, Event>(
   this: Model,
   event: Event,
   handler: HooksHandler<InstanceType<Model>, Event>): void;
Type Parameters ​
Type Parameter
Model extends LucidModel
Event extends EventsList
Parameters ​
ParameterType
thisModel
eventEvent
handlerHooksHandler<InstanceType<Model>, Event>
Returns ​

void


all() ​

ts
all<T>(this: T, options?: ModelAdapterOptions): Promise<InstanceType<T>[]>;

Fetch all rows

Type Parameters ​

Type Parameter
T extends LucidModel

Parameters ​

ParameterType
thisT
options?ModelAdapterOptions

Returns ​

Promise<InstanceType<T>[]>


before() ​

Call Signature ​

ts
before<Model, Event>(
   this: Model,
   event: Event,
   handler: HooksHandler<ModelQueryBuilderContract<Model, InstanceType<Model>>, Event>): void;

Register a before hook

Type Parameters ​
Type Parameter
Model extends LucidModel
Event extends "find" | "fetch"
Parameters ​
ParameterType
thisModel
eventEvent
handlerHooksHandler<ModelQueryBuilderContract<Model, InstanceType<Model>>, Event>
Returns ​

void

Call Signature ​

ts
before<Model>(
   this: Model,
   event: "paginate",
   handler: HooksHandler<[ModelQueryBuilderContract<Model, InstanceType<Model>>, ModelQueryBuilderContract<Model, InstanceType<Model>>], "paginate">): void;
Type Parameters ​
Type Parameter
Model extends LucidModel
Parameters ​
ParameterType
thisModel
event"paginate"
handlerHooksHandler<[ModelQueryBuilderContract<Model, InstanceType<Model>>, ModelQueryBuilderContract<Model, InstanceType<Model>>], "paginate">
Returns ​

void

Call Signature ​

ts
before<Model, Event>(
   this: Model,
   event: Event,
   handler: HooksHandler<InstanceType<Model>, Event>): void;
Type Parameters ​
Type Parameter
Model extends LucidModel
Event extends EventsList
Parameters ​
ParameterType
thisModel
eventEvent
handlerHooksHandler<InstanceType<Model>, Event>
Returns ​

void


boot() ​

ts
boot(): void;

Boot model

Returns ​

void


create() ​

ts
create<T>(
   this: T,
   values: Partial<ModelAttributes<InstanceType<T>>>,
options?: ModelAssignOptions): Promise<InstanceType<T>>;

Create model and return its instance back

Type Parameters ​

Type Parameter
T extends LucidModel

Parameters ​

ParameterType
thisT
valuesPartial<ModelAttributes<InstanceType<T>>>
options?ModelAssignOptions

Returns ​

Promise<InstanceType<T>>


createMany() ​

ts
createMany<T>(
   this: T,
   values: Partial<ModelAttributes<InstanceType<T>>>[],
options?: ModelAssignOptions): Promise<InstanceType<T>[]>;

Create many of model instances

Type Parameters ​

Type Parameter
T extends LucidModel

Parameters ​

ParameterType
thisT
valuesPartial<ModelAttributes<InstanceType<T>>>[]
options?ModelAssignOptions

Returns ​

Promise<InstanceType<T>[]>


createManyQuietly() ​

ts
createManyQuietly<T>(
   this: T,
   values: Partial<ModelAttributes<InstanceType<T>>>[],
options?: ModelAssignOptions): Promise<InstanceType<T>[]>;

Same as [[BaseModel.createMany]] but without invoking hooks

Type Parameters ​

Type Parameter
T extends LucidModel

Parameters ​

ParameterType
thisT
valuesPartial<ModelAttributes<InstanceType<T>>>[]
options?ModelAssignOptions

Returns ​

Promise<InstanceType<T>[]>


createQuietly() ​

ts
createQuietly<T>(
   this: T,
   values: Partial<ModelAttributes<InstanceType<T>>>,
options?: ModelAssignOptions): Promise<InstanceType<T>>;

Same as [[BaseModel.create]] but without invoking hooks

Type Parameters ​

Type Parameter
T extends LucidModel

Parameters ​

ParameterType
thisT
valuesPartial<ModelAttributes<InstanceType<T>>>
options?ModelAssignOptions

Returns ​

Promise<InstanceType<T>>


fetchOrCreateMany() ​

ts
fetchOrCreateMany<T>(
   this: T,
   predicate:
  | keyof ModelAttributes<InstanceType<T>>
  | keyof ModelAttributes<InstanceType<T>>[],
   payload: Partial<ModelAttributes<InstanceType<T>>>[],
options?: ModelAssignOptions): Promise<InstanceType<T>[]>;

Find rows or create many when missing. One db call is invoked for each create

Type Parameters ​

Type Parameter
T extends LucidModel

Parameters ​

ParameterType
thisT
predicate| keyof ModelAttributes<InstanceType<T>> | keyof ModelAttributes<InstanceType<T>>[]
payloadPartial<ModelAttributes<InstanceType<T>>>[]
options?ModelAssignOptions

Returns ​

Promise<InstanceType<T>[]>


fetchOrNewUpMany() ​

ts
fetchOrNewUpMany<T>(
   this: T,
   predicate:
  | keyof ModelAttributes<InstanceType<T>>
  | keyof ModelAttributes<InstanceType<T>>[],
   payload: Partial<ModelAttributes<InstanceType<T>>>[],
options?: ModelAssignOptions): Promise<InstanceType<T>[]>;

Find rows or create in-memory instances of the missing one's.

Type Parameters ​

Type Parameter
T extends LucidModel

Parameters ​

ParameterType
thisT
predicate| keyof ModelAttributes<InstanceType<T>> | keyof ModelAttributes<InstanceType<T>>[]
payloadPartial<ModelAttributes<InstanceType<T>>>[]
options?ModelAssignOptions

Returns ​

Promise<InstanceType<T>[]>


find() ​

ts
find<T>(
   this: T,
   value: any,
   options?: ModelAdapterOptions): Promise<
  | InstanceType<T>
| null>;

Find one using the primary key

Type Parameters ​

Type Parameter
T extends LucidModel

Parameters ​

ParameterType
thisT
valueany
options?ModelAdapterOptions

Returns ​

Promise< | InstanceType<T> | null>


findBy() ​

Call Signature ​

ts
findBy<T>(
   this: T,
   clause: Record<string, unknown>,
   options?: ModelAdapterOptions): Promise<
  | InstanceType<T>
| null>;

Find one using a clause

Type Parameters ​
Type Parameter
T extends LucidModel
Parameters ​
ParameterType
thisT
clauseRecord<string, unknown>
options?ModelAdapterOptions
Returns ​

Promise< | InstanceType<T> | null>

Call Signature ​

ts
findBy<T>(
   this: T,
   key: string,
   value: any,
   options?: ModelAdapterOptions): Promise<
  | InstanceType<T>
| null>;

Find one using a key-value pair

Type Parameters ​
Type Parameter
T extends LucidModel
Parameters ​
ParameterType
thisT
keystring
valueany
options?ModelAdapterOptions
Returns ​

Promise< | InstanceType<T> | null>


findByOrFail() ​

Call Signature ​

ts
findByOrFail<T>(
   this: T,
   clause: Record<string, unknown>,
options?: ModelAdapterOptions): Promise<InstanceType<T>>;

Find one using a clause or fail

Type Parameters ​
Type Parameter
T extends LucidModel
Parameters ​
ParameterType
thisT
clauseRecord<string, unknown>
options?ModelAdapterOptions
Returns ​

Promise<InstanceType<T>>

Call Signature ​

ts
findByOrFail<T>(
   this: T,
   key: string,
   value: any,
options?: ModelAdapterOptions): Promise<InstanceType<T>>;

Find one using a key-value pair or fail

Type Parameters ​
Type Parameter
T extends LucidModel
Parameters ​
ParameterType
thisT
keystring
valueany
options?ModelAdapterOptions
Returns ​

Promise<InstanceType<T>>


findMany() ​

ts
findMany<T>(
   this: T,
   value: any[],
options?: ModelAdapterOptions): Promise<InstanceType<T>[]>;

Find many using an array of primary keys

Type Parameters ​

Type Parameter
T extends LucidModel

Parameters ​

ParameterType
thisT
valueany[]
options?ModelAdapterOptions

Returns ​

Promise<InstanceType<T>[]>


findManyBy() ​

Call Signature ​

ts
findManyBy<T>(
   this: T,
   clause: Record<string, unknown>,
options?: ModelAdapterOptions): Promise<InstanceType<T>[]>;

Find multiple models instance using a clause

Type Parameters ​
Type Parameter
T extends LucidModel
Parameters ​
ParameterType
thisT
clauseRecord<string, unknown>
options?ModelAdapterOptions
Returns ​

Promise<InstanceType<T>[]>

Call Signature ​

ts
findManyBy<T>(
   this: T,
   key: string,
   value: any,
options?: ModelAdapterOptions): Promise<InstanceType<T>[]>;

Find multiple models instance using a key/value pair

Type Parameters ​
Type Parameter
T extends LucidModel
Parameters ​
ParameterType
thisT
keystring
valueany
options?ModelAdapterOptions
Returns ​

Promise<InstanceType<T>[]>


findOrFail() ​

ts
findOrFail<T>(
   this: T,
   value: any,
options?: ModelAdapterOptions): Promise<InstanceType<T>>;

Find one using the primary key or fail

Type Parameters ​

Type Parameter
T extends LucidModel

Parameters ​

ParameterType
thisT
valueany
options?ModelAdapterOptions

Returns ​

Promise<InstanceType<T>>


first() ​

ts
first<T>(this: T, options?: ModelAdapterOptions): Promise<
  | InstanceType<T>
| null>;

Same as query().first()

Type Parameters ​

Type Parameter
T extends LucidModel

Parameters ​

ParameterType
thisT
options?ModelAdapterOptions

Returns ​

Promise< | InstanceType<T> | null>


firstOrCreate() ​

ts
firstOrCreate<T>(
   this: T,
   searchPayload: Partial<ModelAttributes<InstanceType<T>>>,
   savePayload?: Partial<ModelAttributes<InstanceType<T>>>,
options?: ModelAssignOptions): Promise<InstanceType<T>>;

Returns the first row or save it to the database

Type Parameters ​

Type Parameter
T extends LucidModel

Parameters ​

ParameterType
thisT
searchPayloadPartial<ModelAttributes<InstanceType<T>>>
savePayload?Partial<ModelAttributes<InstanceType<T>>>
options?ModelAssignOptions

Returns ​

Promise<InstanceType<T>>


firstOrFail() ​

ts
firstOrFail<T>(this: T, options?: ModelAdapterOptions): Promise<InstanceType<T>>;

Same as query().firstOrFail()

Type Parameters ​

Type Parameter
T extends LucidModel

Parameters ​

ParameterType
thisT
options?ModelAdapterOptions

Returns ​

Promise<InstanceType<T>>


firstOrNew() ​

ts
firstOrNew<T>(
   this: T,
   searchPayload: Partial<ModelAttributes<InstanceType<T>>>,
   savePayload?: Partial<ModelAttributes<InstanceType<T>>>,
options?: ModelAssignOptions): Promise<InstanceType<T>>;

Returns the first row or create a new instance of model without persisting it

Type Parameters ​

Type Parameter
T extends LucidModel

Parameters ​

ParameterType
thisT
searchPayloadPartial<ModelAttributes<InstanceType<T>>>
savePayload?Partial<ModelAttributes<InstanceType<T>>>
options?ModelAssignOptions

Returns ​

Promise<InstanceType<T>>


query() ​

ts
query<Model, Result>(this: Model, options?: ModelAdapterOptions): ModelQueryBuilderContract<Model, Result>;

Returns the query for fetching a model instance

Type Parameters ​

Type ParameterDefault type
Model extends LucidModel-
ResultInstanceType<Model>

Parameters ​

ParameterType
thisModel
options?ModelAdapterOptions

Returns ​

ModelQueryBuilderContract<Model, Result>


truncate() ​

ts
truncate(cascade?: boolean): Promise<void>;

Truncate model table

Parameters ​

ParameterType
cascade?boolean

Returns ​

Promise<void>


updateOrCreate() ​

ts
updateOrCreate<T>(
   this: T,
   searchPayload: Partial<ModelAttributes<InstanceType<T>>>,
   updatePayload: Partial<ModelAttributes<InstanceType<T>>>,
options?: ModelAssignOptions): Promise<InstanceType<T>>;

Returns the first row or save it to the database

Type Parameters ​

Type Parameter
T extends LucidModel

Parameters ​

ParameterType
thisT
searchPayloadPartial<ModelAttributes<InstanceType<T>>>
updatePayloadPartial<ModelAttributes<InstanceType<T>>>
options?ModelAssignOptions

Returns ​

Promise<InstanceType<T>>


updateOrCreateMany() ​

ts
updateOrCreateMany<T>(
   this: T,
   predicate:
  | keyof ModelAttributes<InstanceType<T>>
  | keyof ModelAttributes<InstanceType<T>>[],
   payload: Partial<ModelAttributes<InstanceType<T>>>[],
options?: ModelAssignOptions): Promise<InstanceType<T>[]>;

Update existing rows or create new one's.

Type Parameters ​

Type Parameter
T extends LucidModel

Parameters ​

ParameterType
thisT
predicate| keyof ModelAttributes<InstanceType<T>> | keyof ModelAttributes<InstanceType<T>>[]
payloadPartial<ModelAttributes<InstanceType<T>>>[]
options?ModelAssignOptions

Returns ​

Promise<InstanceType<T>[]>


useAdapter() ​

ts
useAdapter(adapter: AdapterContract): void;

Define an adapter to use for interacting with the database

Parameters ​

ParameterType
adapterAdapterContract

Returns ​

void