From f6eee840bd14c82f6539842502fd58ef3b78df46 Mon Sep 17 00:00:00 2001 From: Ajay Prajapati Date: Fri, 13 Jun 2025 13:21:48 +0530 Subject: [PATCH] feat: condition to allow empty array in createAll Signed-off-by: Ajay Prajapati --- lib/dao.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index b80c30d3b..0f2ff5e9e 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -512,10 +512,25 @@ DataAccessObject.createAll = function(dataArray, options, cb) { options = options || {}; cb = cb || utils.createPromiseCallback(); - assert(typeof dataArray === 'object' && dataArray.length, - 'The data argument must be an array with length > 0'); + assert(typeof dataArray === 'object' && Array.isArray(dataArray), + 'The data argument must be an array'); assert(typeof options === 'object', 'The options argument must be an object'); assert(typeof cb === 'function', 'The cb argument must be a function'); + // If the dataArray is empty, we return an empty array or an error + if (dataArray.length === 0) { + if (options.allowEmptyArray === true) { + process.nextTick(function() { + cb(null, []); + }); + } else { + process.nextTick(function() { + const err = new Error('The data argument must be an array with length > 0'); + err.statusCode = 400; + cb(err); + }); + } + return cb.promise; + } const validationPromises = []; for (let index = 0; index < dataArray.length; index++) {