Fix for #1724 - Added options to attribute updates (#1725) (#1748)

* Fix for #1724 - Added options to attribute updates

* Fix for #1724 - Added unit tests for options
This commit is contained in:
Bill Matson 2019-05-31 15:06:58 -04:00 committed by Diana Lau
parent 5e3e1198d6
commit 16936e0146
2 changed files with 69 additions and 3 deletions

View File

@ -2834,7 +2834,7 @@ EmbedsMany.prototype.destroyById = function(fkId, options, cb) {
if (index > -1) embeddedList.splice(index, 1);
if (typeof cb !== 'function') return;
modelInstance.updateAttribute(propertyName,
embeddedList, function(err) {
embeddedList, context.options, function(err) {
if (err) return cb(err);
modelTo.notifyObserversOf('after delete', context, function(err) {
cb(err);
@ -2873,11 +2873,11 @@ EmbedsMany.prototype.destroyAll = function(where, options, cb) {
if (typeof cb === 'function') {
modelInstance.updateAttribute(propertyName,
embeddedList, function(err) {
embeddedList, options || {}, function(err) {
cb(err);
});
} else {
modelInstance.setAttribute(propertyName, embeddedList);
modelInstance.setAttribute(propertyName, embeddedList, options || {});
}
};

View File

@ -4980,6 +4980,72 @@ describe('relations', function() {
});
});
it('should pass options when removed by id', function(done) {
const verifyOptions = function(ctx, next) {
if (!ctx.options || !ctx.options.verify) {
return next(new Error('options or options.verify is missing'));
}
return next();
};
Person.observe('before save', verifyOptions);
Person.findOne(function(err, p) {
p.addressList.create({street: 'options 1'}, {verify: true}, function(err, address) {
if (err) {
Person.clearObservers('before save');
return done(err);
}
p.addressList.destroy(address.id, {verify: true}, function(err) {
if (err) {
Person.clearObservers('before save');
return done(err);
}
Person.findById(p.id, function(err, verify) {
if (err) {
Person.clearObservers('before save');
return done(err);
}
verify.addresses.should.have.length(1);
Person.clearObservers('before save');
done();
});
});
});
});
});
it('should pass options when removed by where', function(done) {
const verifyOptions = function(ctx, next) {
if (!ctx.options || !ctx.options.verify) {
return next(new Error('options or options.verify is missing'));
}
return next();
};
Person.observe('before save', verifyOptions);
Person.findOne(function(err, p) {
p.addressList.create({street: 'options 2'}, {verify: true}, function(err, address) {
if (err) {
Person.clearObservers('before save');
return done(err);
}
p.addressList.destroyAll({street: 'options 2'}, {verify: true}, function(err) {
if (err) {
Person.clearObservers('before save');
return done(err);
}
Person.findById(p.id, function(err, verify) {
if (err) {
Person.clearObservers('before save');
return done(err);
}
verify.addresses.should.have.length(1);
Person.clearObservers('before save');
done();
});
});
});
});
});
// eslint-disable-next-line mocha/no-identical-title
it('should create embedded items on scope', function(done) {
Person.findOne(function(err, p) {