diff --git a/lib/model.js b/lib/model.js index 82dd0857bed..8c3b15ddaba 100644 --- a/lib/model.js +++ b/lib/model.js @@ -2838,6 +2838,11 @@ Model.$__insertMany = function(arr, options, callback) { const throwOnValidationError = typeof options.throwOnValidationError === 'boolean' ? options.throwOnValidationError : false; const lean = !!options.lean; + const asyncLocalStorage = this.db.base.transactionAsyncLocalStorage?.getStore(); + if ((!options || !options.hasOwnProperty('session')) && asyncLocalStorage?.session != null) { + options = { ...options, session: asyncLocalStorage.session }; + } + if (!Array.isArray(arr)) { arr = [arr]; } diff --git a/test/docs/transactions.test.js b/test/docs/transactions.test.js index b51a6ae7fd3..0406798e0c6 100644 --- a/test/docs/transactions.test.js +++ b/test/docs/transactions.test.js @@ -388,6 +388,8 @@ describe('transactions', function() { }(); assert.equal(doc.name, 'test_transactionAsyncLocalStorage'); + await Test.insertMany([{ name: 'bar' }]); + throw new Error('Oops!'); }), /Oops!/ @@ -397,6 +399,9 @@ describe('transactions', function() { exists = await Test.exists({ name: 'foo' }); assert.ok(!exists); + + exists = await Test.exists({ name: 'bar' }); + assert.ok(!exists); doc = new Test({ name: 'test_transactionAsyncLocalStorage' }); await assert.rejects(