Skip to content

Commit 90fa8af

Browse files
authored
Merge pull request #14661 from Automattic/vkarpov15/gh-14656
types(connection): make transaction() return type match the executor function
2 parents 2cf3274 + 90a2b5d commit 90fa8af

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

test/docs/transactions.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ describe('transactions', function() {
479479
const doc = await Test.findById(_id).orFail();
480480
let attempt = 0;
481481

482-
await db.transaction(async(session) => {
482+
const res = await db.transaction(async(session) => {
483483
await doc.save({ session });
484484

485485
if (attempt === 0) {
@@ -489,7 +489,10 @@ describe('transactions', function() {
489489
errorLabels: [mongoose.mongo.MongoErrorLabel.TransientTransactionError]
490490
});
491491
}
492+
493+
return { answer: 42 };
492494
});
495+
assert.deepStrictEqual(res, { answer: 42 });
493496

494497
const { items } = await Test.findById(_id).orFail();
495498
assert.ok(Array.isArray(items));

test/types/connection.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ expectType<mongodb.Db>(conn.db);
4545
expectType<mongodb.MongoClient>(conn.getClient());
4646
expectType<Connection>(conn.setClient(new mongodb.MongoClient('mongodb://127.0.0.1:27017/test')));
4747

48-
expectType<Promise<void>>(conn.transaction(async(res) => {
48+
expectType<Promise<string>>(conn.transaction(async(res) => {
4949
expectType<mongodb.ClientSession>(res);
5050
return 'a';
5151
}));
52-
expectType<Promise<void>>(conn.transaction(async(res) => {
52+
expectType<Promise<string>>(conn.transaction(async(res) => {
5353
expectType<mongodb.ClientSession>(res);
5454
return 'a';
5555
}, { readConcern: 'majority' }));

types/connection.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ declare module 'mongoose' {
236236
* async function executes successfully and attempt to retry if
237237
* there was a retryable error.
238238
*/
239-
transaction(fn: (session: mongodb.ClientSession) => Promise<any>, options?: mongodb.TransactionOptions): Promise<void>;
239+
transaction<ReturnType = unknown>(fn: (session: mongodb.ClientSession) => Promise<ReturnType>, options?: mongodb.TransactionOptions): Promise<ReturnType>;
240240

241241
/** Switches to a different database using the same connection pool. */
242242
useDb(name: string, options?: { useCache?: boolean, noListener?: boolean }): Connection;

0 commit comments

Comments
 (0)