UNPKG

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