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

Commit bce7e323 by Mick Hansen

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

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