Skip to content

Commit

Permalink
fix(model): handle transactionAsyncLocalStorage option with `insert…
Browse files Browse the repository at this point in the history
…Many()`

Fix #14738
  • Loading branch information
vkarpov15 committed Jul 11, 2024
1 parent 090297e commit 3646952
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
5 changes: 5 additions & 0 deletions test/docs/transactions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ describe('transactions', function() {
}();
assert.equal(doc.name, 'test_transactionAsyncLocalStorage');

await Test.insertMany([{ name: 'bar' }]);

throw new Error('Oops!');
}),
/Oops!/
Expand All @@ -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);
});
});

Expand Down

0 comments on commit 3646952

Please sign in to comment.