1 | import {
|
2 | BulkCreateOptions,
|
3 | CreateOptions,
|
4 | CreationAttributes,
|
5 | Filterable,
|
6 | FindAttributeOptions,
|
7 | FindOptions,
|
8 | InstanceDestroyOptions,
|
9 | InstanceUpdateOptions,
|
10 | Model,
|
11 | ModelCtor,
|
12 | ModelType,
|
13 | Transactionable,
|
14 | } from '../model';
|
15 | import { Association, AssociationScope, ForeignKeyOptions, ManyToManyOptions, MultiAssociationAccessors } from './base';
|
16 |
|
17 | /**
|
18 | * Used for a association table in n:m associations.
|
19 | */
|
20 | export interface ThroughOptions {
|
21 | /**
|
22 | * The model used to join both sides of the N:M association.
|
23 | * Can be a string if you want the model to be generated by sequelize.
|
24 | */
|
25 | model: ModelType | string;
|
26 |
|
27 | /**
|
28 | * If true the generated join table will be paranoid
|
29 | * @default false
|
30 | */
|
31 | paranoid?: boolean;
|
32 |
|
33 | /**
|
34 | * A key/value set that will be used for association create and find defaults on the through model.
|
35 | * (Remember to add the attributes to the through model)
|
36 | */
|
37 | scope?: AssociationScope;
|
38 |
|
39 | /**
|
40 | * If true a unique key will be generated from the foreign keys used (might want to turn this off and create
|
41 | * specific unique keys when using scopes)
|
42 | *
|
43 | * @default true
|
44 | */
|
45 | unique?: boolean;
|
46 | }
|
47 |
|
48 | /**
|
49 | * Attributes for the join table
|
50 | */
|
51 | export interface JoinTableAttributes {
|
52 | [attribute: string]: unknown;
|
53 | }
|
54 |
|
55 | /**
|
56 | * Options provided when associating models with belongsToMany relationship
|
57 | */
|
58 | export interface BelongsToManyOptions extends ManyToManyOptions {
|
59 | /**
|
60 | * The name of the table that is used to join source and target in n:m associations. Can also be a
|
61 | * sequelize model if you want to define the junction table yourself and add extra attributes to it.
|
62 | */
|
63 | through: ModelType | string | ThroughOptions;
|
64 |
|
65 | /**
|
66 | * The name of the foreign key in the join table (representing the target model) or an object representing
|
67 | * the type definition for the other column (see `Sequelize.define` for syntax). When using an object, you
|
68 | * can add a `name` property to set the name of the colum. Defaults to the name of target + primary key of
|
69 | * target
|
70 | */
|
71 | otherKey?: string | ForeignKeyOptions;
|
72 |
|
73 | /**
|
74 | * The name of the field to use as the key for the association in the source table. Defaults to the primary
|
75 | * key of the source table
|
76 | */
|
77 | sourceKey?: string;
|
78 |
|
79 | /**
|
80 | * The name of the field to use as the key for the association in the target table. Defaults to the primary
|
81 | * key of the target table
|
82 | */
|
83 | targetKey?: string;
|
84 |
|
85 | /**
|
86 | * Should the join model have timestamps
|
87 | */
|
88 | timestamps?: boolean;
|
89 |
|
90 | /**
|
91 | * The unique key name to override the autogenerated one when primary key is not present on through model
|
92 | */
|
93 | uniqueKey?: string;
|
94 | }
|
95 |
|
96 | export class BelongsToMany<S extends Model = Model, T extends Model = Model> extends Association<S, T> {
|
97 | public otherKey: string;
|
98 | public sourceKey: string;
|
99 | public targetKey: string;
|
100 | public accessors: MultiAssociationAccessors;
|
101 | constructor(source: ModelCtor<S>, target: ModelCtor<T>, options: BelongsToManyOptions);
|
102 | }
|
103 |
|
104 | /**
|
105 | * The options for the getAssociations mixin of the belongsToMany association.
|
106 | * @see BelongsToManyGetAssociationsMixin
|
107 | */
|
108 | export interface BelongsToManyGetAssociationsMixinOptions extends FindOptions<any> {
|
109 | /**
|
110 | * A list of the attributes from the join table that you want to select.
|
111 | */
|
112 | joinTableAttributes?: FindAttributeOptions
|
113 | /**
|
114 | * Apply a scope on the related model, or remove its default scope by passing false.
|
115 | */
|
116 | scope?: string | boolean;
|
117 | }
|
118 |
|
119 | /**
|
120 | * The getAssociations mixin applied to models with belongsToMany.
|
121 | * An example of usage is as follows:
|
122 | *
|
123 | * ```js
|
124 | *
|
125 | * User.belongsToMany(Role, { through: UserRole });
|
126 | *
|
127 | * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
|
128 | * getRoles: Sequelize.BelongsToManyGetAssociationsMixin<RoleInstance>;
|
129 | * // setRoles...
|
130 | * // addRoles...
|
131 | * // addRole...
|
132 | * // createRole...
|
133 | * // removeRole...
|
134 | * // removeRoles...
|
135 | * // hasRole...
|
136 | * // hasRoles...
|
137 | * // countRoles...
|
138 | * }
|
139 | * ```
|
140 | *
|
141 | * @see https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
|
142 | * @see Instance
|
143 | */
|
144 | export type BelongsToManyGetAssociationsMixin<TModel> = (
|
145 | options?: BelongsToManyGetAssociationsMixinOptions
|
146 | ) => Promise<TModel[]>;
|
147 |
|
148 | /**
|
149 | * The options for the setAssociations mixin of the belongsToMany association.
|
150 | * @see BelongsToManySetAssociationsMixin
|
151 | */
|
152 | export interface BelongsToManySetAssociationsMixinOptions
|
153 | extends FindOptions<any>,
|
154 | BulkCreateOptions<any>,
|
155 | InstanceUpdateOptions<any>,
|
156 | InstanceDestroyOptions {
|
157 | through?: JoinTableAttributes;
|
158 | }
|
159 |
|
160 | /**
|
161 | * The setAssociations mixin applied to models with belongsToMany.
|
162 | * An example of usage is as follows:
|
163 | *
|
164 | * ```js
|
165 | *
|
166 | * User.belongsToMany(Role, { through: UserRole });
|
167 | *
|
168 | * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
|
169 | * // getRoles...
|
170 | * setRoles: Sequelize.BelongsToManySetAssociationsMixin<RoleInstance, RoleId, UserRoleAttributes>;
|
171 | * // addRoles...
|
172 | * // addRole...
|
173 | * // createRole...
|
174 | * // removeRole...
|
175 | * // removeRoles...
|
176 | * // hasRole...
|
177 | * // hasRoles...
|
178 | * // countRoles...
|
179 | * }
|
180 | * ```
|
181 | *
|
182 | * @see https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
|
183 | * @see Instance
|
184 | */
|
185 | export type BelongsToManySetAssociationsMixin<TModel, TModelPrimaryKey> = (
|
186 | newAssociations?: (TModel | TModelPrimaryKey)[],
|
187 | options?: BelongsToManySetAssociationsMixinOptions
|
188 | ) => Promise<void>;
|
189 |
|
190 | /**
|
191 | * The options for the addAssociations mixin of the belongsToMany association.
|
192 | * @see BelongsToManyAddAssociationsMixin
|
193 | */
|
194 | export interface BelongsToManyAddAssociationsMixinOptions
|
195 | extends FindOptions<any>,
|
196 | BulkCreateOptions<any>,
|
197 | InstanceUpdateOptions<any>,
|
198 | InstanceDestroyOptions {
|
199 | through?: JoinTableAttributes;
|
200 | }
|
201 |
|
202 | /**
|
203 | * The addAssociations mixin applied to models with belongsToMany.
|
204 | * An example of usage is as follows:
|
205 | *
|
206 | * ```js
|
207 | *
|
208 | * User.belongsToMany(Role, { through: UserRole });
|
209 | *
|
210 | * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
|
211 | * // getRoles...
|
212 | * // setRoles...
|
213 | * addRoles: Sequelize.BelongsToManyAddAssociationsMixin<RoleInstance, RoleId, UserRoleAttributes>;
|
214 | * // addRole...
|
215 | * // createRole...
|
216 | * // removeRole...
|
217 | * // removeRoles...
|
218 | * // hasRole...
|
219 | * // hasRoles...
|
220 | * // countRoles...
|
221 | * }
|
222 | * ```
|
223 | *
|
224 | * @see https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
|
225 | * @see Instance
|
226 | */
|
227 | export type BelongsToManyAddAssociationsMixin<TModel, TModelPrimaryKey> = (
|
228 | newAssociations?: (TModel | TModelPrimaryKey)[],
|
229 | options?: BelongsToManyAddAssociationsMixinOptions
|
230 | ) => Promise<void>;
|
231 |
|
232 | /**
|
233 | * The options for the addAssociation mixin of the belongsToMany association.
|
234 | * @see BelongsToManyAddAssociationMixin
|
235 | */
|
236 | export interface BelongsToManyAddAssociationMixinOptions
|
237 | extends FindOptions<any>,
|
238 | BulkCreateOptions<any>,
|
239 | InstanceUpdateOptions<any>,
|
240 | InstanceDestroyOptions {
|
241 | through?: JoinTableAttributes;
|
242 | }
|
243 |
|
244 | /**
|
245 | * The addAssociation mixin applied to models with belongsToMany.
|
246 | * An example of usage is as follows:
|
247 | *
|
248 | * ```js
|
249 | *
|
250 | * User.belongsToMany(Role, { through: UserRole });
|
251 | *
|
252 | * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
|
253 | * // getRoles...
|
254 | * // setRoles...
|
255 | * // addRoles...
|
256 | * addRole: Sequelize.BelongsToManyAddAssociationMixin<RoleInstance, RoleId, UserRoleAttributes>;
|
257 | * // createRole...
|
258 | * // removeRole...
|
259 | * // removeRoles...
|
260 | * // hasRole...
|
261 | * // hasRoles...
|
262 | * // countRoles...
|
263 | * }
|
264 | * ```
|
265 | *
|
266 | * @see https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
|
267 | * @see Instance
|
268 | */
|
269 | export type BelongsToManyAddAssociationMixin<TModel, TModelPrimaryKey> = (
|
270 | newAssociation?: TModel | TModelPrimaryKey,
|
271 | options?: BelongsToManyAddAssociationMixinOptions
|
272 | ) => Promise<void>;
|
273 |
|
274 | /**
|
275 | * The options for the createAssociation mixin of the belongsToMany association.
|
276 | * @see BelongsToManyCreateAssociationMixin
|
277 | */
|
278 | export interface BelongsToManyCreateAssociationMixinOptions extends CreateOptions<any> {
|
279 | through?: JoinTableAttributes;
|
280 | }
|
281 | /**
|
282 | * The createAssociation mixin applied to models with belongsToMany.
|
283 | * An example of usage is as follows:
|
284 | *
|
285 | * ```js
|
286 | *
|
287 | * User.belongsToMany(Role, { through: UserRole });
|
288 | *
|
289 | * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
|
290 | * // getRoles...
|
291 | * // setRoles...
|
292 | * // addRoles...
|
293 | * // addRole...
|
294 | * createRole: Sequelize.BelongsToManyCreateAssociationMixin<RoleAttributes, UserRoleAttributes>;
|
295 | * // removeRole...
|
296 | * // removeRoles...
|
297 | * // hasRole...
|
298 | * // hasRoles...
|
299 | * // countRoles...
|
300 | * }
|
301 | * ```
|
302 | *
|
303 | * @see https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
|
304 | * @see Instance
|
305 | */
|
306 | export type BelongsToManyCreateAssociationMixin<TModel extends Model> = (
|
307 | values?: CreationAttributes<TModel>,
|
308 | options?: BelongsToManyCreateAssociationMixinOptions
|
309 | ) => Promise<TModel>;
|
310 |
|
311 | /**
|
312 | * The options for the removeAssociation mixin of the belongsToMany association.
|
313 | * @see BelongsToManyRemoveAssociationMixin
|
314 | */
|
315 | export interface BelongsToManyRemoveAssociationMixinOptions extends InstanceDestroyOptions {}
|
316 |
|
317 | /**
|
318 | * The removeAssociation mixin applied to models with belongsToMany.
|
319 | * An example of usage is as follows:
|
320 | *
|
321 | * ```js
|
322 | *
|
323 | * User.belongsToMany(Role, { through: UserRole });
|
324 | *
|
325 | * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
|
326 | * // getRoles...
|
327 | * // setRoles...
|
328 | * // addRoles...
|
329 | * // addRole...
|
330 | * // createRole...
|
331 | * removeRole: Sequelize.BelongsToManyRemoveAssociationMixin<RoleInstance, RoleId>;
|
332 | * // removeRoles...
|
333 | * // hasRole...
|
334 | * // hasRoles...
|
335 | * // countRoles...
|
336 | * }
|
337 | * ```
|
338 | *
|
339 | * @see https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
|
340 | * @see Instance
|
341 | */
|
342 | export type BelongsToManyRemoveAssociationMixin<TModel, TModelPrimaryKey> = (
|
343 | oldAssociated?: TModel | TModelPrimaryKey,
|
344 | options?: BelongsToManyRemoveAssociationMixinOptions
|
345 | ) => Promise<void>;
|
346 |
|
347 | /**
|
348 | * The options for the removeAssociations mixin of the belongsToMany association.
|
349 | * @see BelongsToManyRemoveAssociationsMixin
|
350 | */
|
351 | export interface BelongsToManyRemoveAssociationsMixinOptions extends InstanceDestroyOptions, InstanceDestroyOptions {}
|
352 |
|
353 | /**
|
354 | * The removeAssociations mixin applied to models with belongsToMany.
|
355 | * An example of usage is as follows:
|
356 | *
|
357 | * ```js
|
358 | *
|
359 | * User.belongsToMany(Role, { through: UserRole });
|
360 | *
|
361 | * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
|
362 | * // getRoles...
|
363 | * // setRoles...
|
364 | * // addRoles...
|
365 | * // addRole...
|
366 | * // createRole...
|
367 | * // removeRole...
|
368 | * removeRoles: Sequelize.BelongsToManyRemoveAssociationsMixin<RoleInstance, RoleId>;
|
369 | * // hasRole...
|
370 | * // hasRoles...
|
371 | * // countRoles...
|
372 | * }
|
373 | * ```
|
374 | *
|
375 | * @see https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
|
376 | * @see Instance
|
377 | */
|
378 | export type BelongsToManyRemoveAssociationsMixin<TModel, TModelPrimaryKey> = (
|
379 | oldAssociateds?: (TModel | TModelPrimaryKey)[],
|
380 | options?: BelongsToManyRemoveAssociationsMixinOptions
|
381 | ) => Promise<void>;
|
382 |
|
383 | /**
|
384 | * The options for the hasAssociation mixin of the belongsToMany association.
|
385 | * @see BelongsToManyHasAssociationMixin
|
386 | */
|
387 | export interface BelongsToManyHasAssociationMixinOptions extends BelongsToManyGetAssociationsMixinOptions {}
|
388 |
|
389 | /**
|
390 | * The hasAssociation mixin applied to models with belongsToMany.
|
391 | * An example of usage is as follows:
|
392 | *
|
393 | * ```js
|
394 | *
|
395 | * User.belongsToMany(Role, { through: UserRole });
|
396 | *
|
397 | * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
|
398 | * // getRoles...
|
399 | * // setRoles...
|
400 | * // addRoles...
|
401 | * // addRole...
|
402 | * // createRole...
|
403 | * // removeRole...
|
404 | * // removeRoles...
|
405 | * hasRole: Sequelize.BelongsToManyHasAssociationMixin<RoleInstance, RoleId>;
|
406 | * // hasRoles...
|
407 | * // countRoles...
|
408 | * }
|
409 | * ```
|
410 | *
|
411 | * @see https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
|
412 | * @see Instance
|
413 | */
|
414 | export type BelongsToManyHasAssociationMixin<TModel, TModelPrimaryKey> = (
|
415 | target: TModel | TModelPrimaryKey,
|
416 | options?: BelongsToManyHasAssociationMixinOptions
|
417 | ) => Promise<boolean>;
|
418 |
|
419 | /**
|
420 | * The options for the hasAssociations mixin of the belongsToMany association.
|
421 | * @see BelongsToManyHasAssociationsMixin
|
422 | */
|
423 | export interface BelongsToManyHasAssociationsMixinOptions extends BelongsToManyGetAssociationsMixinOptions {}
|
424 |
|
425 | /**
|
426 | * The removeAssociations mixin applied to models with belongsToMany.
|
427 | * An example of usage is as follows:
|
428 | *
|
429 | * ```js
|
430 | *
|
431 | * User.belongsToMany(Role, { through: UserRole });
|
432 | *
|
433 | * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
|
434 | * // getRoles...
|
435 | * // setRoles...
|
436 | * // addRoles...
|
437 | * // addRole...
|
438 | * // createRole...
|
439 | * // removeRole...
|
440 | * // removeRoles
|
441 | * // hasRole...
|
442 | * hasRoles: Sequelize.BelongsToManyHasAssociationsMixin<RoleInstance, RoleId>;
|
443 | * // countRoles...
|
444 | * }
|
445 | * ```
|
446 | *
|
447 | * @see https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
|
448 | * @see Instance
|
449 | */
|
450 | export type BelongsToManyHasAssociationsMixin<TModel, TModelPrimaryKey> = (
|
451 | targets: (TModel | TModelPrimaryKey)[],
|
452 | options?: BelongsToManyHasAssociationsMixinOptions
|
453 | ) => Promise<boolean>;
|
454 |
|
455 | /**
|
456 | * The options for the countAssociations mixin of the belongsToMany association.
|
457 | * @see BelongsToManyCountAssociationsMixin
|
458 | */
|
459 | export interface BelongsToManyCountAssociationsMixinOptions extends Transactionable, Filterable<any> {
|
460 | /**
|
461 | * Apply a scope on the related model, or remove its default scope by passing false.
|
462 | */
|
463 | scope?: string | boolean;
|
464 | }
|
465 |
|
466 | /**
|
467 | * The countAssociations mixin applied to models with belongsToMany.
|
468 | * An example of usage is as follows:
|
469 | *
|
470 | * ```js
|
471 | *
|
472 | * User.belongsToMany(Role, { through: UserRole });
|
473 | *
|
474 | * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
|
475 | * // getRoles...
|
476 | * // setRoles...
|
477 | * // addRoles...
|
478 | * // addRole...
|
479 | * // createRole...
|
480 | * // removeRole...
|
481 | * // removeRoles...
|
482 | * // hasRole...
|
483 | * // hasRoles...
|
484 | * countRoles: Sequelize.BelongsToManyCountAssociationsMixin;
|
485 | * }
|
486 | * ```
|
487 | *
|
488 | * @see https://sequelize.org/master/class/lib/associations/belongs-to-many.js~BelongsToMany.html
|
489 | * @see Instance
|
490 | */
|
491 | export type BelongsToManyCountAssociationsMixin = (
|
492 | options?: BelongsToManyCountAssociationsMixinOptions
|
493 | ) => Promise<number>;
|