Merge pull request #1380 from strongloop/feature/fix-create-promise

Return promise for batch create
This commit is contained in:
Raymond Feng 2017-05-19 18:43:35 -05:00 committed by GitHub
commit 492b644679
2 changed files with 30 additions and 2 deletions

View File

@ -265,7 +265,7 @@ DataAccessObject.create = function(data, options, cb) {
data = data || {};
options = options || {};
cb = cb || (Array.isArray(data) ? noCallback : utils.createPromiseCallback());
cb = cb || utils.createPromiseCallback();
assert(typeof data === 'object', 'The data argument must be an object or array');
assert(typeof options === 'object', 'The options argument must be an object');
@ -305,7 +305,7 @@ DataAccessObject.create = function(data, options, cb) {
}
cb(errors, data);
});
return;
return cb.promise;
}
var enforced = {};

View File

@ -277,6 +277,34 @@ describe('manipulation', function() {
});
});
it('should create batch of objects (promise variant)', function(done) {
var batch = [
{name: 'ShaltayPromise'},
{name: 'BoltayPromise'},
{},
];
Person.create(batch).then(function(ps) {
should.exist(ps);
ps.should.be.instanceOf(Array);
ps.should.have.lengthOf(batch.length);
Person.validatesPresenceOf('name');
Person.create(batch, function(errors, persons) {
delete Person.validations;
should.exist(errors);
errors.should.have.lengthOf(batch.length);
should.not.exist(errors[0]);
should.not.exist(errors[1]);
should.exist(errors[2]);
should.exist(persons);
persons.should.have.lengthOf(batch.length);
persons[0].errors.should.be.false;
done();
});
});
});
it('should create batch of objects with beforeCreate', function(done) {
Person.beforeCreate = function(next, data) {
if (data && data.name === 'A') {