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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | 132x 2320x 2320x 2320x 2320x 2320x 2320x 2320x 2320x 2320x 2320x 2320x 2320x 2320x 366x 1822x | import type { ModelDefaults } from '../connection/options.ts';
import type { DataTypeInput } from '../data-types/data-types.ts';
import type { IndexOptions } from '../ddl/ddl-generator.ts';
import { AttributeNormalizer } from './attribute-normalizer.ts';
import type {
AttributeDefinition,
AttributeOptions,
ModelAttributes,
} from './attribute-options.ts';
import { DefinitionNormalizer } from './definition-normalizer.ts';
export type {
AttributeDefinition,
AttributeOptions,
GeneratedColumn,
ModelAttributes,
ReferencedTable,
ReferenceOptions,
UniqueOptions,
} from './attribute-options.ts';
const attributeNormalizer = new AttributeNormalizer();
/**
* Singular and plural forms of a model name (Sequelize v6 `ModelNameOptions`).
*
* @example
* ```ts
* const name: ModelNameOptions = { singular: 'person', plural: 'people' };
* ```
*/
export type ModelNameOptions = Readonly<{
/** Singular model name. */
singular?: string;
/** Plural model name. */
plural?: string;
}>;
/**
* Table-level model options shared by `Model.init` and `sequelize.define`.
*
* Extends the model defaults (`paranoid`, `timestamps`, `schema`, ...).
*
* @example
* ```ts
* const opts: DefinitionOptions = {
* modelName: 'Invoice',
* tableName: 'invoices',
* timestamps: true,
* paranoid: true,
* version: true,
* indexes: [{ fields: ['number'], unique: true }],
* };
* ```
*/
export type DefinitionOptions = ModelDefaults &
Readonly<{
/** Model name. Defaults to the class name. */
modelName?: string;
/**
* Singular and plural model name (Sequelize v6 `ModelOptions.name`). Drives the default alias
* and foreign key of `belongsTo` / `hasOne` / `hasMany` / `belongsToMany` and the accessor names.
* A missing form falls back to the inflection of `modelName`.
*/
name?: ModelNameOptions;
/** Table name. Defaults to the pluralized model name. */
tableName?: string;
/** Separator between schema and table that `getTableName` reports (PostgreSQL always uses `.` in SQL). */
schemaDelimiter?: string;
/** Name of the created-at attribute, or `false` to disable it.
* @defaultValue 'createdAt' */
createdAt?: string | boolean;
/** Name of the updated-at attribute, or `false` to disable it.
* @defaultValue 'updatedAt' */
updatedAt?: string | boolean;
/** Name of the deleted-at attribute of paranoid models.
* @defaultValue 'deletedAt' */
deletedAt?: string | boolean;
/** Optimistic locking: `true` adds a `version` attribute, a string names it. */
version?: boolean | string;
/** Indexes created by `sync()`. */
indexes?: readonly IndexOptions[];
/** Table comment. */
comment?: string;
/**
* `CREATE UNLOGGED TABLE` on `sync`: faster writes, no WAL, emptied after a crash, not replicated. PostgreSQL extension.
* @postgrelize
*/
unlogged?: boolean;
/**
* `CREATE TEMPORARY TABLE` on `sync`: lives as long as the database session; cannot have a `schema`. See {@link QueryInterface.createTable}. PostgreSQL extension.
* @postgrelize
*/
temporary?: boolean;
}>;
/** Normalized fields of a {@link ModelDefinition}. */
export type ModelDefinitionFields = Readonly<{
/** Model name. */
name: string;
/** Table name. */
tableName: string;
/** Table schema. */
schema: string | undefined;
/** Normalized attributes by attribute name. */
attributes: ReadonlyMap<string, AttributeDefinition>;
/** Attribute name by column name. */
fieldToAttribute: ReadonlyMap<string, string>;
/** Primary key attribute names. */
primaryKeys: readonly string[];
/** Created-at attribute. */
createdAt: string | undefined;
/** Updated-at attribute. */
updatedAt: string | undefined;
/** Deleted-at attribute when the model is paranoid. */
deletedAt: string | undefined;
/** Version attribute when optimistic locking is enabled. */
version: string | undefined;
/** Unique keys from attribute `unique` options: key name → attribute names (in definition order). */
uniqueKeys: ReadonlyMap<string, readonly string[]>;
/** Model options after defaults were applied. */
options: DefinitionOptions;
}>;
/**
* Immutable normalized model definition: table, attributes, keys, timestamps and options of a
* model, built from `Model.init` arguments with Sequelize v6 defaults by {@link ModelDefinition.normalize}.
*/
export class ModelDefinition {
/** Model name. */
readonly name: string;
/** Table name. */
readonly tableName: string;
/** Table schema. */
readonly schema: string | undefined;
/** Normalized attributes by attribute name. */
readonly attributes: ReadonlyMap<string, AttributeDefinition>;
/** Attribute name by column name. */
readonly fieldToAttribute: ReadonlyMap<string, string>;
/** Primary key attribute names. */
readonly primaryKeys: readonly string[];
/** Created-at attribute. */
readonly createdAt: string | undefined;
/** Updated-at attribute. */
readonly updatedAt: string | undefined;
/** Deleted-at attribute when the model is paranoid. */
readonly deletedAt: string | undefined;
/** Version attribute when optimistic locking is enabled. */
readonly version: string | undefined;
/** Unique keys from attribute `unique` options: key name → attribute names (in definition order). */
readonly uniqueKeys: ReadonlyMap<string, readonly string[]>;
/** Model options after defaults were applied. */
readonly options: DefinitionOptions;
/**
* @param fields - Normalized fields; the instance is frozen.
*/
constructor(fields: ModelDefinitionFields) {
this.name = fields.name;
this.tableName = fields.tableName;
this.schema = fields.schema;
this.attributes = fields.attributes;
this.fieldToAttribute = fields.fieldToAttribute;
this.primaryKeys = fields.primaryKeys;
this.createdAt = fields.createdAt;
this.updatedAt = fields.updatedAt;
this.deletedAt = fields.deletedAt;
this.version = fields.version;
this.uniqueKeys = fields.uniqueKeys;
this.options = fields.options;
Object.freeze(this);
}
/**
* Normalizes one attribute: column name, data type instance, `allowNull` default.
*
* @param name - Attribute name.
* @param input - Data type or column options.
* @param underscored - Derive the column name in snake case.
* @returns Frozen attribute definition.
*/
static attribute(
name: string,
input: DataTypeInput | AttributeOptions,
underscored: boolean,
): AttributeDefinition {
return attributeNormalizer.attribute(name, input, underscored);
}
/**
* Normalizes `Model.init` arguments into an immutable definition, applying Sequelize v6 defaults.
*
* @param className - Class name (default model name).
* @param attributes - Declared attributes.
* @param options - Model options.
* @param defaults - `define` defaults of the Sequelize instance.
* @param associationAttributes - Attributes added by associations (foreign keys).
* @returns The definition.
*/
static normalize(
className: string,
attributes: ModelAttributes,
options: DefinitionOptions,
defaults: ModelDefaults = {},
associationAttributes: ModelAttributes = {},
): ModelDefinition {
return new ModelDefinition(
new DefinitionNormalizer(
className,
attributes,
options,
defaults,
associationAttributes,
).fields(),
);
}
}
|