Added has-one + tests and improved naming and code format [Few Breaking Changes]

This commit is contained in:
Ali Taheri
2015-10-06 11:58:13 +03:30
parent 05557455c4
commit 4f278188db
2 changed files with 306 additions and 42 deletions
+147
View File
@@ -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<ProductInstance, ProductAttributes>('product', {});
var product = Product.build();
var Barcode = s.define<BarcodeInstance, BarcodeAttributes>('barcode', {});
var barcode = Barcode.build();
var Warehouse = s.define<WarehouseInstance, WarehouseAttributes>('warehouse', {});
var warehouse = Warehouse.build();
var Branch = s.define<BranchInstance, BranchAttributes>('brach', {});
var branch = Branch.build();
var WarehouseBranch = s.define<WarehouseBranchInstance, WarehouseBranchAttributes>('warehouseBranch', {});
var Customer = s.define<CustomerInstance, CustomerAttributes>('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 <Model>
// TODO: belongsToMany <void>
interface ProductAttributes {
id?: number;
name?: string;
price?: number;
};
interface ProductInstance extends Sequelize.Instance<ProductInstance, ProductAttributes>, ProductAttributes {
// hasOne association mixins:
getBarcode: Sequelize.HasOneGetAssociationMixin<BarcodeInstance>;
setBarcode: Sequelize.HasOneSetAssociationMixin<BarcodeInstance, number>;
createBarcode: Sequelize.HasOneCreateAssociationMixin<BarcodeAttributes>;
// belongsTo association mixins:
getWarehouse: Sequelize.BelongsToGetAssociationMixin<WarehouseInstance>;
setWarehouse: Sequelize.BelongsToSetAssociationMixin<WarehouseInstance, number>;
createWarehouse: Sequelize.BelongsToCreateAssociationMixin<WarehouseAttributes>;
};
interface BarcodeAttributes {
id?: number;
code?: string;
dateIssued?: Date;
};
interface BarcodeInstance extends Sequelize.Instance<BarcodeInstance, BarcodeAttributes>, BarcodeAttributes {
// belongsTo association mixins:
getProduct: Sequelize.BelongsToGetAssociationMixin<ProductInstance>;
setProduct: Sequelize.BelongsToSetAssociationMixin<ProductInstance, number>;
createProduct: Sequelize.BelongsToCreateAssociationMixin<ProductAttributes>;
};
interface WarehouseAttributes {
id?: number;
address?: string;
capacity?: number;
};
interface WarehouseInstance extends Sequelize.Instance<WarehouseInstance, WarehouseAttributes>, WarehouseAttributes {
};
interface BranchAttributes {
};
interface BranchInstance extends Sequelize.Instance<BranchInstance, BranchAttributes>, BranchAttributes {
};
interface WarehouseBranchAttributes {
};
interface WarehouseBranchInstance extends Sequelize.Instance<WarehouseBranchInstance, WarehouseBranchAttributes>, WarehouseBranchAttributes { };
interface CustomerAttributes {
};
interface CustomerInstance extends Sequelize.Instance<CustomerInstance, CustomerAttributes>, CustomerAttributes {
};
//
// DataTypes
// ~~~~~~~~~~~
+159 -42
View File
@@ -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<UserInstance, UserAttrib>, UserAttrib {
* getRole: Sequelize.BelongsToAssociationGetMixin<RoleInstance>;
* getRole: Sequelize.BelongsToGetAssociationMixin<RoleInstance>;
* // setRole...
* // createRole...
* }
@@ -47,33 +50,36 @@ declare module "sequelize" {
* @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to/
* @see Instance
*/
interface BelongsToAssociationGetMixin<TInstance> {
/**
* Get the associated instance.
* @param options The obtions to use when getting the association.
*/
(options?: BelongsToAssociationGetMixinOptions): Promise<TInstance>
interface BelongsToGetAssociationMixin<TInstance> {
/**
* Get the associated instance.
* @param options The options to use when getting the association.
*/
(options?: BelongsToGetAssociationMixinOptions): Promise<TInstance>
}
/**
* 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<UserInstance, UserAttributes>, UserAttributes {
* // getRole...
* setRole: BelongsToAssociationSetMixin<RoleInstance, RoleId>;
* setRole: Sequelize.BelongsToSetAssociationMixin<RoleInstance, RoleId>;
* // createRole...
* }
* ```
@@ -81,46 +87,157 @@ declare module "sequelize" {
* @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to/
* @see Instance
*/
interface BelongsToAssociationSetMixin<TInstance, TInstancePrimaryKey> {
/**
* 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<void>
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 to use when setting the association.
*/
(newAssociation?: TInstance | TInstancePrimaryKey, options?: BelongsToSetAssociationMixinOptions): Promise<void>
}
/**
* 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<UserInstance, UserAttributes>, UserAttributes {
* // getRole...
* // setRole...
* createRole: BelongsToAssociationCreateMixin<RoleAttributes>;
* createRole: Sequelize.BelongsToCreateAssociationMixin<RoleAttributes>;
* }
* ```
*
* @see http://docs.sequelizejs.com/en/latest/api/associations/belongs-to/
* @see Instance
*/
interface BelongsToAssociationCreateMixin<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?: BelongsToAssociationCreateMixinOptions): Promise<void>
interface BelongsToCreateAssociationMixin<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?: BelongsToCreateAssociationMixinOptions): Promise<void>
}
// 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<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>
}
/**
* 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<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 to use when setting the association.
*/
(newAssociation?: TInstance | TInstancePrimaryKey, options?: HasOneSetAssociationMixinOptions): Promise<void>
}
/**
* 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<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): Promise<void>
}
// TODO: HasMany Associations
// TODO: BelongsToMany Associations