fix: return inserted ids with models in createAll

Signed-off-by: Samarpan Bhattacharya <this.is.samy@gmail.com>
This commit is contained in:
Samarpan Bhattacharya 2022-11-05 23:24:26 +05:30 committed by Diana Lau
parent 6c96e2f9b3
commit 4378620a6e
1 changed files with 20 additions and 2 deletions

View File

@ -707,8 +707,16 @@ SQLConnector.prototype.createAll = function(model, data, options, callback) {
if (err) { if (err) {
callback(err); callback(err);
} else { } else {
const insertedId = self.getInsertedId(model, info); const insertedIds = self.getInsertedIds(model, info);
callback(err, insertedId); // We need to parse the ids created and map them back into the model
const returnData = [];
const idPropName = self.propertyName(model, self.idColumn(model));
for (let i = 0; i < insertedIds.length; i++) {
const saved = Object.assign({}, data[i]);
saved[idPropName] = insertedIds[i];
returnData.push(saved);
}
callback(err, returnData);
} }
}); });
}; };
@ -1798,6 +1806,16 @@ SQLConnector.prototype.getInsertedId = function(model, info) {
throw new Error(g.f('{{getInsertedId()}} must be implemented by the connector')); throw new Error(g.f('{{getInsertedId()}} must be implemented by the connector'));
}; };
/**
* Parse the result for SQL INSERT (bulk) for newly inserted ids
* @param {String} model Model name
* @param {Object} info The status object from driver
* @returns {*} The inserted id values as an array
*/
SQLConnector.prototype.getInsertedIds = function(model, info) {
throw new Error(g.f('{{getInsertedIds()}} must be implemented by the connector'));
};
/** /**
* Execute a SQL statement with given parameters * Execute a SQL statement with given parameters
* @param {String} sql The SQL statement * @param {String} sql The SQL statement