Skip to content

Commit f6eee84

Browse files
Ajay  PrajapatiAjay  Prajapati
authored andcommitted
feat: condition to allow empty array in createAll
Signed-off-by: Ajay Prajapati <ajay.prajapati@Ajay-SFIN957.local>
1 parent 337391a commit f6eee84

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

lib/dao.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,25 @@ DataAccessObject.createAll = function(dataArray, options, cb) {
512512
options = options || {};
513513
cb = cb || utils.createPromiseCallback();
514514

515-
assert(typeof dataArray === 'object' && dataArray.length,
516-
'The data argument must be an array with length > 0');
515+
assert(typeof dataArray === 'object' && Array.isArray(dataArray),
516+
'The data argument must be an array');
517517
assert(typeof options === 'object', 'The options argument must be an object');
518518
assert(typeof cb === 'function', 'The cb argument must be a function');
519+
// If the dataArray is empty, we return an empty array or an error
520+
if (dataArray.length === 0) {
521+
if (options.allowEmptyArray === true) {
522+
process.nextTick(function() {
523+
cb(null, []);
524+
});
525+
} else {
526+
process.nextTick(function() {
527+
const err = new Error('The data argument must be an array with length > 0');
528+
err.statusCode = 400;
529+
cb(err);
530+
});
531+
}
532+
return cb.promise;
533+
}
519534

520535
const validationPromises = [];
521536
for (let index = 0; index < dataArray.length; index++) {

0 commit comments

Comments
 (0)