Added belongsToMany associations and tests

This commit is contained in:
Ali Taheri
2015-10-10 16:42:08 +03:30
parent 2a504785a7
commit ab6a8f4a29
2 changed files with 603 additions and 20 deletions
+465 -4
View File
@@ -341,7 +341,7 @@ declare module "sequelize" {
* 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 passed to `target.findAll` and `update`.
* @param options The options passed to `target.findAll` and `update`.
*/
(
newAssociations?: Array<TInstance | TInstancePrimaryKey>,
@@ -390,7 +390,7 @@ declare module "sequelize" {
/**
* Associate several instances with this.
* @param newAssociations An array of instances or primary key of instances to associate with this.
* @param options The pptions passed to `target.update`.
* @param options The options passed to `target.update`.
*/
(
newAssociations?: Array<TInstance | TInstancePrimaryKey>,
@@ -439,7 +439,7 @@ declare module "sequelize" {
/**
* Associate an instance with this.
* @param newAssociation An instance or the primary key of an instance to associate with this.
* @param Options passed to `target.update`.
* @param options The options passed to `target.update`.
*/
(
newAssociation?: TInstance | TInstancePrimaryKey,
@@ -712,7 +712,468 @@ declare module "sequelize" {
(options?: HasManyCountAssociationsMixinOptions): Promise<number>
}
// TODO: BelongsToMany Associations
/**
* The options for the getAssociations mixin of the belongsToMany association.
* @see BelongsToManyGetAssociationsMixin
*/
interface BelongsToManyGetAssociationsMixinOptions {
/**
* An optional where clause to limit the associated models.
*/
where?: WhereOptions;
/**
* Apply a scope on the related model, or remove its default scope by passing false.
*/
scope?: string | boolean;
}
/**
* 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;
}
/**
* 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 | FindOptions | BulkCreateOptions | InstanceUpdateOptions | InstanceDestroyOptions | 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;
}
/**
* 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 | FindOptions | BulkCreateOptions | InstanceUpdateOptions | InstanceDestroyOptions | 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;
}
/**
* 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 | FindOptions | BulkCreateOptions | InstanceUpdateOptions | InstanceDestroyOptions | 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, 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 | TJoinTableAttributes
): Promise<void>
}
/**
* 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?: WhereOptions;
/**
* Apply a scope on the related model, or remove its default scope by passing false.
*/
scope?: string | boolean;
}
/**
* 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