Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types(connection): make transaction() return type match the executor function #14661

Merged
merged 5 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/types/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class MongooseMap extends Map {
v.$__.wasPopulated = { value: v._id };
return v;
});
} else {
} else if (value != null) {
if (value.$__ == null) {
value = new populated.options[populateModelSymbol](value);
}
Expand Down
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.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 @@
});

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
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading