Abstract
Protected
Readonly
busProtected
Optional
dirtyDatabase fields that should be run on the next save, even if the fields are not mapped to members on the model.
Protected
Readonly
loggingProtected
Optional
originalThe original row fetched from the database.
Cache of relation instances by property accessor.
This is used by the @Relation()
decorator to cache Relation instances.
Protected
removedProtected
scopesProtected
subscribersLocal listeners subscribed to events on this bus.
Protected
subscriptionsReadonly
uuidProtected
withto eager-load the subtree by default
Static
Protected
Readonly
CREATED_Optionally, the timestamp field set on creation.
Static
Protected
Readonly
UPDATED_Optionally, the timestamp field set op update.
Static
Protected
appendsArray of additional fields on the class that should be included in the object serializations.
Static
Protected
connectionThe name of the connection this model should run through.
Static
Protected
keyThe name of the column that uniquely identifies this model.
Static
Readonly
leftThe table column where the left tree number is stored.
Static
Protected
masksArray of fields on the class that should be excluded from the object serializations.
Static
Readonly
parentStatic
Protected
populateIf false (default), the primary key will be excluded from INSERTs.
Static
Readonly
rightThe table column where the right tree number is stored.
Static
Protected
tableThe name of the table this model is stored in.
Static
Protected
timestampsIf true, the CREATED_AT and UPDATED_AT columns will be automatically set.
Private
appThe global application instance.
Protected
isProtected
Get a wrapped function that compares whether the given model field on the current instance differs from the originally fetched value.
Used to filter for dirty fields.
Protected
Get a wrapped function that compares whether the given model field on the current instance differs from the originally fetched value.
Used to filter for dirty fields.
Protected
appGet the global Application.
Append the other
node as a child of this node.
Returns the common ancestor from which point the tree was renumbered.
You should save the subtree after this.
await nodeA.appendChild(nodeB).saveSubtree()
if provided, other
will be inserted before the before
child of this node. Otherwise, it will be added as the last child.
Apply the default scopes to this model to the given query builder.
Similar to assumeFromSource, but instead of mapping database fields to model
properties, this function assumes the object
contains a mapping of model properties
to the values of those properties.
Only properties with @Field()
annotations will be set.
Given a row from the database, set the properties on this model that correspond to fields on that database.
The row
maps database fields to values, and the values are set for the properties
that they correspond to based on the model's @Field()
annotations.
Create the inverse of a one-to-many relation. Should be called from a method on the model:
class MyModel extends Model<MyModel> {
@Related()
public otherModels() {
return this.hasMany(MyOtherModel)
}
}
class MyOtherModel extends Model<MyOtherModel> {
@Related()
public myModels() {
return this.belongsToMany(MyModel, 'otherModels')
}
}
Create the inverse of a one-to-one relation. Should be called from a method on the model:
class MyModel extends Model<MyModel> {
@Related()
public otherModel() {
return this.hasOne(MyOtherModel)
}
}
class MyOtherModel extends Model<MyOtherModel> {
@Related()
public myModel() {
return this.belongsToOne(MyModel, 'otherModel')
}
}
Initialize the model's properties from the given values and do any other initial setup.
values
can optionally be an object mapping model properties to the values of those
properties. Only properties with @Field()
annotations will be set.
Optional
values: { Protected
callGet the immediate children of this model.
Protected
containerGet a flat collection of this node & its subtree nodes, in pre-order.
Private
flattenRecursive helper for flatten()
.
Get the method with the given name from this class, bound to this class.
function
Get the method with the given name from this class, bound to this class.
function
Rest
...args: any[]Protected
getProtected
getProtected
Get the relation instance returned by a method on this model.
Create a new one-to-one relation instance. Should be called from a method on the model:
class MyModel extends Model<MyModel> {
@Related()
public otherModels() {
return this.hasMany(MyOtherModel)
}
}
Optional
foreignKeyOverride: keyof T & stringOptional
localKeyOverride: keyof T2 & stringCreate a new one-to-one relation instance. Should be called from a method on the model:
class MyModel extends Model<MyModel> {
@Related()
public otherModel() {
return this.hasOne(MyOtherModel)
}
}
Optional
foreignKeyOverride: keyof T & stringOptional
localKeyOverride: keyof T2 & stringProtected
hasProtected
Returns true if the current model has a scope with the given identifier.
Protected
initializeProtected
makeProtected
namedProtected
Register a scope on the model with a specific name.
Return an object of only the given properties on this model.
Assume a
is an instance of some model A
with the given fields.
const a = new A({ field1: 'field1 value', field2: 'field2 value', id: 123 })
a.only('field1', 'id) // => {field1: 'field1 value', id: 123}
Rest
...fields: string[]The parent node of this model, if one exists.
Register a pipeline as an event handler.
Populates an instance of the model with the same database fields that are set on this model, with the exclusion of the primary key.
Useful for inserting copies of records.
Assume a record, a
, is an instance of some model A
with the given fields.
const a = A.find(123) // => A{id: 123, name: 'some_name', other_field: 'a value'}
const b = a.populate(new A) // => A{name: 'some_name', other_field: 'a value'}
to include the tree fields
Remove this node from the tree structure. If the node existed in the tree structure and was removed, this method returns the tree node that was renumbered. You should save the subtree after this.
await nodeA.removeFromTree().saveSubtree()
Persist the model into the database. If the model already exists, perform an update on its fields. Otherwise, insert a new row with its fields.
Passing the withoutTimestamps
will prevent the configured CREATED_AT/UPDATED_AT
timestamps from being updated.
Protected
setProtected
shouldSubscribe to an event on the bus.
The subtree nodes of this model, recursively nested.
Protected
withProtected
Register a scope on the model.
Static
connectionStatic
findStatic
getStatic
getGet the database connection instance for this model's connection.
Static
getStatic
propertyStatic
qualifyStatic
qualifyStatic
queryto include the tree fields
Static
queryGet the QuerySource object for this model as it should be applied to query builders.
This sets the alias for the model table equal to the table name itself, so it can be referenced explicitly in queries if necessary.
Static
setStatic
table
Model implementation with helpers for querying tree-structured data.
This works by using a modified pre-order traversal to number the tree nodes with a left- and right-side numbers. For example:
These numbers are stored, by default, in
left_num
andright_num
columns. Thesubtree()
method returns aHasSubtree
relation which loads the subtree of a model and recursively nests the nodes.You can use the
children()
helper method to get a collection of the immediate children of this node, which also have the subtree set.To query the model without loading the entire subtree, use the
without()
method on theModelBuilder
. For example: