Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | 139583x 139583x 16x 16x 1x 31x 17x 29x 1x 1x 16x 16x 1x 1292x 1292x 3x 9x 9x 9x 3x | import type { Sequelize } from '../sequelize.ts';
import { SqlCompiler } from '../sql/compile.ts';
import { InstanceState } from './instance-state.ts';
import type {
AttributeDefinition,
DefinitionOptions,
ModelAttributes,
} from './model-definition.ts';
import { lifecycle, registry } from './model-services.ts';
import type {
AnyModel,
BuildOptions,
InitOptions,
ModelStatic,
QualifiedTableName,
Values,
} from './options.ts';
/**
* Core of every model: instance state, model metadata and `init`. Capabilities are added by the
* model mixins; `Model` is the public class.
*
* @internal
*/
export class ModelBase {
/**
* @param values - Attribute values.
* @param options - Build options.
*/
constructor(values: Values = {}, options: BuildOptions = {}) {
InstanceState.attach(this);
lifecycle.initialize(this as unknown as AnyModel, values, options);
}
/**
* For instances returned by `insert`, `create` / `bulkCreate` / `upsert` with `onConflict`:
* true when the row was inserted, false when an existing row was updated (or nothing was
* written). `undefined` otherwise. PostgreSQL extension.
* @postgrelize
*/
get wasInserted(): boolean | undefined {
return InstanceState.of(this).wasInserted;
}
/**
* For instances returned with `returningOld: true`: attribute values of the row before the
* statement, or `null` when the row did not exist (inserted). `undefined` otherwise. PostgreSQL 18+.
* @postgrelize
*/
get oldValues(): Readonly<Record<string, unknown>> | null | undefined {
return InstanceState.of(this).oldValues;
}
/** `Sequelize` instance the model belongs to. */
get sequelize(): Sequelize {
return (this.constructor as typeof ModelBase).sequelize;
}
/** Name the model was registered under, as given by `modelName` in the init options (default: the class name). */
static get modelName(): string {
return registry.state(this).definition.name;
}
/** Name of the database table backing the model (pluralized model name unless `tableName` was set). */
static get tableName(): string {
return registry.state(this).definition.tableName;
}
/** Normalized attributes keyed by attribute name. */
static get rawAttributes(): Readonly<Record<string, AttributeDefinition>> {
return Object.fromEntries(registry.state(this).definition.attributes);
}
/** First primary key attribute name. */
static get primaryKeyAttribute(): string {
// typed `string` like v6; a model without a primary key yields `undefined` at runtime, as in v6
return registry.state(this).definition.primaryKeys[0] as string;
}
/** All primary key attribute names. */
static get primaryKeyAttributes(): readonly string[] {
return registry.state(this).definition.primaryKeys;
}
/** Model options after defaults were applied. */
static get options(): DefinitionOptions {
return registry.state(this).definition.options;
}
/** `Sequelize` instance the model belongs to. */
static get sequelize(): Sequelize {
return (
(registry.sequelizeOf(this) as unknown as Sequelize | undefined) ??
(() => {
throw new Error('Model is not initialized.');
})()
);
}
/**
* Defines attributes and options of the model and registers it in `sequelize.models`. Does not
* touch the database; call {@link Model.sync} or run migrations to create the table.
*
* @param this - The model class being initialized.
* @param attributes - Attribute definitions.
* @param options - Model options, including the `sequelize` instance.
* @returns The model class.
* @example
* ```ts
* class Project extends Model {}
* Project.init(
* { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, name: DataTypes.STRING },
* { sequelize, modelName: 'Project', paranoid: true },
* );
* ```
*/
static init<MS extends ModelStatic<AnyModel>, M extends InstanceType<MS> = InstanceType<MS>>(
this: MS,
attributes: ModelAttributes<M>,
options: InitOptions,
): MS {
registry.init(this, attributes, options);
return this;
}
/**
* Returns the normalized attribute definitions, same as {@link Model.rawAttributes}.
*
* @returns Attribute definitions.
* @example
* ```ts
* const attributes = User.getAttributes();
* Object.keys(attributes);
* ```
*/
static getAttributes(): Readonly<Record<string, AttributeDefinition>> {
return this.rawAttributes;
}
/**
* Table name, or the qualified table (`{ tableName, schema, delimiter, ... }`, like v6) when the
* model has a schema. `String(qualified)` is the quoted `"schema"."table"` reference.
*
* @returns Table name or qualified table.
* @example
* ```ts
* const table = User.getTableName();
* ```
*/
static getTableName(): string | QualifiedTableName {
const { definition } = registry.state(this);
const { tableName, schema } = definition;
return schema === undefined
? tableName
: {
tableName,
table: tableName,
name: definition.name,
schema,
delimiter: definition.options.schemaDelimiter ?? '.',
toString: (): string =>
`${SqlCompiler.quoteIdent(schema)}.${SqlCompiler.quoteIdent(tableName)}`,
};
}
}
|