diff --git a/sequelize/sequelize-tests.ts b/sequelize/sequelize-tests.ts index ae6fd7af4..12089371b 100644 --- a/sequelize/sequelize-tests.ts +++ b/sequelize/sequelize-tests.ts @@ -151,6 +151,153 @@ 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(() => { }); + +// TODO: hasMany + +// TODO: belongsToMany + +// TODO: belongsToMany + +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 { + +}; + +interface BranchAttributes { + +}; + +interface BranchInstance extends Sequelize.Instance, BranchAttributes { + +}; + +interface WarehouseBranchAttributes { + +}; + +interface WarehouseBranchInstance extends Sequelize.Instance, WarehouseBranchAttributes { }; + +interface CustomerAttributes { + +}; + +interface CustomerInstance extends Sequelize.Instance, CustomerAttributes { + +}; + + + // // DataTypes // ~~~~~~~~~~~ diff --git a/sequelize/sequelize.d.ts b/sequelize/sequelize.d.ts index ba8d03d94..d7cbe2158 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. - */ + 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. - */ + 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,46 +87,157 @@ 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 to use when setting the association. + */ + (newAssociation?: TInstance | TInstancePrimaryKey, options?: BelongsToSetAssociationMixinOptions): 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 extends CreateOptions, BelongsToSetAssociationMixinOptions { } /** - * 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): Promise } - // TODO: HasOne 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 to use when setting the association. + */ + (newAssociation?: TInstance | TInstancePrimaryKey, options?: HasOneSetAssociationMixinOptions): Promise + } + + /** + * The options for the createAssociation mixin of the hasOne association. + * @see HasOneCreateAssociationMixin + */ + interface HasOneCreateAssociationMixinOptions extends CreateOptions, HasOneSetAssociationMixinOptions { } + + /** + * 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): Promise + } + + // TODO: HasMany Associations // TODO: BelongsToMany Associations