UNPKG

11.4 kBTypeScriptView Raw
1import { DataType } from '../data-types';
2import {
3 CreateOptions,
4 CreationAttributes,
5 Filterable,
6 FindOptions,
7 InstanceUpdateOptions,
8 Model,
9 ModelCtor,
10 Transactionable,
11} from '../model';
12import { Association, ManyToManyOptions, MultiAssociationAccessors } from './base';
13
14/**
15 * Options provided when associating models with hasMany relationship
16 */
17export interface HasManyOptions extends ManyToManyOptions {
18
19 /**
20 * The name of the field to use as the key for the association in the source table. Defaults to the primary
21 * key of the source table
22 */
23 sourceKey?: string;
24
25 /**
26 * A string or a data type to represent the identifier in the table
27 */
28 keyType?: DataType;
29}
30
31export class HasMany<S extends Model = Model, T extends Model = Model> extends Association<S, T> {
32 public accessors: MultiAssociationAccessors;
33 constructor(source: ModelCtor<S>, target: ModelCtor<T>, options: HasManyOptions);
34}
35
36/**
37 * The options for the getAssociations mixin of the hasMany association.
38 * @see HasManyGetAssociationsMixin
39 */
40export interface HasManyGetAssociationsMixinOptions extends FindOptions<any> {
41 /**
42 * Apply a scope on the related model, or remove its default scope by passing false.
43 */
44 scope?: string | string[] | boolean;
45}
46
47/**
48 * The getAssociations mixin applied to models with hasMany.
49 * An example of usage is as follows:
50 *
51 * ```js
52 *
53 * User.hasMany(Role);
54 *
55 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
56 * getRoles: Sequelize.HasManyGetAssociationsMixin<RoleInstance>;
57 * // setRoles...
58 * // addRoles...
59 * // addRole...
60 * // createRole...
61 * // removeRole...
62 * // removeRoles...
63 * // hasRole...
64 * // hasRoles...
65 * // countRoles...
66 * }
67 * ```
68 *
69 * @see https://sequelize.org/master/class/lib/associations/has-many.js~HasMany.html
70 * @see Instance
71 */
72export type HasManyGetAssociationsMixin<TModel> = (options?: HasManyGetAssociationsMixinOptions) => Promise<TModel[]>;
73
74/**
75 * The options for the setAssociations mixin of the hasMany association.
76 * @see HasManySetAssociationsMixin
77 */
78export interface HasManySetAssociationsMixinOptions extends FindOptions<any>, InstanceUpdateOptions<any> {}
79
80/**
81 * The setAssociations mixin applied to models with hasMany.
82 * An example of usage is as follows:
83 *
84 * ```js
85 *
86 * User.hasMany(Role);
87 *
88 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
89 * // getRoles...
90 * setRoles: Sequelize.HasManySetAssociationsMixin<RoleInstance, RoleId>;
91 * // addRoles...
92 * // addRole...
93 * // createRole...
94 * // removeRole...
95 * // removeRoles...
96 * // hasRole...
97 * // hasRoles...
98 * // countRoles...
99 * }
100 * ```
101 *
102 * @see https://sequelize.org/master/class/lib/associations/has-many.js~HasMany.html
103 * @see Instance
104 */
105export type HasManySetAssociationsMixin<TModel, TModelPrimaryKey> = (
106 newAssociations?: (TModel | TModelPrimaryKey)[],
107 options?: HasManySetAssociationsMixinOptions
108) => Promise<void>;
109
110/**
111 * The options for the addAssociations mixin of the hasMany association.
112 * @see HasManyAddAssociationsMixin
113 */
114export interface HasManyAddAssociationsMixinOptions extends InstanceUpdateOptions<any> {}
115
116/**
117 * The addAssociations mixin applied to models with hasMany.
118 * An example of usage is as follows:
119 *
120 * ```js
121 *
122 * User.hasMany(Role);
123 *
124 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
125 * // getRoles...
126 * // setRoles...
127 * addRoles: Sequelize.HasManyAddAssociationsMixin<RoleInstance, RoleId>;
128 * // addRole...
129 * // createRole...
130 * // removeRole...
131 * // removeRoles...
132 * // hasRole...
133 * // hasRoles...
134 * // countRoles...
135 * }
136 * ```
137 *
138 * @see https://sequelize.org/master/class/lib/associations/has-many.js~HasMany.html
139 * @see Instance
140 */
141export type HasManyAddAssociationsMixin<TModel, TModelPrimaryKey> = (
142 newAssociations?: (TModel | TModelPrimaryKey)[],
143 options?: HasManyAddAssociationsMixinOptions
144) => Promise<void>;
145
146/**
147 * The options for the addAssociation mixin of the hasMany association.
148 * @see HasManyAddAssociationMixin
149 */
150export interface HasManyAddAssociationMixinOptions extends InstanceUpdateOptions<any> {}
151
152/**
153 * The addAssociation mixin applied to models with hasMany.
154 * An example of usage is as follows:
155 *
156 * ```js
157 *
158 * User.hasMany(Role);
159 *
160 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
161 * // getRoles...
162 * // setRoles...
163 * // addRoles...
164 * addRole: Sequelize.HasManyAddAssociationMixin<RoleInstance, RoleId>;
165 * // createRole...
166 * // removeRole...
167 * // removeRoles...
168 * // hasRole...
169 * // hasRoles...
170 * // countRoles...
171 * }
172 * ```
173 *
174 * @see https://sequelize.org/master/class/lib/associations/has-many.js~HasMany.html
175 * @see Instance
176 */
177export type HasManyAddAssociationMixin<TModel, TModelPrimaryKey> = (
178 newAssociation?: TModel | TModelPrimaryKey,
179 options?: HasManyAddAssociationMixinOptions
180) => Promise<void>;
181
182/**
183 * The options for the createAssociation mixin of the hasMany association.
184 * @see HasManyCreateAssociationMixin
185 */
186export interface HasManyCreateAssociationMixinOptions extends CreateOptions<any> {}
187
188/**
189 * The createAssociation mixin applied to models with hasMany.
190 * An example of usage is as follows:
191 *
192 * ```js
193 *
194 * User.hasMany(Role);
195 *
196 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
197 * // getRoles...
198 * // setRoles...
199 * // addRoles...
200 * // addRole...
201 * createRole: Sequelize.HasManyCreateAssociationMixin<RoleAttributes>;
202 * // removeRole...
203 * // removeRoles...
204 * // hasRole...
205 * // hasRoles...
206 * // countRoles...
207 * }
208 * ```
209 *
210 * @see https://sequelize.org/master/class/lib/associations/has-many.js~HasMany.html
211 * @see Instance
212 */
213export type HasManyCreateAssociationMixin<
214 TModel extends Model,
215 TForeignKey extends keyof CreationAttributes<TModel> = never,
216 TScope extends keyof CreationAttributes<TModel> = never
217> = (
218 values?: Omit<CreationAttributes<TModel>, TForeignKey | TScope>,
219 options?: HasManyCreateAssociationMixinOptions
220) => Promise<TModel>;
221
222/**
223 * The options for the removeAssociation mixin of the hasMany association.
224 * @see HasManyRemoveAssociationMixin
225 */
226export interface HasManyRemoveAssociationMixinOptions extends InstanceUpdateOptions<any> {}
227
228/**
229 * The removeAssociation mixin applied to models with hasMany.
230 * An example of usage is as follows:
231 *
232 * ```js
233 *
234 * User.hasMany(Role);
235 *
236 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
237 * // getRoles...
238 * // setRoles...
239 * // addRoles...
240 * // addRole...
241 * // createRole...
242 * removeRole: Sequelize.HasManyRemoveAssociationMixin<RoleInstance, RoleId>;
243 * // removeRoles...
244 * // hasRole...
245 * // hasRoles...
246 * // countRoles...
247 * }
248 * ```
249 *
250 * @see https://sequelize.org/master/class/lib/associations/has-many.js~HasMany.html
251 * @see Instance
252 */
253export type HasManyRemoveAssociationMixin<TModel, TModelPrimaryKey> = (
254 oldAssociated?: TModel | TModelPrimaryKey,
255 options?: HasManyRemoveAssociationMixinOptions
256) => Promise<void>;
257
258/**
259 * The options for the removeAssociations mixin of the hasMany association.
260 * @see HasManyRemoveAssociationsMixin
261 */
262export interface HasManyRemoveAssociationsMixinOptions extends InstanceUpdateOptions<any> {}
263
264/**
265 * The removeAssociations mixin applied to models with hasMany.
266 * An example of usage is as follows:
267 *
268 * ```js
269 *
270 * User.hasMany(Role);
271 *
272 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
273 * // getRoles...
274 * // setRoles...
275 * // addRoles...
276 * // addRole...
277 * // createRole...
278 * // removeRole...
279 * removeRoles: Sequelize.HasManyRemoveAssociationsMixin<RoleInstance, RoleId>;
280 * // hasRole...
281 * // hasRoles...
282 * // countRoles...
283 * }
284 * ```
285 *
286 * @see https://sequelize.org/master/class/lib/associations/has-many.js~HasMany.html
287 * @see Instance
288 */
289export type HasManyRemoveAssociationsMixin<TModel, TModelPrimaryKey> = (
290 oldAssociateds?: (TModel | TModelPrimaryKey)[],
291 options?: HasManyRemoveAssociationsMixinOptions
292) => Promise<void>;
293
294/**
295 * The options for the hasAssociation mixin of the hasMany association.
296 * @see HasManyHasAssociationMixin
297 */
298export interface HasManyHasAssociationMixinOptions extends HasManyGetAssociationsMixinOptions {}
299
300/**
301 * The hasAssociation mixin applied to models with hasMany.
302 * An example of usage is as follows:
303 *
304 * ```js
305 *
306 * User.hasMany(Role);
307 *
308 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
309 * // getRoles...
310 * // setRoles...
311 * // addRoles...
312 * // addRole...
313 * // createRole...
314 * // removeRole...
315 * // removeRoles...
316 * hasRole: Sequelize.HasManyHasAssociationMixin<RoleInstance, RoleId>;
317 * // hasRoles...
318 * // countRoles...
319 * }
320 * ```
321 *
322 * @see https://sequelize.org/master/class/lib/associations/has-many.js~HasMany.html
323 * @see Instance
324 */
325export type HasManyHasAssociationMixin<TModel, TModelPrimaryKey> = (
326 target: TModel | TModelPrimaryKey,
327 options?: HasManyHasAssociationMixinOptions
328) => Promise<boolean>;
329
330/**
331 * The options for the hasAssociations mixin of the hasMany association.
332 * @see HasManyHasAssociationsMixin
333 */
334export interface HasManyHasAssociationsMixinOptions extends HasManyGetAssociationsMixinOptions {}
335
336/**
337 * The removeAssociations mixin applied to models with hasMany.
338 * An example of usage is as follows:
339 *
340 * ```js
341 *
342 * User.hasMany(Role);
343 *
344 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
345 * // getRoles...
346 * // setRoles...
347 * // addRoles...
348 * // addRole...
349 * // createRole...
350 * // removeRole...
351 * // removeRoles
352 * // hasRole...
353 * hasRoles: Sequelize.HasManyHasAssociationsMixin<RoleInstance, RoleId>;
354 * // countRoles...
355 * }
356 * ```
357 *
358 * @see https://sequelize.org/master/class/lib/associations/has-many.js~HasMany.html
359 * @see Instance
360 */
361export type HasManyHasAssociationsMixin<TModel, TModelPrimaryKey> = (
362 targets: (TModel | TModelPrimaryKey)[],
363 options?: HasManyHasAssociationsMixinOptions
364) => Promise<boolean>;
365
366/**
367 * The options for the countAssociations mixin of the hasMany association.
368 * @see HasManyCountAssociationsMixin
369 */
370export interface HasManyCountAssociationsMixinOptions extends Transactionable, Filterable<any> {
371 /**
372 * Apply a scope on the related model, or remove its default scope by passing false.
373 */
374 scope?: string | boolean;
375}
376
377/**
378 * The countAssociations mixin applied to models with hasMany.
379 * An example of usage is as follows:
380 *
381 * ```js
382 *
383 * User.hasMany(Role);
384 *
385 * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
386 * // getRoles...
387 * // setRoles...
388 * // addRoles...
389 * // addRole...
390 * // createRole...
391 * // removeRole...
392 * // removeRoles...
393 * // hasRole...
394 * // hasRoles...
395 * countRoles: Sequelize.HasManyCountAssociationsMixin;
396 * }
397 * ```
398 *
399 * @see https://sequelize.org/master/class/lib/associations/has-many.js~HasMany.html
400 * @see Instance
401 */
402export type HasManyCountAssociationsMixin = (options?: HasManyCountAssociationsMixinOptions) => Promise<number>;