Skip to content

Commit

Permalink
Fix .createEach() bug. (closes balderdashy/sails#7266)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikermcneil committed Dec 11, 2022
1 parent cf02582 commit 2d96570
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/waterline/utils/query/forge-stage-two-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,24 @@ module.exports = function forgeStageTwoQuery(query, orm) {
throw buildUsageError('E_NOOP', 'No things to create were provided.', query.using);
}//-•

// Ensure no two items in the `newRecords` array point to the same object reference.
// Why? Multiple references to the same object can get tangly and cause problems downstream
// in Waterline, such as this confusing error message: https://github.com/balderdashy/sails/issues/7266
//
// On the other hand, simply using `.uniq()` to deduplicate can be somewhat unexpected behavior.
// (Imagine using `let x = {}; await Widget.createEach([x,x,x,x]);` to create four widgets.
// It would be a surprise if it only created one widget.)
if (query.newRecords.length !== _.uniq(query.newRecords).length) {
throw buildUsageError(
'E_INVALID_NEW_RECORDS',
'Two or more of the items in the provided array of new records are actually references '+
'to the same JavaScript object (`.createEach(x,y,x)`). This is too ambiguous, since it '+
'could mean creating much more or much less data than intended. Instead, pass in distinct '+
'dictionaries for each new record you would like to create (`createEach.({},{},x,y,z)`).',
query.using
);
}//-•

// Validate and normalize each new record in the provided array.
query.newRecords = _.map(query.newRecords, function (newRecord){

Expand Down

0 comments on commit 2d96570

Please sign in to comment.