不要怂,就是干,撸起袖子干!

Commit e3e4b31f by Mick Hansen

chore(associations): through -> through.model

1 parent 826d0407
...@@ -30,11 +30,11 @@ module.exports = (function() { ...@@ -30,11 +30,11 @@ module.exports = (function() {
options.where = new Utils.and([ options.where = new Utils.and([
new Utils.where( new Utils.where(
through.rawAttributes[self.association.identifier], through.model.rawAttributes[self.association.identifier],
self.instance[instancePrimaryKey] self.instance[instancePrimaryKey]
), ),
new Utils.where( new Utils.where(
through.rawAttributes[self.association.foreignIdentifier], through.model.rawAttributes[self.association.foreignIdentifier],
{ {
join: new Utils.literal([ join: new Utils.literal([
self.QueryInterface.quoteTable(self.association.target.name), self.QueryInterface.quoteTable(self.association.target.name),
...@@ -46,9 +46,9 @@ module.exports = (function() { ...@@ -46,9 +46,9 @@ module.exports = (function() {
options.where options.where
]); ]);
if (Object(targetAssociation.through) === targetAssociation.through) { if (Object(targetAssociation.through.model) === targetAssociation.through.model) {
queryOptions.hasJoinTableModel = true; queryOptions.hasJoinTableModel = true;
queryOptions.joinTableModel = through; queryOptions.joinTableModel = through.model;
if (!options.attributes) { if (!options.attributes) {
options.attributes = [ options.attributes = [
...@@ -59,21 +59,21 @@ module.exports = (function() { ...@@ -59,21 +59,21 @@ module.exports = (function() {
if (options.joinTableAttributes) { if (options.joinTableAttributes) {
options.joinTableAttributes.forEach(function(elem) { options.joinTableAttributes.forEach(function(elem) {
options.attributes.push( options.attributes.push(
self.QueryInterface.quoteTable(through.name) + '.' + self.QueryInterface.quoteIdentifier(elem) + ' as ' + self.QueryInterface.quoteTable(through.model.name) + '.' + self.QueryInterface.quoteIdentifier(elem) + ' as ' +
self.QueryInterface.quoteIdentifier(through.name + '.' + elem, true) self.QueryInterface.quoteIdentifier(through.model.name + '.' + elem, true)
); );
}); });
} else { } else {
Utils._.forOwn(through.rawAttributes, function(elem, key) { Utils._.forOwn(through.model.rawAttributes, function(elem, key) {
options.attributes.push( options.attributes.push(
self.QueryInterface.quoteTable(through.name) + '.' + self.QueryInterface.quoteIdentifier(key) + ' as ' + self.QueryInterface.quoteTable(through.model.name) + '.' + self.QueryInterface.quoteIdentifier(key) + ' as ' +
self.QueryInterface.quoteIdentifier(through.name + '.' + key, true) self.QueryInterface.quoteIdentifier(through.model.name + '.' + key, true)
); );
}); });
} }
} }
return self.association.target.findAllJoin([through.getTableName(), through.name], options, queryOptions); return self.association.target.findAllJoin([through.model.getTableName(), through.model.name], options, queryOptions);
}; };
HasManyDoubleLinked.prototype.injectSetter = function(oldAssociations, newAssociations, defaultAttributes) { HasManyDoubleLinked.prototype.injectSetter = function(oldAssociations, newAssociations, defaultAttributes) {
...@@ -105,10 +105,10 @@ module.exports = (function() { ...@@ -105,10 +105,10 @@ module.exports = (function() {
if (!newObj) { if (!newObj) {
obsoleteAssociations.push(old); obsoleteAssociations.push(old);
} else if (Object(targetAssociation.through) === targetAssociation.through) { } else if (Object(targetAssociation.through.model) === targetAssociation.through.model) {
var throughAttributes = newObj[self.association.through.name]; var throughAttributes = newObj[self.association.through.model.name];
// Quick-fix for subtle bug when using existing objects that might have the through model attached (not as an attribute object) // Quick-fix for subtle bug when using existing objects that might have the through model attached (not as an attribute object)
if (throughAttributes instanceof self.association.through.DAO) { if (throughAttributes instanceof self.association.through.model.Instance) {
throughAttributes = {}; throughAttributes = {};
} }
...@@ -136,7 +136,7 @@ module.exports = (function() { ...@@ -136,7 +136,7 @@ module.exports = (function() {
where[self.association.identifier] = ((sourceKeys.length === 1) ? self.instance[sourceKeys[0]] : self.instance.id); where[self.association.identifier] = ((sourceKeys.length === 1) ? self.instance[sourceKeys[0]] : self.instance.id);
where[foreignIdentifier] = foreignIds; where[foreignIdentifier] = foreignIds;
promises.push(self.association.through.destroy(where, options)); promises.push(self.association.through.model.destroy(where, options));
} }
if (unassociatedObjects.length > 0) { if (unassociatedObjects.length > 0) {
...@@ -146,19 +146,19 @@ module.exports = (function() { ...@@ -146,19 +146,19 @@ module.exports = (function() {
attributes[self.association.identifier] = ((sourceKeys.length === 1) ? self.instance[sourceKeys[0]] : self.instance.id); attributes[self.association.identifier] = ((sourceKeys.length === 1) ? self.instance[sourceKeys[0]] : self.instance.id);
attributes[foreignIdentifier] = ((targetKeys.length === 1) ? unassociatedObject[targetKeys[0]] : unassociatedObject.id); attributes[foreignIdentifier] = ((targetKeys.length === 1) ? unassociatedObject[targetKeys[0]] : unassociatedObject.id);
if (Object(targetAssociation.through) === targetAssociation.through) { if (Object(targetAssociation.through.model) === targetAssociation.through.model) {
attributes = Utils._.defaults(attributes, unassociatedObject[targetAssociation.through.name], defaultAttributes); attributes = Utils._.defaults(attributes, unassociatedObject[targetAssociation.through.model.name], defaultAttributes);
} }
return attributes; return attributes;
}); });
promises.push(self.association.through.bulkCreate(bulk, options)); promises.push(self.association.through.model.bulkCreate(bulk, options));
} }
if (changedAssociations.length > 0) { if (changedAssociations.length > 0) {
changedAssociations.forEach(function(assoc) { changedAssociations.forEach(function(assoc) {
promises.push(self.association.through.update(assoc.attributes, assoc.where, options)); promises.push(self.association.through.model.update(assoc.attributes, assoc.where, options));
}); });
} }
...@@ -183,17 +183,17 @@ module.exports = (function() { ...@@ -183,17 +183,17 @@ module.exports = (function() {
if (exists) { if (exists) {
var where = attributes; var where = attributes;
attributes = Utils._.defaults({}, newAssociation[targetAssociation.through.name], additionalAttributes); attributes = Utils._.defaults({}, newAssociation[targetAssociation.through.model.name], additionalAttributes);
if (Object.keys(attributes).length) { if (Object.keys(attributes).length) {
return targetAssociation.through.update(attributes, where, options); return targetAssociation.through.model.update(attributes, where, options);
} else { } else {
return Utils.Promise.resolve(); return Utils.Promise.resolve();
} }
} else { } else {
attributes = Utils._.defaults(attributes, newAssociation[targetAssociation.through.name], additionalAttributes); attributes = Utils._.defaults(attributes, newAssociation[targetAssociation.through.model.name], additionalAttributes);
return this.association.through.create(attributes, options); return this.association.through.model.create(attributes, options);
} }
}; };
......
...@@ -230,6 +230,7 @@ Mixin.belongsTo = singleLinked(BelongsTo); ...@@ -230,6 +230,7 @@ Mixin.belongsTo = singleLinked(BelongsTo);
* @param {Model|string|object} [options.through] 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. * @param {Model|string|object} [options.through] 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.
* @param {Model} [options.through.model] The model used to join both sides of the N:M association. * @param {Model} [options.through.model] The model used to join both sides of the N:M association.
* @param {object} [options.through.scope] 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) * @param {object} [options.through.scope] 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)
* @param {boolean} [options.through.unique=true] 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)
* @param {string|object} [options.as] The alias of this model. If you provide a string, it should be plural, and will be singularized using node.inflection. If you want to control the singular version yourself, provide an object with `plural` and `singular` keys. 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 pluralized name of target * @param {string|object} [options.as] The alias of this model. If you provide a string, it should be plural, and will be singularized using node.inflection. If you want to control the singular version yourself, provide an object with `plural` and `singular` keys. 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 pluralized name of target
* @param {string|object} [options.foreignKey] The name of the foreign key in the target table / join 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 colum. Defaults to the name of source + primary key of source * @param {string|object} [options.foreignKey] The name of the foreign key in the target table / join 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 colum. Defaults to the name of source + primary key of source
* @param {object} [options.scope] A key/value set that will be used for association create and find defaults on the target. (sqlite not supported for N:M) * @param {object} [options.scope] A key/value set that will be used for association create and find defaults on the target. (sqlite not supported for N:M)
......
...@@ -885,7 +885,6 @@ module.exports = (function() { ...@@ -885,7 +885,6 @@ module.exports = (function() {
// Generate join SQL for left side of through // Generate join SQL for left side of through
joinQueryItem += joinType + self.quoteTable(throughTable, throughAs) + ' ON '; joinQueryItem += joinType + self.quoteTable(throughTable, throughAs) + ' ON ';
joinQueryItem += sourceJoinOn; joinQueryItem += sourceJoinOn;
// Generate join SQL for right side of through // Generate join SQL for right side of through
joinQueryItem += joinType + self.quoteTable(table, as) + ' ON '; joinQueryItem += joinType + self.quoteTable(table, as) + ' ON ';
...@@ -1405,14 +1404,14 @@ module.exports = (function() { ...@@ -1405,14 +1404,14 @@ module.exports = (function() {
joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.target.tableName); joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.target.tableName);
joins += ' ON ' + self.quoteIdentifiers(association.source.tableName + '.' + association.identifier); joins += ' ON ' + self.quoteIdentifiers(association.source.tableName + '.' + association.identifier);
joins += ' = ' + self.quoteIdentifiers(association.target.tableName + '.' + association.target.autoIncrementField); joins += ' = ' + self.quoteIdentifiers(association.target.tableName + '.' + association.target.autoIncrementField);
} else if (Object(association.through) === association.through) { } else if (Object(association.through.model) === association.through.model) {
joinedTables[association.through.tableName] = true; joinedTables[association.through.model.tableName] = true;
joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.through.tableName); joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.through.model.tableName);
joins += ' ON ' + self.quoteIdentifiers(association.source.tableName + '.' + association.source.autoIncrementField); joins += ' ON ' + self.quoteIdentifiers(association.source.tableName + '.' + association.source.autoIncrementField);
joins += ' = ' + self.quoteIdentifiers(association.through.tableName + '.' + association.identifier); joins += ' = ' + self.quoteIdentifiers(association.through.model.tableName + '.' + association.identifier);
joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.target.tableName); joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.target.tableName);
joins += ' ON ' + self.quoteIdentifiers(association.through.tableName + '.' + association.foreignIdentifier); joins += ' ON ' + self.quoteIdentifiers(association.through.model.tableName + '.' + association.foreignIdentifier);
joins += ' = ' + self.quoteIdentifiers(association.target.tableName + '.' + association.target.autoIncrementField); joins += ' = ' + self.quoteIdentifiers(association.target.tableName + '.' + association.target.autoIncrementField);
} else { } else {
joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.target.tableName); joins += ' LEFT JOIN ' + self.quoteIdentifiers(association.target.tableName);
......
...@@ -1762,13 +1762,13 @@ module.exports = (function() { ...@@ -1762,13 +1762,13 @@ module.exports = (function() {
include.as = association.as; include.as = association.as;
// If through, we create a pseudo child include, to ease our parsing later on // If through, we create a pseudo child include, to ease our parsing later on
if (Object(include.association.through) === include.association.through) { if (include.association.through && Object(include.association.through.model) === include.association.through.model) {
if (!include.include) include.include = []; if (!include.include) include.include = [];
var through = include.association.through; var through = include.association.through;
include.through = Utils._.defaults(include.through || {}, { include.through = Utils._.defaults(include.through || {}, {
model: through, model: through.model,
as: Utils.singularize(through.tableName), as: Utils.singularize(through.model.tableName),
association: { association: {
isSingleAssociation: true isSingleAssociation: true
}, },
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!