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 | import type { AnyModel, ModelStatic } from '../model/options.ts';
/** Kind of association (Sequelize v6 `associationType`). */
export type AssociationType = 'BelongsTo' | 'HasOne' | 'HasMany' | 'BelongsToMany';
/** Singular and plural names of an association alias. */
export type AssociationName = Readonly<{
/** Singular form, used by single-item accessors (`addPost`). */
singular: string;
/** Plural form, used by multi-item accessors (`getPosts`). */
plural: string;
}>;
/**
* Foreign key given as an object instead of a plain name.
*
* @example
* ```ts
* const fk: ForeignKeyOptions = { name: 'authorId', field: 'author_id', allowNull: false };
* User.hasMany(Post, { foreignKey: fk });
* ```
*/
export type ForeignKeyOptions = Readonly<{
/** Attribute name. */
name?: string;
/** Column name. */
field?: string;
/** Whether the key may be `NULL`. */
allowNull?: boolean;
}>;
/**
* Options shared by all association methods (Sequelize v6 `AssociationOptions`).
*
* Used by `hasOne`, `hasMany`, `belongsTo` and (extended) `belongsToMany`.
*
* @see {@link Model.hasMany}
*
* @example
* ```ts
* const opts: AssociationOptions = {
* as: 'posts',
* foreignKey: { name: 'authorId', allowNull: false },
* onDelete: 'CASCADE',
* };
* User.hasMany(Post, opts);
* ```
*/
export type AssociationOptions = Readonly<{
/** Alias: string, or `{ singular, plural }` for multi associations. Default: the target model name. */
as?: string | AssociationName;
/** Foreign key attribute name or options. */
foreignKey?: string | ForeignKeyOptions;
/** Attribute of the source referenced by the foreign key (hasOne / hasMany / belongsToMany). */
sourceKey?: string;
/** Attribute of the target referenced by the foreign key (belongsTo / belongsToMany). */
targetKey?: string;
/** Attribute values applied to associated rows (filter on read, values on write). */
scope?: Readonly<Record<string, unknown>>;
/** Create foreign key constraints on `sync()`.
* @defaultValue true */
constraints?: boolean;
/** `ON DELETE` action of the foreign key constraint, e.g. `'CASCADE'`. */
onDelete?: string;
/** `ON UPDATE` action of the foreign key constraint, e.g. `'CASCADE'`. */
onUpdate?: string;
/**
* Sequelize v6 `hooks` flag. With `onDelete: 'CASCADE'` and `hooks: true`, destroying an instance first
* loads its associated rows and destroys each of them with its own hooks (`beforeDestroy`, `afterDestroy`),
* then deletes the instance; without the flag PostgreSQL cascades the delete and no hooks of the target run.
* Only a real delete cascades this way, not the soft delete of a paranoid model.
*/
hooks?: boolean;
}>;
/**
* Junction table of a belongsToMany association given as an object.
*
* @example
* ```ts
* const through: ThroughOptions = { model: 'ProjectTask', scope: { kind: 'member' } };
* Project.belongsToMany(Task, { through });
* ```
*/
export type ThroughOptions = Readonly<{
/** Junction model, or the name of an implicit junction model to create. */
model: string | ModelStatic<AnyModel>;
/** Attribute values applied to junction rows. */
scope?: Readonly<Record<string, unknown>>;
}>;
/**
* Options of `belongsToMany`.
*
* Extends {@link AssociationOptions}; `foreignKey` is the junction column pointing at the source, `otherKey` the one pointing at the target.
*
* @see {@link Model.belongsToMany}
*
* @example
* ```ts
* const opts: BelongsToManyOptions = { through: 'ProjectTask', foreignKey: 'projectId', otherKey: 'taskId' };
* Project.belongsToMany(Task, opts);
* ```
*/
export type BelongsToManyOptions = AssociationOptions &
Readonly<{
/** Junction model, its name, or options. */
through: string | ModelStatic<AnyModel> | ThroughOptions;
/** Junction foreign key pointing at the target. Default: derived from the target model name. */
otherKey?: string | ForeignKeyOptions;
}>;
/** Resolved junction of a belongsToMany association. */
export type AssociationThrough = Readonly<{
/** Junction model. */
model: ModelStatic<AnyModel>;
/** Attribute values applied to junction rows. */
scope?: Readonly<Record<string, unknown>>;
}>;
/** Association between two models (Sequelize v6 association object). */
export type Association = Readonly<{
/** Kind of association. */
associationType: AssociationType;
/** Model the association is defined on. */
source: ModelStatic<AnyModel>;
/** Associated model. */
target: ModelStatic<AnyModel>;
/** Alias used in `include` and as the instance property. */
as: string;
/** Singular and plural alias forms. */
name: AssociationName;
/** Whether `as` was given explicitly. */
isAliased: boolean;
/** Foreign key attribute (on the source for belongsTo, on the target for hasOne/hasMany, on the junction for belongsToMany). */
foreignKey: string;
/** Column of the foreign key. */
identifierField: string;
/** Source attribute referenced by the foreign key. */
sourceKey: string;
/** Target attribute referenced by the foreign key (belongsTo) or by `otherKey` (belongsToMany). */
targetKey: string;
/** Junction foreign key pointing at the target (belongsToMany). */
otherKey: string | undefined;
/** Junction model (belongsToMany). */
through: AssociationThrough | undefined;
/** Association scope. */
scope: Readonly<Record<string, unknown>> | undefined;
/** True for belongsTo / hasOne. */
isSingleAssociation: boolean;
/** True for hasMany / belongsToMany. */
isMultiAssociation: boolean;
/** Names of the instance methods added to the source model. */
accessors: Readonly<Record<string, string>>;
/** Options the association was defined with. */
options: AssociationOptions;
}>;
|