Skip to content

Commit

Permalink
Merge branch 'master' into 8.6
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jul 15, 2024
2 parents b8f0109 + 93ebbe1 commit f5a0af1
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 6 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
8.5.1 / 2024-07-12
==================
* perf(model): performance improvements for insertMany() #14724
* fix(model): avoid leaving subdoc defaults on top-level doc when setting subdocument to same value #14728 #14722
* fix(model): handle transactionAsyncLocalStorage option with insertMany() #14743
* types: make _id required on Document type #14735 #14660
* types: fix ChangeStream.close to return a Promise<void> like the driver #14740 [orgads](https://github.com/orgads)

8.5.0 / 2024-07-08
==================
* perf: memoize toJSON / toObject default options #14672
Expand Down
3 changes: 2 additions & 1 deletion lib/cursor/changeStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ class ChangeStream extends EventEmitter {
close() {
this.closed = true;
if (this.driverChangeStream) {
this.driverChangeStream.close();
return this.driverChangeStream.close();
}
return Promise.resolve();
}
}

Expand Down
10 changes: 8 additions & 2 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,10 @@ Model.prototype.$__handleSave = function(options, callback) {

const session = this.$session();
const asyncLocalStorage = this[modelDbSymbol].base.transactionAsyncLocalStorage?.getStore();
if (!saveOptions.hasOwnProperty('session') && session != null) {
if (session != null) {
saveOptions.session = session;
} else if (asyncLocalStorage?.session != null) {
} else if (!options.hasOwnProperty('session') && asyncLocalStorage?.session != null) {
// Only set session from asyncLocalStorage if `session` option wasn't originally passed in options
saveOptions.session = asyncLocalStorage.session;
}
if (this.$isNew) {
Expand Down Expand Up @@ -2838,6 +2839,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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mongoose",
"description": "Mongoose MongoDB ODM",
"version": "8.5.0",
"version": "8.5.1",
"author": "Guillermo Rauch <[email protected]>",
"keywords": [
"mongodb",
Expand Down
18 changes: 17 additions & 1 deletion test/docs/transactions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ describe('transactions', function() {
await Test.createCollection();
await Test.deleteMany({});

const doc = new Test({ name: 'test_transactionAsyncLocalStorage' });
let doc = new Test({ name: 'test_transactionAsyncLocalStorage' });
await assert.rejects(
() => m.connection.transaction(async() => {
await doc.save();
Expand All @@ -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,20 @@ 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(
() => m.connection.transaction(async() => {
await doc.save({ session: null });
throw new Error('Oops!');
}),
/Oops!/
);
exists = await Test.exists({ _id: doc._id });
assert.ok(exists);
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13746,7 +13746,7 @@ describe('document', function() {
});
});

describe('Check if instance function that is supplied in schema option is availabe', function() {
describe('Check if instance function that is supplied in schema option is available', function() {
it('should give an instance function back rather than undefined', function ModelJS() {
const testSchema = new mongoose.Schema({}, { methods: { instanceFn() { return 'Returned from DocumentInstanceFn'; } } });
const TestModel = mongoose.model('TestModel', testSchema);
Expand Down

0 comments on commit f5a0af1

Please sign in to comment.