Skip to content

Commit

Permalink
Merge pull request #14743 from Automattic/vkarpov15/gh-14738
Browse files Browse the repository at this point in the history
fix(model): handle `transactionAsyncLocalStorage` option with `insertMany()`
  • Loading branch information
vkarpov15 authored Jul 12, 2024
2 parents 090297e + 3646952 commit 70e8e19
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 70e8e19

Please sign in to comment.