Skip to content

Commit 3f21bfa

Browse files
committed
fix: backport #14743
1 parent 378d115 commit 3f21bfa

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

lib/model.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3113,6 +3113,11 @@ Model.$__insertMany = function(arr, options, callback) {
31133113
const throwOnValidationError = typeof options.throwOnValidationError === 'boolean' ? options.throwOnValidationError : false;
31143114
const lean = !!options.lean;
31153115

3116+
const asyncLocalStorage = this.db.base.transactionAsyncLocalStorage?.getStore();
3117+
if ((!options || !options.hasOwnProperty('session')) && asyncLocalStorage?.session != null) {
3118+
options = { ...options, session: asyncLocalStorage.session };
3119+
}
3120+
31163121
if (!Array.isArray(arr)) {
31173122
arr = [arr];
31183123
}

test/docs/transactions.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,8 @@ describe('transactions', function() {
459459
}();
460460
assert.equal(doc.name, 'test_transactionAsyncLocalStorage');
461461

462+
await Test.insertMany([{ name: 'bar' }]);
463+
462464
throw new Error('Oops!');
463465
}),
464466
/Oops!/
@@ -468,6 +470,9 @@ describe('transactions', function() {
468470

469471
exists = await Test.exists({ name: 'foo' });
470472
assert.ok(!exists);
473+
474+
exists = await Test.exists({ name: 'bar' });
475+
assert.ok(!exists);
471476
});
472477
});
473478
});

0 commit comments

Comments
 (0)