# BaseModel class · @imqueue/pg-sequelize

Source: https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.basemodel/
Published: 2026-08-01
Author: @imqueue maintainers (https://github.com/imqueue)
Package: @imqueue/pg-sequelize 4.2.0 — generated reference, not hand-written

The class every model in an `@imqueue` service extends.

**Signature:**

```typescript
export declare abstract class BaseModel<T> extends Model<BaseModel<T>> 
```
**Extends:** Model<[BaseModel](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.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


```typescript
@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)](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.basemodel.appendchild/) |  | Attaches related data to this instance as though it had been joined in. |
| [drop(options)](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.basemodel.drop/) | `static` | Drops this model's relation: `DROP VIEW` for a view, `DROP TABLE` otherwise. |
| [findAll(options)](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.basemodel.findall/) | `static` | Search for multiple instances. |
| [findByPk(identifier, options)](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.basemodel.findbypk/) | `static` | Finds one instance by primary key. |
| [findOne(options)](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.basemodel.findone/) | `static` | Finds the first instance matching the options. |
| [fixNumbers()](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.basemodel.fixnumbers/) |  | Casts this instance's numeric columns back to numbers. |
| [getViewDefinition(viewParams, asQuery)](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.basemodel.getviewdefinition/) | `static` | This view's SQL, with any dynamic parameters substituted in. |
| [restoreSerialization()](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.basemodel.restoreserialization/) |  | Forgets the `returning` column list, so serializing emits every loaded column again. |
| [sync(options)](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.basemodel.sync/) | `static` | Creates this model's table, or does nothing when the model is a view. |
| [syncIndex(column, options, position)](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.basemodel.syncindex/) | `static` | Builds and runs the statements that create one column index. |
| [syncIndices(\_options)](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.basemodel.syncindices/) | `static` | Creates every column index declared on this model. |
| [syncView(options)](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.basemodel.syncview/) | `static` | Creates this view, replacing whatever definition is in the database. |
| [toGraph(graph)](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.basemodel.tograph/) | `static` | Builds a graph of this model's associations, following them transitively. |
| [toJSON()](https://imqueue.org/api/pg-sequelize/latest/pg-sequelize.basemodel.tojson/) |  | Serializes this instance, honouring the `returning` column list. |

