Skip to content

Commit

Permalink
Merge pull request #14661 from Automattic/vkarpov15/gh-14656
Browse files Browse the repository at this point in the history
types(connection): make transaction() return type match the executor function
  • Loading branch information
vkarpov15 authored Jun 11, 2024
2 parents 2cf3274 + 90a2b5d commit 90fa8af
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
5 changes: 4 additions & 1 deletion test/docs/transactions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions test/types/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ expectType<mongodb.Db>(conn.db);
expectType<mongodb.MongoClient>(conn.getClient());
expectType<Connection>(conn.setClient(new mongodb.MongoClient('mongodb://127.0.0.1:27017/test')));

expectType<Promise<void>>(conn.transaction(async(res) => {
expectType<Promise<string>>(conn.transaction(async(res) => {
expectType<mongodb.ClientSession>(res);
return 'a';
}));
expectType<Promise<void>>(conn.transaction(async(res) => {
expectType<Promise<string>>(conn.transaction(async(res) => {
expectType<mongodb.ClientSession>(res);
return 'a';
}, { readConcern: 'majority' }));
Expand Down
2 changes: 1 addition & 1 deletion types/connection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>, options?: mongodb.TransactionOptions): Promise<void>;
transaction<ReturnType = unknown>(fn: (session: mongodb.ClientSession) => Promise<ReturnType>, options?: mongodb.TransactionOptions): Promise<ReturnType>;

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

0 comments on commit 90fa8af

Please sign in to comment.