From 5e4023f1d1d64862f36fceef7597e8ae875ac442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Fily?= Date: Sat, 8 Jun 2024 14:32:07 +0200 Subject: [PATCH 1/4] Fix problem when deleting an entry on a populated Map --- lib/types/map.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/types/map.js b/lib/types/map.js index d63cd403523..dd88de8ed59 100644 --- a/lib/types/map.js +++ b/lib/types/map.js @@ -119,7 +119,7 @@ class MongooseMap extends Map { v.$__.wasPopulated = { value: v._id }; return v; }); - } else { + } else if (value) { // Fix when using delete method because the value is undefined if (value.$__ == null) { value = new populated.options[populateModelSymbol](value); } From 26ab76a51ff618a37155f6d18457c9fd699fba5e Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 10 Jun 2024 16:24:28 -0400 Subject: [PATCH 2/4] remove unnecessary comment --- lib/types/map.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/types/map.js b/lib/types/map.js index dd88de8ed59..4b8607df311 100644 --- a/lib/types/map.js +++ b/lib/types/map.js @@ -119,7 +119,7 @@ class MongooseMap extends Map { v.$__.wasPopulated = { value: v._id }; return v; }); - } else if (value) { // Fix when using delete method because the value is undefined + } else if (value != null) { if (value.$__ == null) { value = new populated.options[populateModelSymbol](value); } From 7bfd2bc8b7638ba21590e0d2f7235319306c0f9c Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 10 Jun 2024 17:17:56 -0400 Subject: [PATCH 3/4] types(connection): make transaction() return type match the executor function Fix #14656 --- test/docs/transactions.test.js | 5 ++++- test/types/connection.test.ts | 6 +++--- types/connection.d.ts | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/test/docs/transactions.test.js b/test/docs/transactions.test.js index e21639331b9..f40067a71ea 100644 --- a/test/docs/transactions.test.js +++ b/test/docs/transactions.test.js @@ -479,7 +479,7 @@ describe('transactions', function() { const doc = await Test.findById(_id).orFail(); let attempt = 0; - await db.transaction(async(session) => { + const res = await db.transaction(async(session) => { await doc.save({ session }); if (attempt === 0) { @@ -489,7 +489,10 @@ describe('transactions', function() { errorLabels: [mongoose.mongo.MongoErrorLabel.TransientTransactionError] }); } + + return { answer: 42 }; }); + assert.deepStrictEqual(res, { answer: 42 }); const { items } = await Test.findById(_id).orFail(); assert.ok(Array.isArray(items)); diff --git a/test/types/connection.test.ts b/test/types/connection.test.ts index c5979663a20..db721ebbb21 100644 --- a/test/types/connection.test.ts +++ b/test/types/connection.test.ts @@ -45,11 +45,11 @@ expectType(conn.db); expectType(conn.getClient()); expectType(conn.setClient(new mongodb.MongoClient('mongodb://127.0.0.1:27017/test'))); -expectType>(conn.transaction(async(res) => { +expectType>(conn.transaction(async(res) => { expectType(res); return 'a'; })); -expectType>(conn.transaction(async(res) => { +expectType>(conn.transaction(async(res) => { expectType(res); return 'a'; }, { readConcern: 'majority' })); @@ -148,4 +148,4 @@ function schemaInstanceMethodsAndQueryHelpersOnConnection() { }); const TestModel = connection.model('User', userSchema); -} +} \ No newline at end of file diff --git a/types/connection.d.ts b/types/connection.d.ts index 2f47bdc84e5..879e4d98e06 100644 --- a/types/connection.d.ts +++ b/types/connection.d.ts @@ -236,7 +236,7 @@ declare module 'mongoose' { * async function executes successfully and attempt to retry if * there was a retryable error. */ - transaction(fn: (session: mongodb.ClientSession) => Promise, options?: mongodb.TransactionOptions): Promise; + transaction(fn: (session: mongodb.ClientSession) => Promise, options?: mongodb.TransactionOptions): Promise; /** Switches to a different database using the same connection pool. */ useDb(name: string, options?: { useCache?: boolean, noListener?: boolean }): Connection; From 90a2b5d7641e9423461762aa63b6cc83c7c3be79 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Tue, 11 Jun 2024 12:49:54 -0400 Subject: [PATCH 4/4] Update test/types/connection.test.ts Co-authored-by: hasezoey --- test/types/connection.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/types/connection.test.ts b/test/types/connection.test.ts index db721ebbb21..b4fcda1cd89 100644 --- a/test/types/connection.test.ts +++ b/test/types/connection.test.ts @@ -148,4 +148,4 @@ function schemaInstanceMethodsAndQueryHelpersOnConnection() { }); const TestModel = connection.model('User', userSchema); -} \ No newline at end of file +}