2.93 kBTypeScriptView Raw
1import { ColumnOptions, Model, ModelCtor, Hookable } from '../model';
2
3export abstract class Association<S extends Model = Model, T extends Model = Model> {
4 public associationType: string;
5 public source: ModelCtor<S>;
6 public target: ModelCtor<T>;
7 public isSelfAssociation: boolean;
8 public isSingleAssociation: boolean;
9 public isMultiAssociation: boolean;
10 public as: string;
11 public isAliased: boolean;
12 public foreignKey: string;
13 public identifier: string;
14 public inspect(): string;
15}
16
17export interface SingleAssociationAccessors {
18 get: string;
19 set: string;
20 create: string;
21}
22
23export interface MultiAssociationAccessors {
24 get: string;
25 set: string;
26 addMultiple: string;
27 add: string;
28 create: string;
29 remove: string;
30 removeMultiple: string;
31 hasSingle: string;
32 hasAll: string;
33 count: string;
34}
35
36/** Foreign Key Options */
37export interface ForeignKeyOptions extends ColumnOptions {
38 /** Attribute name for the relation */
39 name?: string;
40}
41
42/**
43 * Options provided when associating models
44 */
45export interface AssociationOptions extends Hookable {
46 /**
47 * The alias of this model, in singular form. See also the `name` option passed to `sequelize.define`. If
48 * you create multiple associations between the same tables, you should provide an alias to be able to
49 * distinguish between them. If you provide an alias when creating the assocition, you should provide the
50 * same alias when eager loading and when getting associated models. Defaults to the singularized name of
51 * target
52 */
53 as?: string | { singular: string; plural: string };
54
55 /**
56 * The name of the foreign key in the target table or an object representing the type definition for the
57 * foreign column (see `Sequelize.define` for syntax). When using an object, you can add a `name` property
58 * to set the name of the column. Defaults to the name of source + primary key of source
59 */
60 foreignKey?: string | ForeignKeyOptions;
61
62 /**
63 * What happens when delete occurs.
64 *
65 * Cascade if this is a n:m, and set null if it is a 1:m
66 *
67 * @default 'SET NULL' or 'CASCADE'
68 */
69 onDelete?: string;
70
71 /**
72 * What happens when update occurs
73 *
74 * @default 'CASCADE'
75 */
76 onUpdate?: string;
77
78 /**
79 * Should on update and on delete constraints be enabled on the foreign key.
80 */
81 constraints?: boolean;
82 foreignKeyConstraint?: boolean;
83
84 scope?: AssociationScope;
85}
86
87/**
88 * Options for Association Scope
89 */
90export interface AssociationScope {
91 /**
92 * The name of the column that will be used for the associated scope and it's value
93 */
94 [scopeName: string]: unknown;
95}
96
97/**
98 * Options provided for many-to-many relationships
99 */
100export interface ManyToManyOptions extends AssociationOptions {
101 /**
102 * A key/value set that will be used for association create and find defaults on the target.
103 * (sqlite not supported for N:M)
104 */
105 scope?: AssociationScope;
106}