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

Commit 97d0d789 by Mick Hansen

feat(associations): add a scope property that is used for default values in crea…

…teAssociation/setAssociations/addAssociation
1 parent 481d5071
......@@ -14,6 +14,7 @@ module.exports = (function() {
this.source = source;
this.target = target;
this.options = options;
this.scope = options.scope;
this.isSingleAssociation = true;
this.isSelfAssociation = (this.source === this.target);
this.as = this.options.as;
......
......@@ -5,7 +5,6 @@ var Utils = require('./../utils')
module.exports = (function() {
var HasManySingleLinked = function(association, instance) {
this.__factory = association;
this.association = association;
this.instance = instance;
this.target = this.association.target;
......@@ -52,22 +51,22 @@ module.exports = (function() {
if (obsoleteAssociations.length > 0) {
// clear the old associations
var obsoleteIds = obsoleteAssociations.map(function(associatedObject) {
associatedObject[self.__factory.identifier] = (newAssociations.length < 1 ? null : self.instance.id);
associatedObject[self.association.identifier] = (newAssociations.length < 1 ? null : self.instance.id);
return associatedObject[associationKey];
});
update = {};
update[self.__factory.identifier] = null;
update[self.association.identifier] = null;
primaryKeys = Object.keys(this.__factory.target.primaryKeys);
primaryKeys = Object.keys(this.association.target.primaryKeys);
primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id';
updateWhere = {};
updateWhere[primaryKey] = obsoleteIds;
promises.push(this.__factory.target.update(
promises.push(this.association.target.update(
update,
updateWhere,
Utils._.extend(options, { allowNull: [self.__factory.identifier] })
Utils._.extend(options, { allowNull: [self.association.identifier] })
));
}
......@@ -76,36 +75,43 @@ module.exports = (function() {
var pkeys = Object.keys(self.instance.Model.primaryKeys)
, pkey = pkeys.length === 1 ? pkeys[0] : 'id';
primaryKeys = Object.keys(this.__factory.target.primaryKeys);
primaryKeys = Object.keys(this.association.target.primaryKeys);
primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id';
updateWhere = {};
// set the new associations
var unassociatedIds = unassociatedObjects.map(function(associatedObject) {
associatedObject[self.__factory.identifier] = self.instance[pkey] || self.instance.id;
associatedObject[self.association.identifier] = self.instance[pkey] || self.instance.id;
return associatedObject[associationKey];
});
update = {};
update[self.__factory.identifier] = (newAssociations.length < 1 ? null : self.instance[pkey] || self.instance.id);
update[self.association.identifier] = (newAssociations.length < 1 ? null : self.instance[pkey] || self.instance.id);
if (this.association.scope) {
Object.keys(this.association.scope).forEach(function (attribute) {
update[attribute] = this.association.scope[attribute];
}.bind(this));
}
updateWhere[primaryKey] = unassociatedIds;
promises.push(this.__factory.target.update(
promises.push(this.association.target.update(
update,
updateWhere,
Utils._.extend(options, { allowNull: [self.__factory.identifier] })
Utils._.extend(options, { allowNull: [self.association.identifier] })
));
}
return Utils.Promise.all(promises);
};
HasManySingleLinked.prototype.injectAdder = function(newAssociation, additionalAttributes) {
var primaryKeys = Object.keys(this.instance.Model.primaryKeys)
, primaryKey = primaryKeys.length === 1 ? primaryKeys[0] : 'id'
, options = additionalAttributes;
newAssociation[this.__factory.identifier] = this.instance[primaryKey];
HasManySingleLinked.prototype.injectAdder = function(newAssociation, options) {
newAssociation.set(this.association.identifier, this.instance.get(this.instance.Model.primaryKeyAttribute));
if (this.association.scope) {
Object.keys(this.association.scope).forEach(function (attribute) {
newAssociation.set(attribute, this.association.scope[attribute]);
}.bind(this));
}
return newAssociation.save(options);
};
......
......@@ -3,6 +3,7 @@
var Utils = require('./../utils')
, Helpers = require('./helpers')
, _ = require('lodash')
, Association = require('./base')
, Transaction = require('../transaction');
var HasManySingleLinked = require('./has-many-single-linked')
......@@ -10,6 +11,7 @@ var HasManySingleLinked = require('./has-many-single-linked')
module.exports = (function() {
var HasMany = function(source, target, options) {
Association.call(this);
var self = this;
this.associationType = 'HasMany';
......@@ -19,6 +21,7 @@ module.exports = (function() {
this.options = options;
this.sequelize = source.daoFactoryManager.sequelize;
this.through = options.through;
this.scope = options.scope;
this.isMultiAssociation = true;
this.isSelfAssociation = this.source === this.target;
this.doubleLinked = false;
......@@ -462,6 +465,11 @@ module.exports = (function() {
});
} else {
values[association.identifier] = instance.get(association.source.primaryKeyAttribute);
if (association.scope) {
Object.keys(association.scope).forEach(function (attribute) {
values[attribute] = association.scope[attribute];
});
}
return association.target.create(values, fieldsOrOptions);
}
};
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!