import mongoose from 'mongoose' ; import { NextFunction , Request , Response } from 'express' ; export const transactionHandler = ( controllerFunc ) => async ( req , res , next ? ) : Promise < void > => { const session = await mongoose . startSession ( ) ; session . startTransaction ( ) ; try { await controllerFunc ( req , res , next ) ; await session . commitTransaction ( ) ; session . endSession ( ) ; } catch ( err ) { await session . abortTransaction ( ) ; session . endSession ( ) ; throw err ; } } ;