1 | import { DataType } from '../data-types';
|
2 | import { CreateOptions, CreationAttributes, FindOptions, Model, ModelCtor, SaveOptions } from '../model';
|
3 | import { Association, AssociationOptions, SingleAssociationAccessors } from './base';
|
4 |
|
5 | // type ModelCtor<M extends Model> = InstanceType<typeof M>;
|
6 | /**
|
7 | * Options provided when associating models with belongsTo relationship
|
8 | *
|
9 | * @see Association class belongsTo method
|
10 | */
|
11 | export interface BelongsToOptions extends AssociationOptions {
|
12 | /**
|
13 | * The name of the field to use as the key for the association in the target table. Defaults to the primary
|
14 | * key of the target table
|
15 | */
|
16 | targetKey?: string;
|
17 |
|
18 | /**
|
19 | * A string or a data type to represent the identifier in the table
|
20 | */
|
21 | keyType?: DataType;
|
22 | }
|
23 |
|
24 | export class BelongsTo<S extends Model = Model, T extends Model = Model> extends Association<S, T> {
|
25 | public accessors: SingleAssociationAccessors;
|
26 | constructor(source: ModelCtor<S>, target: ModelCtor<T>, options: BelongsToOptions);
|
27 | }
|
28 |
|
29 | /**
|
30 | * The options for the getAssociation mixin of the belongsTo association.
|
31 | * @see BelongsToGetAssociationMixin
|
32 | */
|
33 | export interface BelongsToGetAssociationMixinOptions extends FindOptions<any> {
|
34 | /**
|
35 | * Apply a scope on the related model, or remove its default scope by passing false.
|
36 | */
|
37 | scope?: string | string[] | boolean;
|
38 | }
|
39 |
|
40 | /**
|
41 | * The getAssociation mixin applied to models with belongsTo.
|
42 | * An example of usage is as follows:
|
43 | *
|
44 | * ```js
|
45 | *
|
46 | * User.belongsTo(Role);
|
47 | *
|
48 | * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttrib>, UserAttrib {
|
49 | * getRole: Sequelize.BelongsToGetAssociationMixin<RoleInstance>;
|
50 | * // setRole...
|
51 | * // createRole...
|
52 | * }
|
53 | * ```
|
54 | *
|
55 | * @see https://sequelize.org/master/class/lib/associations/belongs-to.js~BelongsTo.html
|
56 | * @see Instance
|
57 | */
|
58 | export type BelongsToGetAssociationMixin<TModel> = (options?: BelongsToGetAssociationMixinOptions) => Promise<TModel>;
|
59 |
|
60 | /**
|
61 | * The options for the setAssociation mixin of the belongsTo association.
|
62 | * @see BelongsToSetAssociationMixin
|
63 | */
|
64 | export interface BelongsToSetAssociationMixinOptions extends SaveOptions<any> {
|
65 | /**
|
66 | * Skip saving this after setting the foreign key if false.
|
67 | */
|
68 | save?: boolean;
|
69 | }
|
70 |
|
71 | /**
|
72 | * The setAssociation mixin applied to models with belongsTo.
|
73 | * An example of usage is as follows:
|
74 | *
|
75 | * ```js
|
76 | *
|
77 | * User.belongsTo(Role);
|
78 | *
|
79 | * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
|
80 | * // getRole...
|
81 | * setRole: Sequelize.BelongsToSetAssociationMixin<RoleInstance, RoleId>;
|
82 | * // createRole...
|
83 | * }
|
84 | * ```
|
85 | *
|
86 | * @see https://sequelize.org/master/class/lib/associations/belongs-to.js~BelongsTo.html
|
87 | * @see Instance
|
88 | */
|
89 | export type BelongsToSetAssociationMixin<TModel, TPrimaryKey> = (
|
90 | newAssociation?: TModel | TPrimaryKey,
|
91 | options?: BelongsToSetAssociationMixinOptions
|
92 | ) => Promise<void>;
|
93 |
|
94 | /**
|
95 | * The options for the createAssociation mixin of the belongsTo association.
|
96 | * @see BelongsToCreateAssociationMixin
|
97 | */
|
98 | export interface BelongsToCreateAssociationMixinOptions
|
99 | extends CreateOptions<any>, BelongsToSetAssociationMixinOptions {}
|
100 |
|
101 | /**
|
102 | * The createAssociation mixin applied to models with belongsTo.
|
103 | * An example of usage is as follows:
|
104 | *
|
105 | * ```js
|
106 | *
|
107 | * User.belongsTo(Role);
|
108 | *
|
109 | * interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
|
110 | * // getRole...
|
111 | * // setRole...
|
112 | * createRole: Sequelize.BelongsToCreateAssociationMixin<RoleAttributes>;
|
113 | * }
|
114 | * ```
|
115 | *
|
116 | * @see https://sequelize.org/master/class/lib/associations/belongs-to.js~BelongsTo.html
|
117 | * @see Instance
|
118 | */
|
119 | export type BelongsToCreateAssociationMixin<TModel extends Model> = (
|
120 | values?: CreationAttributes<TModel>,
|
121 | options?: BelongsToCreateAssociationMixinOptions
|
122 | ) => Promise<TModel>;
|
123 |
|
124 | export default BelongsTo;
|