From 08fde90f0f5c7c39678e69fa67c0e625e7314eb5 Mon Sep 17 00:00:00 2001 From: Luis Pais Date: Tue, 23 Jul 2019 20:54:18 +0100 Subject: [PATCH] [@types/mongodb] Added withTransaction definition and another necessary type. (#37085) * [@types/mongodb] added withTransaction definition * Added missing semicolon. --- types/mongodb/index.d.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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);