diff --git a/sequelize/sequelize-tests.ts b/sequelize/sequelize-tests.ts index ae6fd7af4..cda4aff11 100644 --- a/sequelize/sequelize-tests.ts +++ b/sequelize/sequelize-tests.ts @@ -151,6 +151,317 @@ Post.belongsToMany( User, { Post.belongsToMany( Post, { through : { model : Post, unique : false }, foreignKey : 'tag_id' } ); Post.belongsToMany( Post, { as : 'Parents', through : 'Family', foreignKey : 'ChildId', otherKey : 'PersonId' } ); +// +// Mixins +// ~~~~~~ +// +// https://github.com/sequelize/sequelize/tree/v3.4.1/test/integration/associations +// + +var Product = s.define('product', {}); +var product = Product.build(); + +var Barcode = s.define('barcode', {}); +var barcode = Barcode.build(); + +var Warehouse = s.define('warehouse', {}); +var warehouse = Warehouse.build(); + +var Branch = s.define('brach', {}); +var branch = Branch.build(); + +var WarehouseBranch = s.define('warehouseBranch', {}); + +var Customer = s.define('customer', {}); +var customer = Customer.build(); + +Product.hasOne(Barcode); +Barcode.belongsTo(Product); + +Warehouse.hasMany(Product); +Product.belongsTo(Warehouse); + +Warehouse.belongsToMany(Branch, { through: WarehouseBranch }); +Branch.belongsToMany(Warehouse, { through: WarehouseBranch }); + +Branch.belongsToMany(Customer, { through: 'branchCustomer' }); +Customer.belongsToMany(Branch, { through: 'branchCustomer' }); + +// hasOne +product.getBarcode(); +product.getBarcode({ scope: null }).then(b => b.code); + +product.setBarcode(); +product.setBarcode(1); +product.setBarcode(barcode); +product.setBarcode(barcode, { save: true }).then(() => { }); + +product.createBarcode(); +product.createBarcode({ id: 1, code: '1434-2' }); +product.createBarcode({ id: 1 }, { save: true, silent: true }).then(() => { }); + +// belongsTo +barcode.getProduct(); +barcode.getProduct({ scope: 'foo' }).then(p => p.name); + +barcode.setProduct(); +barcode.setProduct(1); +barcode.setProduct(product); +barcode.setProduct(product, { save: true }).then(() => { }); + +barcode.createProduct(); +barcode.createProduct({ id: 1, name: 'Crowbar' }); +barcode.createProduct({ id: 1 }, { save: true, silent: true }).then(() => { }); + +product.getWarehouse(); +product.getWarehouse({ scope: null }).then(w => w.capacity); + +product.setWarehouse(); +product.setWarehouse(1); +product.setWarehouse(warehouse); +product.setWarehouse(warehouse, { save: true }).then(() => { }); + +product.createWarehouse(); +product.createWarehouse({ id: 1, capacity: 10000 }); +product.createWarehouse({ id: 1 }, { save: true, silent: true }).then(() => { }); + +// hasMany +warehouse.getProducts(); +warehouse.getProducts({ where: {}, scope: false }); +warehouse.getProducts({ where: {}, scope: false }).then((products) => products[0].id); + +warehouse.setProducts(); +warehouse.setProducts([product]); +warehouse.setProducts([product], { validate: true }).then(() => { }); + +warehouse.addProducts(); +warehouse.addProducts([product]); +warehouse.addProducts([product, 2], { validate: false }).then(() => { }); + +warehouse.addProduct(); +warehouse.addProduct(product); +warehouse.addProduct(2, { validate: true }).then(() => { }); + +warehouse.createProduct(); +warehouse.createProduct({ id: 1, name: 'baz' }); +warehouse.createProduct({ id: 1 }, { silent: true }).then(() => { }); + +warehouse.removeProducts(); +warehouse.removeProducts([product]); +warehouse.removeProducts([product, 2], { validate: false }).then(() => { }); + +warehouse.removeProduct(); +warehouse.removeProduct(product); +warehouse.removeProduct(2, { validate: true }).then(() => { }); + +warehouse.hasProducts([product]); +warehouse.hasProducts([product, 2], { scope: 'bar' }).then((result: boolean) => { }); + +warehouse.hasProduct(product); +warehouse.hasProduct(2, { scope: 'baz' }).then((result: boolean) => { }); + +warehouse.countProducts(); +warehouse.countProducts({ scope: 'baz' }).then((result: number) => { }); + +// belongsToMany +warehouse.getBranches(); +warehouse.getBranches({ where: {} }); +warehouse.getBranches({ where: {} }).then((branches) => branches[0].rank); + +warehouse.setBranches(); +warehouse.setBranches([branch]); +warehouse.setBranches([branch, 2], { validate: true, distance: 1 }).then(() => { }); + +warehouse.addBranches(); +warehouse.addBranches([branch]); +warehouse.addBranches([branch, 2], { validate: false, distance: 1 }).then(() => { }); + +warehouse.addBranch(); +warehouse.addBranch(branch); +warehouse.addBranch(2, { validate: true, distance: 1 }).then(() => { }); + +warehouse.createBranch(); +warehouse.createBranch({ id: 1, address: 'baz' }); +warehouse.createBranch({ id: 1 }, { silent: true, distance: 1 }).then(() => { }); + +warehouse.removeBranches(); +warehouse.removeBranches([branch]); +warehouse.removeBranches([branch, 2], { validate: false }).then(() => { }); + +warehouse.removeBranch(); +warehouse.removeBranch(branch); +warehouse.removeBranch(2, { validate: true }).then(() => { }); + +warehouse.hasBranches([branch]); +warehouse.hasBranches([branch, 2], { scope: 'bar' }).then((result: boolean) => { }); + +warehouse.hasBranch(branch); +warehouse.hasBranch(2, { scope: 'baz' }).then((result: boolean) => { }); + +warehouse.countBranches(); +warehouse.countBranches({ scope: 'baz' }).then((result: number) => { }); + +// belongsToMany +customer.getBranches(); +customer.getBranches({ where: {} }); +customer.getBranches({ where: {} }).then((branches) => branches[0].rank); + +customer.setBranches(); +customer.setBranches([branch]); +customer.setBranches([branch, 2], { validate: true }).then(() => { }); + +customer.addBranches(); +customer.addBranches([branch]); +customer.addBranches([branch, 2], { validate: false }).then(() => { }); + +customer.addBranch(); +customer.addBranch(branch); +customer.addBranch(2, { validate: true }).then(() => { }); + +customer.createBranch(); +customer.createBranch({ id: 1, address: 'baz' }); +customer.createBranch({ id: 1 }, { silent: true }).then(() => { }); + +customer.removeBranches(); +customer.removeBranches([branch]); +customer.removeBranches([branch, 2], { validate: false }).then(() => { }); + +customer.removeBranch(); +customer.removeBranch(branch); +customer.removeBranch(2, { validate: true }).then(() => { }); + +customer.hasBranches([branch]); +customer.hasBranches([branch, 2], { scope: 'bar' }).then((result: boolean) => { }); + +customer.hasBranch(branch); +customer.hasBranch(2, { scope: 'baz' }).then((result: boolean) => { }); + +customer.countBranches(); +customer.countBranches({ scope: 'baz' }).then((result: number) => { }); + + + +interface ProductAttributes { + id?: number; + name?: string; + price?: number; +}; + +interface ProductInstance extends Sequelize.Instance, ProductAttributes { + // hasOne association mixins: + getBarcode: Sequelize.HasOneGetAssociationMixin; + setBarcode: Sequelize.HasOneSetAssociationMixin; + createBarcode: Sequelize.HasOneCreateAssociationMixin; + + // belongsTo association mixins: + getWarehouse: Sequelize.BelongsToGetAssociationMixin; + setWarehouse: Sequelize.BelongsToSetAssociationMixin; + createWarehouse: Sequelize.BelongsToCreateAssociationMixin; +}; + +interface BarcodeAttributes { + id?: number; + code?: string; + dateIssued?: Date; +}; + +interface BarcodeInstance extends Sequelize.Instance, BarcodeAttributes { + // belongsTo association mixins: + getProduct: Sequelize.BelongsToGetAssociationMixin; + setProduct: Sequelize.BelongsToSetAssociationMixin; + createProduct: Sequelize.BelongsToCreateAssociationMixin; +}; + +interface WarehouseAttributes { + id?: number; + address?: string; + capacity?: number; +}; + +interface WarehouseInstance extends Sequelize.Instance, WarehouseAttributes { + // hasMany association mixins: + getProducts: Sequelize.HasManyGetAssociationsMixin; + setProducts: Sequelize.HasManySetAssociationsMixin; + addProducts: Sequelize.HasManyAddAssociationsMixin; + addProduct: Sequelize.HasManyAddAssociationMixin; + createProduct: Sequelize.HasManyCreateAssociationMixin; + removeProduct: Sequelize.HasManyRemoveAssociationMixin; + removeProducts: Sequelize.HasManyRemoveAssociationsMixin; + hasProduct: Sequelize.HasManyHasAssociationMixin; + hasProducts: Sequelize.HasManyHasAssociationsMixin; + countProducts: Sequelize.HasManyCountAssociationsMixin; + + // belongsToMany association mixins: + getBranches: Sequelize.BelongsToManyGetAssociationsMixin; + setBranches: Sequelize.BelongsToManySetAssociationsMixin; + addBranches: Sequelize.BelongsToManyAddAssociationsMixin; + addBranch: Sequelize.BelongsToManyAddAssociationMixin; + createBranch: Sequelize.BelongsToManyCreateAssociationMixin; + removeBranch: Sequelize.BelongsToManyRemoveAssociationMixin; + removeBranches: Sequelize.BelongsToManyRemoveAssociationsMixin; + hasBranch: Sequelize.BelongsToManyHasAssociationMixin; + hasBranches: Sequelize.BelongsToManyHasAssociationsMixin; + countBranches: Sequelize.BelongsToManyCountAssociationsMixin; +}; + +interface BranchAttributes { + id?: number; + address?: string; + rank?: number; +}; + +interface BranchInstance extends Sequelize.Instance, BranchAttributes { + // belongsToMany association mixins: + getWarehouses: Sequelize.BelongsToManyGetAssociationsMixin; + setWarehouses: Sequelize.BelongsToManySetAssociationsMixin; + addWarehouses: Sequelize.BelongsToManyAddAssociationsMixin; + addWarehouse: Sequelize.BelongsToManyAddAssociationMixin; + createWarehouse: Sequelize.BelongsToManyCreateAssociationMixin; + removeWarehouse: Sequelize.BelongsToManyRemoveAssociationMixin; + removeWarehouses: Sequelize.BelongsToManyRemoveAssociationsMixin; + hasWarehouse: Sequelize.BelongsToManyHasAssociationMixin; + hasWarehouses: Sequelize.BelongsToManyHasAssociationsMixin; + countWarehouses: Sequelize.BelongsToManyCountAssociationsMixin; + + // belongsToMany association mixins: + getCustomers: Sequelize.BelongsToManyGetAssociationsMixin; + setCustomers: Sequelize.BelongsToManySetAssociationsMixin; + addCustomers: Sequelize.BelongsToManyAddAssociationsMixin; + addCustomer: Sequelize.BelongsToManyAddAssociationMixin; + createCustomer: Sequelize.BelongsToManyCreateAssociationMixin; + removeCustomer: Sequelize.BelongsToManyRemoveAssociationMixin; + removeCustomers: Sequelize.BelongsToManyRemoveAssociationsMixin; + hasCustomer: Sequelize.BelongsToManyHasAssociationMixin; + hasCustomers: Sequelize.BelongsToManyHasAssociationsMixin; + countCustomers: Sequelize.BelongsToManyCountAssociationsMixin; +}; + +interface WarehouseBranchAttributes { + distance?: number; +}; + +interface WarehouseBranchInstance extends Sequelize.Instance, WarehouseBranchAttributes { }; + +interface CustomerAttributes { + id?: number; + fullname?: string; + credit?: number; +}; + +interface CustomerInstance extends Sequelize.Instance, CustomerAttributes { + // belongsToMany association mixins: + getBranches: Sequelize.BelongsToManyGetAssociationsMixin; + setBranches: Sequelize.BelongsToManySetAssociationsMixin; + addBranches: Sequelize.BelongsToManyAddAssociationsMixin; + addBranch: Sequelize.BelongsToManyAddAssociationMixin; + createBranch: Sequelize.BelongsToManyCreateAssociationMixin; + removeBranch: Sequelize.BelongsToManyRemoveAssociationMixin; + removeBranches: Sequelize.BelongsToManyRemoveAssociationsMixin; + hasBranch: Sequelize.BelongsToManyHasAssociationMixin; + hasBranches: Sequelize.BelongsToManyHasAssociationsMixin; + countBranches: Sequelize.BelongsToManyCountAssociationsMixin; +}; + // // DataTypes // ~~~~~~~~~~~ diff --git a/sequelize/sequelize.d.ts b/sequelize/sequelize.d.ts index ba8d03d94..a0a567eca 100644 --- a/sequelize/sequelize.d.ts +++ b/sequelize/sequelize.d.ts @@ -22,23 +22,26 @@ declare module "sequelize" { /** - * The options for the get mixin of the BelongsTo association. - * @see BelongsToAssociationGetMixin + * The options for the getAssociation mixin of the belongsTo association. + * @see BelongsToGetAssociationMixin */ - interface BelongsToAssociationGetMixinOptions { - /** - * Apply a scope on the related model, or remove its default scope by passing false. - */ - scope: string | boolean; + interface BelongsToGetAssociationMixinOptions { + /** + * Apply a scope on the related model, or remove its default scope by passing false. + */ + scope?: string | boolean; } /** - * The get association mixin applied to models with BelongsTo. + * The getAssociation mixin applied to models with belongsTo. * An example of usage is as follows: * * ```js + * + * User.belongsTo(Role); + * * interface UserInstance extends Sequelize.Instance, UserAttrib { - * getRole: Sequelize.BelongsToAssociationGetMixin; + * getRole: Sequelize.BelongsToGetAssociationMixin; * // setRole... * // createRole... * } @@ -47,33 +50,36 @@ declare module "sequelize" { * @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to/ * @see Instance */ - interface BelongsToAssociationGetMixin { - /** - * Get the associated instance. - * @param options The obtions to use when getting the association. - */ - (options?: BelongsToAssociationGetMixinOptions): Promise + interface BelongsToGetAssociationMixin { + /** + * Get the associated instance. + * @param options The options to use when getting the association. + */ + (options?: BelongsToGetAssociationMixinOptions): Promise } /** - * The options for the set mixin of the BelongsTo association. - * @see BelongsToAssociationSetMixin + * The options for the setAssociation mixin of the belongsTo association. + * @see BelongsToSetAssociationMixin */ - interface BelongsToAssociationSetMixinOptions { - /** - * Skip saving this after setting the foreign key if false. - */ - save: boolean; + interface BelongsToSetAssociationMixinOptions { + /** + * Skip saving this after setting the foreign key if false. + */ + save?: boolean; } /** - * The set association mixin applied to models with BelongsTo. + * The setAssociation mixin applied to models with belongsTo. * An example of usage is as follows: * * ```js + * + * User.belongsTo(Role); + * * interface UserInstance extends Sequelize.Instance, UserAttributes { * // getRole... - * setRole: BelongsToAssociationSetMixin; + * setRole: Sequelize.BelongsToSetAssociationMixin; * // createRole... * } * ``` @@ -81,48 +87,1093 @@ declare module "sequelize" { * @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to/ * @see Instance */ - interface BelongsToAssociationSetMixin { - /** - * Get 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 obtions to use when setting the association. - */ - (newAssociation: TInstance | TInstancePrimaryKey, options?: BelongsToAssociationSetMixinOptions): Promise + interface BelongsToSetAssociationMixin { + /** + * 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 } /** - * The options for the create mixin of the BelongsTo association. - * @see BelongsToAssociationCreateMixin + * The options for the createAssociation mixin of the belongsTo association. + * @see BelongsToCreateAssociationMixin */ - interface BelongsToAssociationCreateMixinOptions extends CreateOptions, BelongsToAssociationSetMixinOptions {} + interface BelongsToCreateAssociationMixinOptions { } /** - * The create association mixin applied to models with BelongsTo. + * The createAssociation mixin applied to models with belongsTo. * An example of usage is as follows: * * ```js + * + * User.belongsTo(Role); + * * interface UserInstance extends Sequelize.Instance, UserAttributes { * // getRole... * // setRole... - * createRole: BelongsToAssociationCreateMixin; + * createRole: Sequelize.BelongsToCreateAssociationMixin; * } * ``` * * @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to/ * @see Instance */ - interface BelongsToAssociationCreateMixin { - /** - * 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?: BelongsToAssociationCreateMixinOptions): Promise + interface BelongsToCreateAssociationMixin { + /** + * 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 } - // TODO: HasOne Associations - // TODO: HasMany Associations - // TODO: BelongsToMany Associations + /** + * 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; + } + + /** + * The getAssociation mixin applied to models with hasOne. + * An example of usage is as follows: + * + * ```js + * + * User.hasOne(Role); + * + * interface UserInstance extends Sequelize.Instance, UserAttrib { + * getRole: Sequelize.HasOneGetAssociationMixin; + * // setRole... + * // createRole... + * } + * ``` + * + * @see http://docs.sequelizejs.com/en/latest/api/associations/has-one/ + * @see Instance + */ + interface HasOneGetAssociationMixin { + /** + * Get the associated instance. + * @param options The options to use when getting the association. + */ + (options?: HasOneGetAssociationMixinOptions): Promise + } + + /** + * 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; + } + + /** + * The setAssociation mixin applied to models with hasOne. + * An example of usage is as follows: + * + * ```js + * + * User.hasOne(Role); + * + * interface UserInstance extends Sequelize.Instance, UserAttributes { + * // getRole... + * setRole: Sequelize.HasOneSetAssociationMixin; + * // createRole... + * } + * ``` + * + * @see http://docs.sequelizejs.com/en/latest/api/associations/has-one/ + * @see Instance + */ + interface HasOneSetAssociationMixin { + /** + * 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 + } + + /** + * 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, UserAttributes { + * // getRole... + * // setRole... + * createRole: Sequelize.HasOneCreateAssociationMixin; + * } + * ``` + * + * @see http://docs.sequelizejs.com/en/latest/api/associations/has-one/ + * @see Instance + */ + interface HasOneCreateAssociationMixin { + /** + * 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 + } + + /** + * The options for the getAssociations mixin of the hasMany association. + * @see HasManyGetAssociationsMixin + */ + interface HasManyGetAssociationsMixinOptions { + + /** + * 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 hasMany. + * An example of usage is as follows: + * + * ```js + * + * User.hasMany(Role); + * + * interface UserInstance extends Sequelize.Instance, UserAttributes { + * getRoles: Sequelize.HasManyGetAssociationsMixin; + * // setRoles... + * // addRoles... + * // addRole... + * // createRole... + * // removeRole... + * // removeRoles... + * // hasRole... + * // hasRoles... + * // countRoles... + * } + * ``` + * + * @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/ + * @see Instance + */ + interface HasManyGetAssociationsMixin { + /** + * Get everything currently associated with this, using an optional where clause. + * @param options The options to use when getting the associations. + */ + (options?: HasManyGetAssociationsMixinOptions): Promise + } + + /** + * The options for the setAssociations mixin of the hasMany association. + * @see HasManySetAssociationsMixin + */ + interface HasManySetAssociationsMixinOptions { + + /** + * Run validation for the join model. + */ + validate?: boolean; + } + + /** + * The setAssociations mixin applied to models with hasMany. + * An example of usage is as follows: + * + * ```js + * + * User.hasMany(Role); + * + * interface UserInstance extends Sequelize.Instance, UserAttributes { + * // getRoles... + * setRoles: Sequelize.HasManySetAssociationsMixin; + * // addRoles... + * // addRole... + * // createRole... + * // removeRole... + * // removeRoles... + * // hasRole... + * // hasRoles... + * // countRoles... + * } + * ``` + * + * @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/ + * @see Instance + */ + interface HasManySetAssociationsMixin { + /** + * 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, + options?: HasManySetAssociationsMixinOptions | FindOptions | InstanceUpdateOptions + ): Promise + } + + /** + * The options for the addAssociations mixin of the hasMany association. + * @see HasManyAddAssociationsMixin + */ + interface HasManyAddAssociationsMixinOptions { + + /** + * Run validation for the join model. + */ + validate?: boolean; + } + + /** + * The addAssociations mixin applied to models with hasMany. + * An example of usage is as follows: + * + * ```js + * + * User.hasMany(Role); + * + * interface UserInstance extends Sequelize.Instance, UserAttributes { + * // getRoles... + * // setRoles... + * addRoles: Sequelize.HasManyAddAssociationsMixin; + * // addRole... + * // createRole... + * // removeRole... + * // removeRoles... + * // hasRole... + * // hasRoles... + * // countRoles... + * } + * ``` + * + * @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/ + * @see Instance + */ + interface HasManyAddAssociationsMixin { + /** + * 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, + options?: HasManyAddAssociationsMixinOptions | InstanceUpdateOptions + ): Promise + } + + /** + * The options for the addAssociation mixin of the hasMany association. + * @see HasManyAddAssociationMixin + */ + interface HasManyAddAssociationMixinOptions { + + /** + * Run validation for the join model. + */ + validate?: boolean; + } + + /** + * The addAssociation mixin applied to models with hasMany. + * An example of usage is as follows: + * + * ```js + * + * User.hasMany(Role); + * + * interface UserInstance extends Sequelize.Instance, UserAttributes { + * // getRoles... + * // setRoles... + * // addRoles... + * addRole: Sequelize.HasManyAddAssociationMixin; + * // createRole... + * // removeRole... + * // removeRoles... + * // hasRole... + * // hasRoles... + * // countRoles... + * } + * ``` + * + * @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/ + * @see Instance + */ + interface HasManyAddAssociationMixin { + /** + * 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 + } + + /** + * 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, UserAttributes { + * // getRoles... + * // setRoles... + * // addRoles... + * // addRole... + * createRole: Sequelize.HasManyCreateAssociationMixin; + * // removeRole... + * // removeRoles... + * // hasRole... + * // hasRoles... + * // countRoles... + * } + * ``` + * + * @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/ + * @see Instance + */ + interface HasManyCreateAssociationMixin { + /** + * 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 + } + + /** + * 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, UserAttributes { + * // getRoles... + * // setRoles... + * // addRoles... + * // addRole... + * // createRole... + * removeRole: Sequelize.HasManyRemoveAssociationMixin; + * // removeRoles... + * // hasRole... + * // hasRoles... + * // countRoles... + * } + * ``` + * + * @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/ + * @see Instance + */ + interface HasManyRemoveAssociationMixin { + /** + * 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 + } + + /** + * 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, UserAttributes { + * // getRoles... + * // setRoles... + * // addRoles... + * // addRole... + * // createRole... + * // removeRole... + * removeRoles: Sequelize.HasManyRemoveAssociationsMixin; + * // hasRole... + * // hasRoles... + * // countRoles... + * } + * ``` + * + * @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/ + * @see Instance + */ + interface HasManyRemoveAssociationsMixin { + /** + * 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, + options?: HasManyRemoveAssociationsMixinOptions | InstanceUpdateOptions + ): Promise + } + + /** + * 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, UserAttributes { + * // getRoles... + * // setRoles... + * // addRoles... + * // addRole... + * // createRole... + * // removeRole... + * // removeRoles... + * hasRole: Sequelize.HasManyHasAssociationMixin; + * // hasRoles... + * // countRoles... + * } + * ``` + * + * @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/ + * @see Instance + */ + interface HasManyHasAssociationMixin { + /** + * 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 + } + + /** + * 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, UserAttributes { + * // getRoles... + * // setRoles... + * // addRoles... + * // addRole... + * // createRole... + * // removeRole... + * // removeRoles + * // hasRole... + * hasRoles: Sequelize.HasManyHasAssociationsMixin; + * // countRoles... + * } + * ``` + * + * @see http://docs.sequelizejs.com/en/latest/api/associations/has-many/ + * @see Instance + */ + interface HasManyHasAssociationsMixin { + /** + * 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, + options?: HasManyHasAssociationsMixinOptions | HasManyGetAssociationsMixinOptions + ): Promise + } + + /** + * The options for the countAssociations mixin of the hasMany association. + * @see HasManyCountAssociationsMixin + */ + interface HasManyCountAssociationsMixinOptions { + + /** + * 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 hasMany. + * An example of usage is as follows: + * + * ```js + * + * User.hasMany(Role); + * + * interface UserInstance extends Sequelize.Instance, 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 + } + + /** + * 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, UserAttributes { + * getRoles: Sequelize.BelongsToManyGetAssociationsMixin; + * // 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 { + /** + * Get everything currently associated with this, using an optional where clause. + * @param options The options to use when getting the associations. + */ + (options?: BelongsToManyGetAssociationsMixinOptions): Promise + } + + /** + * 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, UserAttributes { + * // getRoles... + * setRoles: Sequelize.BelongsToManySetAssociationsMixin; + * // addRoles... + * // addRole... + * // createRole... + * // removeRole... + * // removeRoles... + * // hasRole... + * // hasRoles... + * // countRoles... + * } + * ``` + * + * @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to-many/ + * @see Instance + */ + interface BelongsToManySetAssociationsMixin { + /** + * 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, + options?: BelongsToManySetAssociationsMixinOptions | FindOptions | BulkCreateOptions | InstanceUpdateOptions | InstanceDestroyOptions | TJoinTableAttributes + ): Promise + } + + /** + * 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, UserAttributes { + * // getRoles... + * // setRoles... + * addRoles: Sequelize.BelongsToManyAddAssociationsMixin; + * // addRole... + * // createRole... + * // removeRole... + * // removeRoles... + * // hasRole... + * // hasRoles... + * // countRoles... + * } + * ``` + * + * @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to-many/ + * @see Instance + */ + interface BelongsToManyAddAssociationsMixin { + /** + * 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, + options?: BelongsToManyAddAssociationsMixinOptions | FindOptions | BulkCreateOptions | InstanceUpdateOptions | InstanceDestroyOptions | TJoinTableAttributes + ): Promise + } + + /** + * 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, UserAttributes { + * // getRoles... + * // setRoles... + * // addRoles... + * addRole: Sequelize.BelongsToManyAddAssociationMixin; + * // createRole... + * // removeRole... + * // removeRoles... + * // hasRole... + * // hasRoles... + * // countRoles... + * } + * ``` + * + * @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to-many/ + * @see Instance + */ + interface BelongsToManyAddAssociationMixin { + /** + * 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 + } + + /** + * 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, UserAttributes { + * // getRoles... + * // setRoles... + * // addRoles... + * // addRole... + * createRole: Sequelize.BelongsToManyCreateAssociationMixin; + * // removeRole... + * // removeRoles... + * // hasRole... + * // hasRoles... + * // countRoles... + * } + * ``` + * + * @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to-many/ + * @see Instance + */ + interface BelongsToManyCreateAssociationMixin { + /** + * 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 + } + + /** + * 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, UserAttributes { + * // getRoles... + * // setRoles... + * // addRoles... + * // addRole... + * // createRole... + * removeRole: Sequelize.BelongsToManyRemoveAssociationMixin; + * // removeRoles... + * // hasRole... + * // hasRoles... + * // countRoles... + * } + * ``` + * + * @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to-many/ + * @see Instance + */ + interface BelongsToManyRemoveAssociationMixin { + /** + * 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 + } + + /** + * 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, UserAttributes { + * // getRoles... + * // setRoles... + * // addRoles... + * // addRole... + * // createRole... + * // removeRole... + * removeRoles: Sequelize.BelongsToManyRemoveAssociationsMixin; + * // hasRole... + * // hasRoles... + * // countRoles... + * } + * ``` + * + * @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to-many/ + * @see Instance + */ + interface BelongsToManyRemoveAssociationsMixin { + /** + * 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, + options?: BelongsToManyRemoveAssociationsMixinOptions | InstanceDestroyOptions + ): Promise + } + + /** + * 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, UserAttributes { + * // getRoles... + * // setRoles... + * // addRoles... + * // addRole... + * // createRole... + * // removeRole... + * // removeRoles... + * hasRole: Sequelize.BelongsToManyHasAssociationMixin; + * // hasRoles... + * // countRoles... + * } + * ``` + * + * @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to-many/ + * @see Instance + */ + interface BelongsToManyHasAssociationMixin { + /** + * 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 + } + + /** + * 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, UserAttributes { + * // getRoles... + * // setRoles... + * // addRoles... + * // addRole... + * // createRole... + * // removeRole... + * // removeRoles + * // hasRole... + * hasRoles: Sequelize.BelongsToManyHasAssociationsMixin; + * // countRoles... + * } + * ``` + * + * @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to-many/ + * @see Instance + */ + interface BelongsToManyHasAssociationsMixin { + /** + * 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, + options?: BelongsToManyHasAssociationsMixinOptions | BelongsToManyGetAssociationsMixinOptions + ): Promise + } + + /** + * 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, 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 + } /** * Foreign Key Options