$ open source · GPL-3.0 — need a commercial license? imqueue.com →

BaseModel class

The class every model in an @imqueue service extends.

Signature:

export declare abstract class BaseModel<T> extends Model<BaseModel<T>> 

Extends: Model<BaseModel<T>>

Remarks

Sequelize's own Model with four additions.

Views: a model declared with View or DynamicView is created and dropped as a view, skipped by the table sync, and has its numeric columns cast back to numbers after every finder, since a view returns them as strings.

Indices: ColumnIndex and NullableIndex declarations become CREATE INDEX statements at sync time, which plain sequelize cannot express.

Serialization: toJSON() honours the returning column list left behind by the last write, and picks up associated instances that the loaded attributes do not already cover.

Associations as a graph: toGraph() walks them transitively, so a cycle can be found before a query walks into it.

Example

@Table
export class Lead extends BaseModel<Lead> {
    @PrimaryKey
    @AutoIncrement
    @Column(DataType.BIGINT)
    public readonly id: number;

    @AllowNull(false)
    @Column(DataType.STRING)
    public name: string;

    @CreatedBy()
    @Column(DataType.BIGINT)
    public createdBy: number;
}

Methods

Method

Modifiers

Description

appendChild(name, data)

Attaches related data to this instance as though it had been joined in.

drop(options)

static

Drops this model's relation: DROP VIEW for a view, DROP TABLE otherwise.

findAll(options)

static

Search for multiple instances.

findByPk(identifier, options)

static

Finds one instance by primary key.

findOne(options)

static

Finds the first instance matching the options.

fixNumbers()

Casts this instance's numeric columns back to numbers.

getViewDefinition(viewParams, asQuery)

static

This view's SQL, with any dynamic parameters substituted in.

restoreSerialization()

Forgets the returning column list, so serializing emits every loaded column again.

sync(options)

static

Creates this model's table, or does nothing when the model is a view.

syncIndex(column, options, position)

static

Builds and runs the statements that create one column index.

syncIndices(_options)

static

Creates every column index declared on this model.

syncView(options)

static

Creates this view, replacing whatever definition is in the database.

toGraph(graph)

static

Builds a graph of this model's associations, following them transitively.

toJSON()

Serializes this instance, honouring the returning column list.