From 5a0b0270f321f2fe96c609e05bc7dd5687c2f37e Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Fri, 19 May 2017 16:16:52 -0700 Subject: [PATCH] Return promise for batch create --- lib/dao.js | 4 ++-- test/manipulation.test.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index 9e3e06f0..835ca8a2 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -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 = {}; diff --git a/test/manipulation.test.js b/test/manipulation.test.js index 21784466..283f983e 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -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') {