Skip to content

Commit

Permalink
Merge branch '6.x' into vkarpov15/gh-14113-2
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Feb 28, 2024
2 parents 635c795 + 0af28e0 commit b6cb439
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
27 changes: 16 additions & 11 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3430,6 +3430,13 @@ Model.$__insertMany = function(arr, options, callback) {
const results = ordered ? null : new Array(arr.length);
const toExecute = arr.map((doc, index) =>
callback => {
// If option `lean` is set to true bypass validation and hydration
if (lean) {
// we have to execute callback at the nextTick to be compatible
// with parallelLimit, as `results` variable has TDZ issue if we
// execute the callback synchronously
return immediate(() => callback(null, doc));
}
if (!(doc instanceof _this)) {
try {
doc = new _this(doc);
Expand All @@ -3440,13 +3447,6 @@ Model.$__insertMany = function(arr, options, callback) {
if (options.session != null) {
doc.$session(options.session);
}
// If option `lean` is set to true bypass validation
if (lean) {
// we have to execute callback at the nextTick to be compatible
// with parallelLimit, as `results` variable has TDZ issue if we
// execute the callback synchronously
return immediate(() => callback(null, doc));
}
doc.$validate({ __noPromise: true }, function(error) {
if (error) {
// Option `ordered` signals that insert should be continued after reaching
Expand Down Expand Up @@ -3510,7 +3510,7 @@ Model.$__insertMany = function(arr, options, callback) {
callback(null, []);
return;
}
const docObjects = docAttributes.map(function(doc) {
const docObjects = lean ? docAttributes : docAttributes.map(function(doc) {
if (doc.$__schema.options.versionKey) {
doc[doc.$__schema.options.versionKey] = 0;
}
Expand Down Expand Up @@ -3572,6 +3572,9 @@ Model.$__insertMany = function(arr, options, callback) {
return !isErrored;
}).
map(function setIsNewForInsertedDoc(doc) {
if (lean) {
return doc;
}
doc.$__reset();
_setIsNew(doc, false);
return doc;
Expand All @@ -3588,9 +3591,11 @@ Model.$__insertMany = function(arr, options, callback) {
return;
}

for (const attribute of docAttributes) {
attribute.$__reset();
_setIsNew(attribute, false);
if (!lean) {
for (const attribute of docAttributes) {
attribute.$__reset();
_setIsNew(attribute, false);
}
}

if (rawResult) {
Expand Down
7 changes: 6 additions & 1 deletion test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,12 @@ describe('connections:', function() {
it('should not throw an error when attempting to mutate unmutable options object gh-13335', async function() {
const m = new mongoose.Mongoose();
const opts = Object.preventExtensions({});
const conn = await m.connect('mongodb://127.0.0.1:27017/db?retryWrites=true&w=majority&readPreference=secondaryPreferred', opts);

const uri = start.uri.lastIndexOf('?') === -1 ?
start.uri + '?retryWrites=true&w=majority&readPreference=primaryPreferred' :
start.uri.slice(0, start.uri.lastIndexOf('?')) + '?retryWrites=true&w=majority&readPreference=primaryPreferred';

const conn = await m.connect(uri, opts);
assert.ok(conn);
});
});
Expand Down

0 comments on commit b6cb439

Please sign in to comment.