Merge pull request #534 from fabien/fix/embeds-many-destroy-all

Properly support embedsMany destroyAll
This commit is contained in:
Raymond Feng 2015-03-24 09:58:02 -07:00
commit 58e7c4e90b
3 changed files with 71 additions and 2 deletions

View File

@ -2107,6 +2107,10 @@ RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params)
scopeMethods.related = scopeMethod(definition, 'related'); // bound to definition scopeMethods.related = scopeMethod(definition, 'related'); // bound to definition
if (!definition.options.persistent) {
scopeMethods.destroyAll = scopeMethod(definition, 'destroyAll');
}
var customMethods = extendScopeMethods(definition, scopeMethods, params.scopeMethods); var customMethods = extendScopeMethods(definition, scopeMethods, params.scopeMethods);
for (var i = 0; i < customMethods.length; i++) { for (var i = 0; i < customMethods.length; i++) {
@ -2313,6 +2317,31 @@ EmbedsMany.prototype.destroyById = function (fkId, cb) {
return inst; // sync return inst; // sync
}; };
EmbedsMany.prototype.destroyAll = function(where, cb) {
if (typeof where === 'function') cb = where, where = {};
var propertyName = this.definition.keyFrom;
var modelInstance = this.modelInstance;
var embeddedList = this.embeddedList();
if (where && Object.keys(where).length > 0) {
var filter = applyFilter({ where: where });
var reject = function(v) { return !filter(v) };
embeddedList = embeddedList ? embeddedList.filter(reject) : embeddedList;
} else {
embeddedList = [];
}
if (typeof cb === 'function') {
modelInstance.updateAttribute(propertyName,
embeddedList, function(err) {
cb(err);
});
} else {
modelInstance.setAttribute(propertyName, embeddedList);
}
};
EmbedsMany.prototype.get = EmbedsMany.prototype.findById; EmbedsMany.prototype.get = EmbedsMany.prototype.findById;
EmbedsMany.prototype.set = EmbedsMany.prototype.updateById; EmbedsMany.prototype.set = EmbedsMany.prototype.updateById;
EmbedsMany.prototype.unset = EmbedsMany.prototype.destroyById; EmbedsMany.prototype.unset = EmbedsMany.prototype.destroyById;

View File

@ -192,6 +192,7 @@ function defineScope(cls, targetClass, name, params, methods, options) {
f.findById = findById; f.findById = findById;
f.findOne = findOne; f.findOne = findOne;
f.count = count; f.count = count;
for (var i in definition.methods) { for (var i in definition.methods) {
f[i] = definition.methods[i].bind(self); f[i] = definition.methods[i].bind(self);
} }

View File

@ -2665,13 +2665,52 @@ describe('relations', function () {
}); });
}); });
it('should have embedded items - verify', function(done) { it('should have removed embedded items - verify', function(done) {
Person.findOne(function(err, p) { Person.findOne(function(err, p) {
p.addresses.should.have.length(1); p.addresses.should.have.length(1);
done(); done();
}); });
}); });
it('should create embedded items on scope', function(done) {
Person.findOne(function(err, p) {
p.addressList.create({ street: 'Street 3' }, function(err, address) {
should.not.exist(err);
address.street.should.equal('Street 3');
done();
});
});
});
it('should remove embedded items - filtered', function(done) {
Person.findOne(function(err, p) {
p.addresses.should.have.length(2);
p.addressList.destroyAll({ street: 'Street 3' }, function(err) {
should.not.exist(err);
p.addresses.should.have.length(1);
done();
});
});
});
it('should remove all embedded items', function(done) {
Person.findOne(function(err, p) {
p.addresses.should.have.length(1);
p.addressList.destroyAll(function(err) {
should.not.exist(err);
p.addresses.should.have.length(0);
done();
});
});
});
it('should have removed all embedded items - verify', function(done) {
Person.findOne(function(err, p) {
p.addresses.should.have.length(0);
done();
});
});
}); });
describe('embedsMany - numeric ids + forceId', function () { describe('embedsMany - numeric ids + forceId', function () {