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 | import type { DataType, DataTypeInput } from '../data-types/data-types.ts';
import type { DeferrableValue } from '../ddl/ddl-generator.ts';
import type { AttributeValidateOptions } from '../validation/types.ts';
import type { AnyModel } from './options.ts';
/** Definition of a generated column. */
export type GeneratedColumn = Readonly<{
/** Expression computing the value, e.g. `literal('"price" * "qty"')`. */
as: unknown;
/** `true` for `STORED`, `false` for `VIRTUAL` (PostgreSQL 18+ only).
* @defaultValue true */
stored?: boolean;
}>;
/**
* Named unique constraint of an attribute.
*
* Give several attributes the same `name` to form one composite unique key.
*
* @example
* ```ts
* const email: UniqueOptions = { name: 'users_email_unique', msg: 'Email already taken', nullsNotDistinct: true };
* ```
*/
export type UniqueOptions = Readonly<{
/** Constraint name; attributes with the same name form a composite unique key. */
name?: string;
/** Validation message used when the constraint is violated. */
msg?: string;
/**
* `UNIQUE NULLS NOT DISTINCT`: treat NULLs as equal. PostgreSQL extension.
* @postgrelize
*/
nullsNotDistinct?: boolean;
}>;
/** Table referenced by a foreign key. */
export type ReferencedTable = Readonly<{
/** Referenced table name. */
tableName: string;
/** Referenced table schema. */
schema?: string;
}>;
/**
* Foreign key target of an attribute.
*
* @example
* ```ts
* const ref: ReferenceOptions = { model: 'Users', key: 'id', deferrable: Deferrable.INITIALLY_DEFERRED };
* ```
*/
export type ReferenceOptions = Readonly<{
/** Referenced table name, an object with the table name and schema, or the referenced model class (its table name is read when the DDL is built). */
model: string | ReferencedTable | ReferencedModel;
/** Referenced column. Default: primary key. */
key?: string;
/** Deferrable mode of the foreign key constraint. */
deferrable?: DeferrableValue;
}>;
/** Model class used as the target of a foreign key; only its table name is read, when the DDL is built. */
export type ReferencedModel = Readonly<{
/**
* Table the model maps to.
*
* @returns The table name, or the name with its schema.
*/
getTableName(): string | ReferencedTable;
}>;
/**
* Column definition of a model attribute (Sequelize v6 `ModelAttributeColumnOptions`).
*
* `sync()` turns these into `CREATE TABLE` column definitions; validators run before INSERT / UPDATE.
*
* @see {@link Model.init}
*
* @example
* ```ts
* const attr: AttributeOptions = {
* type: DataTypes.STRING,
* allowNull: false,
* unique: true,
* validate: { isEmail: true },
* };
* ```
*/
export type AttributeOptions<M extends AnyModel = AnyModel> = Readonly<{
/** Column type; a `DataTypes` entry or its factory call. */
type: DataTypeInput;
/** Whether `NULL` is allowed.
* @defaultValue true */
allowNull?: boolean;
/** Default value: a literal value, an expression, `DataTypes.NOW` / `DataTypes.UUIDV4`, or a function called once per new row (client-side only, never part of the DDL). */
defaultValue?: unknown;
/** Part of the primary key. Without any primary key, an `id` attribute is added.
* @defaultValue false */
primaryKey?: boolean;
/** Generated by a sequence (`SERIAL`, or an identity column with `identity`).
* @defaultValue false */
autoIncrement?: boolean;
/**
* `GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY` instead of `SERIAL`. PostgreSQL extension.
* @postgrelize
*/
identity?: 'always' | 'byDefault';
/**
* Generated column `GENERATED ALWAYS AS (expression) STORED`; never written by INSERT/UPDATE. PostgreSQL extension.
* @postgrelize
*/
generated?: GeneratedColumn;
/** Column name, if different from the attribute name. Default: the attribute name. */
field?: string;
/** Unique constraint: `true`, a constraint name, or `{ name, msg }`. */
unique?: boolean | string | UniqueOptions;
/** Column comment (`COMMENT ON COLUMN`) written by `sync()`. */
comment?: string;
/** Allowed values of an `ENUM` column declared with the bare type, `{ type: DataTypes.ENUM, values: ['a', 'b'] }` (Sequelize v6). Ignored for other types. */
values?: readonly string[];
/** Foreign key reference. */
references?: string | ReferenceOptions;
/** `ON DELETE` action of the foreign key, e.g. `'CASCADE'`; needs `references`. */
onDelete?: string;
/** `ON UPDATE` action of the foreign key, e.g. `'CASCADE'`; needs `references`. */
onUpdate?: string;
/** Validators run before INSERT / UPDATE. */
validate?: AttributeValidateOptions;
/** Custom getter, called with the model instance as `this`. */
get?(this: M): unknown;
/** Custom setter, called with the model instance as `this`. */
set?(this: M, value: unknown): void;
}>;
/**
* Attributes passed to `Model.init` / `sequelize.define`: a type or full column options per attribute.
* `M` is the model instance type of `this` in `get` / `set`; with `TAttributes` (v6 shape) every
* attribute of that type must be defined.
*/
// oxlint-disable-next-line typescript/no-explicit-any -- the v6 default of `TAttributes`
export type ModelAttributes<M extends AnyModel = AnyModel, TAttributes = any> = {
readonly [name in keyof TAttributes]: DataTypeInput | AttributeOptions<M>;
};
/** Normalized attribute (exposed as `Model.rawAttributes[name]`). */
export type AttributeDefinition = Readonly<
Omit<AttributeOptions, 'type' | 'field'> & {
/** Attribute name. */
fieldName: string;
/** Column name. */
field: string;
/** Normalized column type. */
type: DataType;
/** Whether `NULL` is allowed. */
allowNull: boolean;
/** Added automatically (`id`, timestamps). */
_autoGenerated?: boolean;
}
>;
|