diff --git a/types/mongodb/index.d.ts b/types/mongodb/index.d.ts index cb1c5c1641..c1fcc48d94 100644 --- a/types/mongodb/index.d.ts +++ b/types/mongodb/index.d.ts @@ -23,6 +23,7 @@ // Erik Christensen // Nick Zahn // Jarom Loveridge +// Luis Pais // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 @@ -126,6 +127,19 @@ export interface ClientSession extends EventEmitter { * Starts a new transaction with the given options. */ startTransaction(options?: TransactionOptions): void; + + /** + * Runs a provided lambda within a transaction, retrying either the commit operation + * or entire transaction as needed (and when the error permits) to better ensure that + * the transaction can complete successfully. + * + * IMPORTANT: This method requires the user to return a Promise, all lambdas that do not + * return a Promise will result in undefined behavior. + * + * @param fn Function to execute with the new session. + * @param options Optional settings for the transaction + */ + withTransaction(fn: WithTransactionCallback, options?: TransactionOptions): Promise; } @@ -202,6 +216,8 @@ export interface MongoCallback { (error: MongoError, result: T): void; } +export type WithTransactionCallback = (session: ClientSession) => Promise; + /** http://mongodb.github.io/node-mongodb-native/3.1/api/MongoError.html */ export class MongoError extends Error { constructor(message: string);