@types/sequelize
Version:
TypeScript definitions for sequelize
1,437 lines (1,353 loc) • 238 kB
TypeScript
// ***************************** IMPORTANT NOTE *****************************
// These types are for the 4.x branch of Sequelize. As of Sequelize 5.0,
// types are packaged directly within Sequelize itself. Please target the
// Sequelize-provided types for any changes related to versions >= 5.0.
// Based on original work by: samuelneff <https://github.com/samuelneff/sequelize-auto-ts/blob/master/lib/sequelize.d.ts>
import * as _ from "lodash";
import Promise = require("bluebird");
import * as cls from "continuation-local-storage";
import ValidatorJS = require("validator");
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
declare namespace sequelize {
//
// Associations
// ~~~~~~~~~~~~~~
//
// https://github.com/sequelize/sequelize/tree/v3.4.1/lib/associations
//
/**
* The options for the getAssociation mixin of the belongsTo association.
* @see BelongsToGetAssociationMixin
*/
interface BelongsToGetAssociationMixinOptions {
/**
* Apply a scope on the related model, or remove its default scope by passing false.
*/
scope?: string | boolean | undefined;
}
/**
* The getAssociation mixin applied to models with belongsTo.
* An example of usage is as follows:
*
* ```js
*
* User.belongsTo(Role);
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttrib>, UserAttrib {
* getRole: Sequelize.BelongsToGetAssociationMixin<RoleInstance>;
* // setRole...
* // createRole...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to/
* @see Instance
*/
interface BelongsToGetAssociationMixin<TInstance> {
/**
* Get the associated instance.
* @param options The options to use when getting the association.
*/
(options?: BelongsToGetAssociationMixinOptions): Promise<TInstance | null>;
}
/**
* The options for the setAssociation mixin of the belongsTo association.
* @see BelongsToSetAssociationMixin
*/
interface BelongsToSetAssociationMixinOptions {
/**
* Skip saving this after setting the foreign key if false.
*/
save?: boolean | undefined;
}
/**
* The setAssociation mixin applied to models with belongsTo.
* An example of usage is as follows:
*
* ```js
*
* User.belongsTo(Role);
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRole...
* setRole: Sequelize.BelongsToSetAssociationMixin<RoleInstance, RoleId>;
* // createRole...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to/
* @see Instance
*/
interface BelongsToSetAssociationMixin<TInstance, TInstancePrimaryKey> {
/**
* Set the associated instance.
* @param newAssociation An instance or the primary key of an instance to associate with this. Pass null or undefined to remove the association.
* @param options the options passed to `this.save`.
*/
(
newAssociation?: TInstance | TInstancePrimaryKey,
options?: BelongsToSetAssociationMixinOptions | InstanceSaveOptions,
): Promise<void>;
}
/**
* The options for the createAssociation mixin of the belongsTo association.
* @see BelongsToCreateAssociationMixin
*/
interface BelongsToCreateAssociationMixinOptions {}
/**
* The createAssociation mixin applied to models with belongsTo.
* An example of usage is as follows:
*
* ```js
*
* User.belongsTo(Role);
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRole...
* // setRole...
* createRole: Sequelize.BelongsToCreateAssociationMixin<RoleAttributes>;
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to/
* @see Instance
*/
interface BelongsToCreateAssociationMixin<TAttributes, TInstance> {
/**
* Create a new instance of the associated model and associate it with this.
* @param values The values used to create the association.
* @param options The options passed to `target.create` and `setAssociation`.
*/
(
values?: TAttributes,
options?: BelongsToCreateAssociationMixinOptions | CreateOptions | BelongsToSetAssociationMixinOptions,
): Promise<TInstance>;
}
/**
* The options for the getAssociation mixin of the hasOne association.
* @see HasOneGetAssociationMixin
*/
interface HasOneGetAssociationMixinOptions {
/**
* Apply a scope on the related model, or remove its default scope by passing false.
*/
scope?: string | boolean | undefined | null;
}
/**
* The getAssociation mixin applied to models with hasOne.
* An example of usage is as follows:
*
* ```js
*
* User.hasOne(Role);
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttrib>, UserAttrib {
* getRole: Sequelize.HasOneGetAssociationMixin<RoleInstance>;
* // setRole...
* // createRole...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/has-one/
* @see Instance
*/
interface HasOneGetAssociationMixin<TInstance> {
/**
* Get the associated instance.
* @param options The options to use when getting the association.
*/
(options?: HasOneGetAssociationMixinOptions): Promise<TInstance | null>;
}
/**
* The options for the setAssociation mixin of the hasOne association.
* @see HasOneSetAssociationMixin
*/
interface HasOneSetAssociationMixinOptions {
/**
* Skip saving this after setting the foreign key if false.
*/
save?: boolean | undefined;
}
/**
* The setAssociation mixin applied to models with hasOne.
* An example of usage is as follows:
*
* ```js
*
* User.hasOne(Role);
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRole...
* setRole: Sequelize.HasOneSetAssociationMixin<RoleInstance, RoleId>;
* // createRole...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/has-one/
* @see Instance
*/
interface HasOneSetAssociationMixin<TInstance, TInstancePrimaryKey> {
/**
* Set the associated instance.
* @param newAssociation An instance or the primary key of an instance to associate with this. Pass null or undefined to remove the association.
* @param options The options passed to `getAssocation` and `target.save`.
*/
(
newAssociation?: TInstance | TInstancePrimaryKey,
options?: HasOneSetAssociationMixinOptions | HasOneGetAssociationMixinOptions | InstanceSaveOptions,
): Promise<void>;
}
/**
* The options for the createAssociation mixin of the hasOne association.
* @see HasOneCreateAssociationMixin
*/
interface HasOneCreateAssociationMixinOptions {}
/**
* The createAssociation mixin applied to models with hasOne.
* An example of usage is as follows:
*
* ```js
*
* User.hasOne(Role);
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRole...
* // setRole...
* createRole: Sequelize.HasOneCreateAssociationMixin<RoleAttributes>;
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/has-one/
* @see Instance
*/
interface HasOneCreateAssociationMixin<TAttributes> {
/**
* Create a new instance of the associated model and associate it with this.
* @param values The values used to create the association.
* @param options The options passed to `target.create` and `setAssociation`.
*/
(
values?: TAttributes,
options?: HasOneCreateAssociationMixinOptions | HasOneSetAssociationMixinOptions | CreateOptions,
): Promise<void>;
}
/**
* The options for the getAssociations mixin of the hasMany association.
* @see HasManyGetAssociationsMixin
*/
interface HasManyGetAssociationsMixinOptions {
/**
* An optional where clause to limit the associated models.
*/
where?: AnyWhereOptions | undefined;
/**
* Apply a scope on the related model, or remove its default scope by passing false.
*/
scope?: string | boolean | undefined;
/**
* Load further nested related models
*/
include?: IncludeOptions | undefined;
}
/**
* The getAssociations mixin applied to models with hasMany.
* An example of usage is as follows:
*
* ```js
*
* User.hasMany(Role);
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* getRoles: Sequelize.HasManyGetAssociationsMixin<RoleInstance>;
* // setRoles...
* // addRoles...
* // addRole...
* // createRole...
* // removeRole...
* // removeRoles...
* // hasRole...
* // hasRoles...
* // countRoles...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/
* @see Instance
*/
interface HasManyGetAssociationsMixin<TInstance> {
/**
* Get everything currently associated with this, using an optional where clause.
* @param options The options to use when getting the associations.
*/
(options?: HasManyGetAssociationsMixinOptions): Promise<TInstance[]>;
}
/**
* The options for the setAssociations mixin of the hasMany association.
* @see HasManySetAssociationsMixin
*/
interface HasManySetAssociationsMixinOptions {
/**
* Run validation for the join model.
*/
validate?: boolean | undefined;
}
/**
* The setAssociations mixin applied to models with hasMany.
* An example of usage is as follows:
*
* ```js
*
* User.hasMany(Role);
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRoles...
* setRoles: Sequelize.HasManySetAssociationsMixin<RoleInstance, RoleId>;
* // addRoles...
* // addRole...
* // createRole...
* // removeRole...
* // removeRoles...
* // hasRole...
* // hasRoles...
* // countRoles...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/
* @see Instance
*/
interface HasManySetAssociationsMixin<TInstance, TInstancePrimaryKey> {
/**
* Set the associated models by passing an array of instances or their primary keys.
* Everything that it not in the passed array will be un-associated.
* @param newAssociations An array of instances or primary key of instances to associate with this. Pass null or undefined to remove all associations.
* @param options The options passed to `target.findAll` and `update`.
*/
(
newAssociations?: Array<TInstance | TInstancePrimaryKey>,
options?: HasManySetAssociationsMixinOptions | AnyFindOptions | InstanceUpdateOptions,
): Promise<void>;
}
/**
* The options for the addAssociations mixin of the hasMany association.
* @see HasManyAddAssociationsMixin
*/
interface HasManyAddAssociationsMixinOptions {
/**
* Run validation for the join model.
*/
validate?: boolean | undefined;
}
/**
* The addAssociations mixin applied to models with hasMany.
* An example of usage is as follows:
*
* ```js
*
* User.hasMany(Role);
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRoles...
* // setRoles...
* addRoles: Sequelize.HasManyAddAssociationsMixin<RoleInstance, RoleId>;
* // addRole...
* // createRole...
* // removeRole...
* // removeRoles...
* // hasRole...
* // hasRoles...
* // countRoles...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/
* @see Instance
*/
interface HasManyAddAssociationsMixin<TInstance, TInstancePrimaryKey> {
/**
* Associate several instances with this.
* @param newAssociations An array of instances or primary key of instances to associate with this.
* @param options The options passed to `target.update`.
*/
(
newAssociations?: Array<TInstance | TInstancePrimaryKey>,
options?: HasManyAddAssociationsMixinOptions | InstanceUpdateOptions,
): Promise<void>;
}
/**
* The options for the addAssociation mixin of the hasMany association.
* @see HasManyAddAssociationMixin
*/
interface HasManyAddAssociationMixinOptions {
/**
* Run validation for the join model.
*/
validate?: boolean | undefined;
}
/**
* The addAssociation mixin applied to models with hasMany.
* An example of usage is as follows:
*
* ```js
*
* User.hasMany(Role);
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRoles...
* // setRoles...
* // addRoles...
* addRole: Sequelize.HasManyAddAssociationMixin<RoleInstance, RoleId>;
* // createRole...
* // removeRole...
* // removeRoles...
* // hasRole...
* // hasRoles...
* // countRoles...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/
* @see Instance
*/
interface HasManyAddAssociationMixin<TInstance, TInstancePrimaryKey> {
/**
* Associate an instance with this.
* @param newAssociation An instance or the primary key of an instance to associate with this.
* @param options The options passed to `target.update`.
*/
(
newAssociation?: TInstance | TInstancePrimaryKey,
options?: HasManyAddAssociationMixinOptions | InstanceUpdateOptions,
): Promise<void>;
}
/**
* The options for the createAssociation mixin of the hasMany association.
* @see HasManyCreateAssociationMixin
*/
interface HasManyCreateAssociationMixinOptions {}
/**
* The createAssociation mixin applied to models with hasMany.
* An example of usage is as follows:
*
* ```js
*
* User.hasMany(Role);
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRoles...
* // setRoles...
* // addRoles...
* // addRole...
* createRole: Sequelize.HasManyCreateAssociationMixin<RoleAttributes>;
* // removeRole...
* // removeRoles...
* // hasRole...
* // hasRoles...
* // countRoles...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/
* @see Instance
*/
interface HasManyCreateAssociationMixin<TAttributes, TInstance> {
/**
* Create a new instance of the associated model and associate it with this.
* @param values The values used to create the association.
* @param options The options to use when creating the association.
*/
(
values?: TAttributes,
options?: HasManyCreateAssociationMixinOptions | CreateOptions,
): Promise<TInstance>;
}
/**
* The options for the removeAssociation mixin of the hasMany association.
* @see HasManyRemoveAssociationMixin
*/
interface HasManyRemoveAssociationMixinOptions {}
/**
* The removeAssociation mixin applied to models with hasMany.
* An example of usage is as follows:
*
* ```js
*
* User.hasMany(Role);
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRoles...
* // setRoles...
* // addRoles...
* // addRole...
* // createRole...
* removeRole: Sequelize.HasManyRemoveAssociationMixin<RoleInstance, RoleId>;
* // removeRoles...
* // hasRole...
* // hasRoles...
* // countRoles...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/
* @see Instance
*/
interface HasManyRemoveAssociationMixin<TInstance, TInstancePrimaryKey> {
/**
* Un-associate the instance.
* @param oldAssociated The instance or the primary key of the instance to un-associate.
* @param options The options passed to `target.update`.
*/
(
oldAssociated?: TInstance | TInstancePrimaryKey,
options?: HasManyRemoveAssociationMixinOptions | InstanceUpdateOptions,
): Promise<void>;
}
/**
* The options for the removeAssociations mixin of the hasMany association.
* @see HasManyRemoveAssociationsMixin
*/
interface HasManyRemoveAssociationsMixinOptions {}
/**
* The removeAssociations mixin applied to models with hasMany.
* An example of usage is as follows:
*
* ```js
*
* User.hasMany(Role);
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRoles...
* // setRoles...
* // addRoles...
* // addRole...
* // createRole...
* // removeRole...
* removeRoles: Sequelize.HasManyRemoveAssociationsMixin<RoleInstance, RoleId>;
* // hasRole...
* // hasRoles...
* // countRoles...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/
* @see Instance
*/
interface HasManyRemoveAssociationsMixin<TInstance, TInstancePrimaryKey> {
/**
* Un-associate several instances.
* @param oldAssociated An array of instances or primary key of instances to un-associate.
* @param options The options passed to `target.update`.
*/
(
oldAssociateds?: Array<TInstance | TInstancePrimaryKey>,
options?: HasManyRemoveAssociationsMixinOptions | InstanceUpdateOptions,
): Promise<void>;
}
/**
* The options for the hasAssociation mixin of the hasMany association.
* @see HasManyHasAssociationMixin
*/
interface HasManyHasAssociationMixinOptions {}
/**
* The hasAssociation mixin applied to models with hasMany.
* An example of usage is as follows:
*
* ```js
*
* User.hasMany(Role);
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRoles...
* // setRoles...
* // addRoles...
* // addRole...
* // createRole...
* // removeRole...
* // removeRoles...
* hasRole: Sequelize.HasManyHasAssociationMixin<RoleInstance, RoleId>;
* // hasRoles...
* // countRoles...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/
* @see Instance
*/
interface HasManyHasAssociationMixin<TInstance, TInstancePrimaryKey> {
/**
* Check if an instance is associated with this.
* @param target The instance or the primary key of the instance to check.
* @param options The options passed to `getAssociations`.
*/
(
target: TInstance | TInstancePrimaryKey,
options?: HasManyHasAssociationMixinOptions | HasManyGetAssociationsMixinOptions,
): Promise<boolean>;
}
/**
* The options for the hasAssociations mixin of the hasMany association.
* @see HasManyHasAssociationsMixin
*/
interface HasManyHasAssociationsMixinOptions {}
/**
* The removeAssociations mixin applied to models with hasMany.
* An example of usage is as follows:
*
* ```js
*
* User.hasMany(Role);
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRoles...
* // setRoles...
* // addRoles...
* // addRole...
* // createRole...
* // removeRole...
* // removeRoles
* // hasRole...
* hasRoles: Sequelize.HasManyHasAssociationsMixin<RoleInstance, RoleId>;
* // countRoles...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/
* @see Instance
*/
interface HasManyHasAssociationsMixin<TInstance, TInstancePrimaryKey> {
/**
* Check if all instances are associated with this.
* @param targets An array of instances or primary key of instances to check.
* @param options The options passed to `getAssociations`.
*/
(
targets: Array<TInstance | TInstancePrimaryKey>,
options?: HasManyHasAssociationsMixinOptions | HasManyGetAssociationsMixinOptions,
): Promise<boolean>;
}
/**
* The options for the countAssociations mixin of the hasMany association.
* @see HasManyCountAssociationsMixin
*/
interface HasManyCountAssociationsMixinOptions {
/**
* An optional where clause to limit the associated models.
*/
where?: AnyWhereOptions | undefined;
/**
* Apply a scope on the related model, or remove its default scope by passing false.
*/
scope?: string | boolean | undefined;
}
/**
* The countAssociations mixin applied to models with hasMany.
* An example of usage is as follows:
*
* ```js
*
* User.hasMany(Role);
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRoles...
* // setRoles...
* // addRoles...
* // addRole...
* // createRole...
* // removeRole...
* // removeRoles...
* // hasRole...
* // hasRoles...
* countRoles: Sequelize.HasManyCountAssociationsMixin;
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/
* @see Instance
*/
interface HasManyCountAssociationsMixin {
/**
* Count everything currently associated with this, using an optional where clause.
* @param options The options to use when counting the associations.
*/
(options?: HasManyCountAssociationsMixinOptions): Promise<number>;
}
/**
* The options for the getAssociations mixin of the belongsToMany association.
* @see BelongsToManyGetAssociationsMixin
*/
interface BelongsToManyGetAssociationsMixinOptions {
/**
* An optional where clause to limit the associated models.
*/
where?: AnyWhereOptions | undefined;
/**
* Apply a scope on the related model, or remove its default scope by passing false.
*/
scope?: string | boolean | undefined;
}
/**
* The getAssociations mixin applied to models with belongsToMany.
* An example of usage is as follows:
*
* ```js
*
* User.belongsToMany(Role, { through: UserRole });
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* getRoles: Sequelize.BelongsToManyGetAssociationsMixin<RoleInstance>;
* // setRoles...
* // addRoles...
* // addRole...
* // createRole...
* // removeRole...
* // removeRoles...
* // hasRole...
* // hasRoles...
* // countRoles...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to-many/
* @see Instance
*/
interface BelongsToManyGetAssociationsMixin<TInstance> {
/**
* Get everything currently associated with this, using an optional where clause.
* @param options The options to use when getting the associations.
*/
(options?: BelongsToManyGetAssociationsMixinOptions): Promise<TInstance[]>;
}
/**
* The options for the setAssociations mixin of the belongsToMany association.
* @see BelongsToManySetAssociationsMixin
*/
interface BelongsToManySetAssociationsMixinOptions {
/**
* Run validation for the join model.
*/
validate?: boolean | undefined;
}
/**
* The setAssociations mixin applied to models with belongsToMany.
* An example of usage is as follows:
*
* ```js
*
* User.belongsToMany(Role, { through: UserRole });
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRoles...
* setRoles: Sequelize.BelongsToManySetAssociationsMixin<RoleInstance, RoleId, UserRoleAttributes>;
* // addRoles...
* // addRole...
* // createRole...
* // removeRole...
* // removeRoles...
* // hasRole...
* // hasRoles...
* // countRoles...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to-many/
* @see Instance
*/
interface BelongsToManySetAssociationsMixin<TInstance, TInstancePrimaryKey, TJoinTableAttributes> {
/**
* Set the associated models by passing an array of instances or their primary keys.
* Everything that it not in the passed array will be un-associated.
* @param newAssociations An array of instances or primary key of instances to associate with this. Pass null or undefined to remove all associations.
* @param options The options passed to `through.findAll`, `bulkCreate`, `update` and `destroy`. Can also hold additional attributes for the join table.
*/
(
newAssociations?: Array<TInstance | TInstancePrimaryKey>,
options?:
| BelongsToManySetAssociationsMixinOptions
| AnyFindOptions
| BulkCreateOptions
| InstanceUpdateOptions
| InstanceDestroyOptions
| { through: TJoinTableAttributes },
): Promise<void>;
}
/**
* The options for the addAssociations mixin of the belongsToMany association.
* @see BelongsToManyAddAssociationsMixin
*/
interface BelongsToManyAddAssociationsMixinOptions {
/**
* Run validation for the join model.
*/
validate?: boolean | undefined;
}
/**
* The addAssociations mixin applied to models with belongsToMany.
* An example of usage is as follows:
*
* ```js
*
* User.belongsToMany(Role, { through: UserRole });
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRoles...
* // setRoles...
* addRoles: Sequelize.BelongsToManyAddAssociationsMixin<RoleInstance, RoleId, UserRoleAttributes>;
* // addRole...
* // createRole...
* // removeRole...
* // removeRoles...
* // hasRole...
* // hasRoles...
* // countRoles...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to-many/
* @see Instance
*/
interface BelongsToManyAddAssociationsMixin<TInstance, TInstancePrimaryKey, TJoinTableAttributes> {
/**
* Associate several instances with this.
* @param newAssociations An array of instances or primary key of instances to associate with this.
* @param options The options passed to `through.findAll`, `bulkCreate`, `update` and `destroy`. Can also hold additional attributes for the join table.
*/
(
newAssociations?: Array<TInstance | TInstancePrimaryKey>,
options?:
| BelongsToManyAddAssociationsMixinOptions
| AnyFindOptions
| BulkCreateOptions
| InstanceUpdateOptions
| InstanceDestroyOptions
| { through: TJoinTableAttributes },
): Promise<void>;
}
/**
* The options for the addAssociation mixin of the belongsToMany association.
* @see BelongsToManyAddAssociationMixin
*/
interface BelongsToManyAddAssociationMixinOptions {
/**
* Run validation for the join model.
*/
validate?: boolean | undefined;
}
/**
* The addAssociation mixin applied to models with belongsToMany.
* An example of usage is as follows:
*
* ```js
*
* User.belongsToMany(Role, { through: UserRole });
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRoles...
* // setRoles...
* // addRoles...
* addRole: Sequelize.BelongsToManyAddAssociationMixin<RoleInstance, RoleId, UserRoleAttributes>;
* // createRole...
* // removeRole...
* // removeRoles...
* // hasRole...
* // hasRoles...
* // countRoles...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to-many/
* @see Instance
*/
interface BelongsToManyAddAssociationMixin<TInstance, TInstancePrimaryKey, TJoinTableAttributes> {
/**
* Associate an instance with this.
* @param newAssociation An instance or the primary key of an instance to associate with this.
* @param options The options passed to `through.findAll`, `bulkCreate`, `update` and `destroy`. Can also hold additional attributes for the join table.
*/
(
newAssociation?: TInstance | TInstancePrimaryKey,
options?:
| BelongsToManyAddAssociationMixinOptions
| AnyFindOptions
| BulkCreateOptions
| InstanceUpdateOptions
| InstanceDestroyOptions
| { through: TJoinTableAttributes },
): Promise<void>;
}
/**
* The options for the createAssociation mixin of the belongsToMany association.
* @see BelongsToManyCreateAssociationMixin
*/
interface BelongsToManyCreateAssociationMixinOptions {}
/**
* The createAssociation mixin applied to models with belongsToMany.
* An example of usage is as follows:
*
* ```js
*
* User.belongsToMany(Role, { through: UserRole });
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRoles...
* // setRoles...
* // addRoles...
* // addRole...
* createRole: Sequelize.BelongsToManyCreateAssociationMixin<RoleAttributes, UserRoleAttributes>;
* // removeRole...
* // removeRoles...
* // hasRole...
* // hasRoles...
* // countRoles...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to-many/
* @see Instance
*/
interface BelongsToManyCreateAssociationMixin<TAttributes, TInstance, TJoinTableAttributes> {
/**
* Create a new instance of the associated model and associate it with this.
* @param values The values used to create the association.
* @param options Options passed to `create` and `add`. Can also hold additional attributes for the join table.
*/
(
values?: TAttributes,
options?: BelongsToManyCreateAssociationMixinOptions | CreateOptions | { through: TJoinTableAttributes },
): Promise<TInstance>;
}
/**
* The options for the removeAssociation mixin of the belongsToMany association.
* @see BelongsToManyRemoveAssociationMixin
*/
interface BelongsToManyRemoveAssociationMixinOptions {}
/**
* The removeAssociation mixin applied to models with belongsToMany.
* An example of usage is as follows:
*
* ```js
*
* User.belongsToMany(Role, { through: UserRole });
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRoles...
* // setRoles...
* // addRoles...
* // addRole...
* // createRole...
* removeRole: Sequelize.BelongsToManyRemoveAssociationMixin<RoleInstance, RoleId>;
* // removeRoles...
* // hasRole...
* // hasRoles...
* // countRoles...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to-many/
* @see Instance
*/
interface BelongsToManyRemoveAssociationMixin<TInstance, TInstancePrimaryKey> {
/**
* Un-associate the instance.
* @param oldAssociated The instance or the primary key of the instance to un-associate.
* @param options The options passed to `through.destroy`.
*/
(
oldAssociated?: TInstance | TInstancePrimaryKey,
options?: BelongsToManyRemoveAssociationMixinOptions | InstanceDestroyOptions,
): Promise<void>;
}
/**
* The options for the removeAssociations mixin of the belongsToMany association.
* @see BelongsToManyRemoveAssociationsMixin
*/
interface BelongsToManyRemoveAssociationsMixinOptions {}
/**
* The removeAssociations mixin applied to models with belongsToMany.
* An example of usage is as follows:
*
* ```js
*
* User.belongsToMany(Role, { through: UserRole });
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRoles...
* // setRoles...
* // addRoles...
* // addRole...
* // createRole...
* // removeRole...
* removeRoles: Sequelize.BelongsToManyRemoveAssociationsMixin<RoleInstance, RoleId>;
* // hasRole...
* // hasRoles...
* // countRoles...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to-many/
* @see Instance
*/
interface BelongsToManyRemoveAssociationsMixin<TInstance, TInstancePrimaryKey> {
/**
* Un-associate several instances.
* @param oldAssociated An array of instances or primary key of instances to un-associate.
* @param options The options passed to `through.destroy`.
*/
(
oldAssociateds?: Array<TInstance | TInstancePrimaryKey>,
options?: BelongsToManyRemoveAssociationsMixinOptions | InstanceDestroyOptions,
): Promise<void>;
}
/**
* The options for the hasAssociation mixin of the belongsToMany association.
* @see BelongsToManyHasAssociationMixin
*/
interface BelongsToManyHasAssociationMixinOptions {}
/**
* The hasAssociation mixin applied to models with belongsToMany.
* An example of usage is as follows:
*
* ```js
*
* User.belongsToMany(Role, { through: UserRole });
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRoles...
* // setRoles...
* // addRoles...
* // addRole...
* // createRole...
* // removeRole...
* // removeRoles...
* hasRole: Sequelize.BelongsToManyHasAssociationMixin<RoleInstance, RoleId>;
* // hasRoles...
* // countRoles...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to-many/
* @see Instance
*/
interface BelongsToManyHasAssociationMixin<TInstance, TInstancePrimaryKey> {
/**
* Check if an instance is associated with this.
* @param target The instance or the primary key of the instance to check.
* @param options The options passed to `getAssociations`.
*/
(
target: TInstance | TInstancePrimaryKey,
options?: BelongsToManyHasAssociationMixinOptions | BelongsToManyGetAssociationsMixinOptions,
): Promise<boolean>;
}
/**
* The options for the hasAssociations mixin of the belongsToMany association.
* @see BelongsToManyHasAssociationsMixin
*/
interface BelongsToManyHasAssociationsMixinOptions {}
/**
* The removeAssociations mixin applied to models with belongsToMany.
* An example of usage is as follows:
*
* ```js
*
* User.belongsToMany(Role, { through: UserRole });
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRoles...
* // setRoles...
* // addRoles...
* // addRole...
* // createRole...
* // removeRole...
* // removeRoles
* // hasRole...
* hasRoles: Sequelize.BelongsToManyHasAssociationsMixin<RoleInstance, RoleId>;
* // countRoles...
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to-many/
* @see Instance
*/
interface BelongsToManyHasAssociationsMixin<TInstance, TInstancePrimaryKey> {
/**
* Check if all instances are associated with this.
* @param targets An array of instances or primary key of instances to check.
* @param options The options passed to `getAssociations`.
*/
(
targets: Array<TInstance | TInstancePrimaryKey>,
options?: BelongsToManyHasAssociationsMixinOptions | BelongsToManyGetAssociationsMixinOptions,
): Promise<boolean>;
}
/**
* The options for the countAssociations mixin of the belongsToMany association.
* @see BelongsToManyCountAssociationsMixin
*/
interface BelongsToManyCountAssociationsMixinOptions {
/**
* An optional where clause to limit the associated models.
*/
where?: AnyWhereOptions | undefined;
/**
* Apply a scope on the related model, or remove its default scope by passing false.
*/
scope?: string | boolean | undefined;
}
/**
* The countAssociations mixin applied to models with belongsToMany.
* An example of usage is as follows:
*
* ```js
*
* User.belongsToMany(Role, { through: UserRole });
*
* interface UserInstance extends Sequelize.Instance<UserInstance, UserAttributes>, UserAttributes {
* // getRoles...
* // setRoles...
* // addRoles...
* // addRole...
* // createRole...
* // removeRole...
* // removeRoles...
* // hasRole...
* // hasRoles...
* countRoles: Sequelize.BelongsToManyCountAssociationsMixin;
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to-many/
* @see Instance
*/
interface BelongsToManyCountAssociationsMixin {
/**
* Count everything currently associated with this, using an optional where clause.
* @param options The options to use when counting the associations.
*/
(options?: BelongsToManyCountAssociationsMixinOptions): Promise<number>;
}
/**
* Foreign Key Options
*
* @see AssociationOptions
*/
interface AssociationForeignKeyOptions extends ColumnOptions {
/**
* Attribute name for the relation
*/
name?: string | undefined;
unique?: boolean | string | undefined;
}
/**
* Options provided when associating models
*
* @see Association class
*/
interface AssociationOptions {
/**
* Set to true to run before-/afterDestroy hooks when an associated model is deleted because of a cascade.
* For example if `User.hasOne(Profile, {onDelete: 'cascade', hooks:true})`, the before-/afterDestroy hooks
* for profile will be called when a user is deleted. Otherwise the profile will be deleted without invoking
* any hooks.
*
* Defaults to false
*/
hooks?: boolean | undefined;
/**
* The alias of this model, in singular form. See also the `name` option passed to `sequelize.define`. If
* you create multiple associations between the same tables, you should provide an alias to be able to
* distinguish between them. If you provide an alias when creating the assocition, you should provide the
* same alias when eager loading and when getting assocated models. Defaults to the singularized name of
* target
*/
as?: string | { singular: string; plural: string } | undefined;
/**
* The name of the foreign key in the target table or an object representing the type definition for the
* foreign column (see `Sequelize.define` for syntax). When using an object, you can add a `name` property
* to set the name of the column. Defaults to the name of source + primary key of source
*/
foreignKey?: string | AssociationForeignKeyOptions | undefined;
/**
* What happens when delete occurs.
*
* Cascade if this is a n:m, and set null if it is a 1:m
*
* Defaults to 'SET NULL' or 'CASCADE'
*/
onDelete?: string | undefined;
/**
* What happens when update occurs
*
* Defaults to 'CASCADE'
*/
onUpdate?: string | undefined;
/**
* Should on update and on delete constraints be enabled on the foreign key.
*/
constraints?: boolean | undefined;
foreignKeyConstraint?: boolean | undefined;
scope?: AssociationScope | undefined;
}
/**
* Options for Association Scope
*
* @see AssociationOptionsManyToMany
*/
interface AssociationScope {
/**
* The name of the column that will be used for the associated scope and it's value
*/
[scopeName: string]: any;
}
/**
* Options provided for many-to-many relationships
*
* @see AssociationOptionsHasMany
* @see AssociationOptionsBelongsToMany
*/
interface AssociationOptionsManyToMany extends AssociationOptions {
/**
* A key/value set that will be used for association create and find defaults on the target.
* (sqlite not supported for N:M)
*/
scope?: AssociationScope | undefined;
}
/**
* Options provided when associating models with hasOne relationship
*
* @see Association class hasOne method
*/
interface AssociationOptionsHasOne extends AssociationOptions {
/**
* A string or a data type to represent the identifier in the table
*/
keyType?: DataTypeAbstract | undefined;
}
/**
* Options provided when associating models with belongsTo relationship
*
* @see Association class belongsTo method
*/
interface AssociationOptionsBelongsTo extends AssociationOptions {
/**
* The name of the field to use as the key for the association in the target table. Defaults to the primary
* key of the target table
*/
targetKey?: string | undefined;
/**
* A string or a data type to represent the identifier in the table
*/
keyType?: DataTypeAbstract | undefined;
}
/**
* Options provided when associating models with hasMany relationship
*
* @see Association class hasMany method
*/
interface AssociationOptionsHasMany extends AssociationOptionsManyToMany {
/**
* A string or a data type to represent the identifier in the table
*/
keyType?: DataTypeAbstract | undefined;
/**
* A string to represent the name of the field to use as the key for an 1 to many association in the source table.
*
* @see http://docs.sequelizejs.com/class/lib/model.js~Model.html#static-method-hasMany
* @see https://github.com/sequelize/sequelize/blob/b4fd46426db9cdbb97074bea121203d565e4195d/lib/associations/has-many.js#L81
*/
sourceKey?: string | undefined;
}
/**
* Options provided when associating models with belongsToMany relationship
*
* @see Association class belongsToMany method
*/
interface AssociationOptionsBelongsToMany extends AssociationOptionsManyToMany {
/**
* The name of the table that is used to join source and target in n:m associations. Can also be a
* sequelize
* model if you want to define the junction table yourself and add extra attributes to it.
*
* In 3.4.1 version of Sequelize, hasMany's use of through gives an error, and on the other hand through
* option for belongsToMany has been made required.
*
* @see https://github.com/sequelize/sequelize/blob/v3.4.1/lib/associations/has-many.js
* @see https://github.com/sequelize/sequelize/blob/v3.4.1/lib/associations/belongs-to-many.js
*/
through: Model<any, any> | string | ThroughOptions;
/**
* The name of the foreign key in the join table (representing the target model) or an object representing
* the type definition for the other column (see `Sequelize.define` for syntax). When using an object, you
* can add a `name` property to set the name of the colum. Defaults to the name of target + primary key of
* target
*/
otherKey?: string | AssociationForeignKeyOptions | undefined;
/**
* Should the join model have timestamps
*/
timestamps?: boolean | undefined;
/**
* Belongs-To-Many creates a unique key when primary key is not present on through model. This unique key name can be overridden using uniqueKey option.
*/
uniqueKey?: string | undefined;
}
/**
* Used for a association table in n:m associations.
*
* @see AssociationOptionsBelongsToMany
*/
interface ThroughOptions {
/**
* The model used to join both sides of the N:M association.
*/
model: Model<any, any>;
/**
* A key/value set that will be used for association create and find defaults on the through model.
* (Remember to add the attributes to the through model)
*/
scope?: AssociationScope | undefined;
/**
* If true a unique key will be generated from the foreign keys used (might want to turn this off and create
* specific unique keys when using scopes)
*
* Defaults to true
*/
unique?: boolean | undefined;
}
/**
* Creating assocations in sequelize is done by calling one of the belongsTo / hasOne / hasMany functions on a
* model (the source), and providing another model as the first argument to the function (the target).
*
* * hasOne - adds a foreign key to target
* * belongsTo - add a foreign key to source
* * hasMany - adds a foreign key to target, unless you also specify that target hasMany source, in which case
* a
* junction table is created with sourceId and targetId
*
* Creating an association will add a foreign key constraint to the attributes. All associations use `CASCADE`
* on update and `SET NULL` on delete, except for n:m, which also uses `CASCADE` on delete.
*
* When creating associations, you can provide an alias, via the `as` option. This is useful if the same model
* is associated twice, or you want your association to be called something other than the name of the target
* model.
*
* As an example, consider the case where users have many pictures, one of which is their profile picture. All
* pictures have a `userId`, but in addition the user model also has a `profilePictureId`, to be able to easily
* load the user's profile picture.
*
* ```js
* User.hasMany(Picture)
* User.belongsTo(Picture, { as: 'ProfilePicture', constraints: false })
*
* user.getPictures() // gets you all pictures
* user.getProfilePicture() // gets you only the profile picture
*
* User.findAll({
* where: ...,
* include: [
* { model: Picture }, // load all pictures
* { model: Picture, as: 'ProfilePicture' }, // load the profile picture. Notice that the spelling must be
* the exact same as the one in the association
* ]
* })
* ```
* To get full control over the foreign key column added by sequelize, you can use the `