Skip to content

Commit

Permalink
types(connection): make transaction() return type match the executor …
Browse files Browse the repository at this point in the history
…function

Fix #14656
  • Loading branch information
vkarpov15 committed Jun 10, 2024
1 parent 7bc5395 commit 7bfd2bc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 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
6 changes: 3 additions & 3 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 Expand Up @@ -148,4 +148,4 @@ function schemaInstanceMethodsAndQueryHelpersOnConnection() {
});

const TestModel = connection.model<User, UserModel, UserQueryHelpers>('User', userSchema);
}
}

Check warning on line 151 in test/types/connection.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Newline required at end of file but not found

Check warning on line 151 in test/types/connection.test.ts

View workflow job for this annotation

GitHub Actions / Lint TS-Files

Newline required at end of file but not found
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 7bfd2bc

Please sign in to comment.