diff --git a/package.json b/package.json index 8f095a6..34b25be 100644 --- a/package.json +++ b/package.json @@ -52,4 +52,4 @@ "reflect-metadata": ">= 0.1.12", "typeorm": ">= 0.2.8" } -} +} \ No newline at end of file diff --git a/src/Transactional.ts b/src/Transactional.ts index 2536241..f9c5376 100644 --- a/src/Transactional.ts +++ b/src/Transactional.ts @@ -1,8 +1,9 @@ import { getNamespace } from 'cls-hooked' -import { EntityManager, getManager } from 'typeorm' +import { ConnectionManager, ContainerInterface, EntityManager, getFromContainer, getManager } from 'typeorm' + import { - getEntityManagerForConnection, NAMESPACE_NAME, + getEntityManagerForConnection, setEntityManagerForConnection, } from './common' import { runInNewHookContext } from './hook' @@ -21,17 +22,17 @@ export function Transactional(options?: { propagation?: Propagation isolationLevel?: IsolationLevel }): MethodDecorator { - const connectionName: string = - options && options.connectionName ? options.connectionName : 'default' - const propagation: Propagation = - options && options.propagation ? options.propagation : Propagation.REQUIRED + const connectionName: string = options && options.connectionName ? options.connectionName : 'default' // prettier-ignore + const propagation: Propagation = options && options.propagation ? options.propagation : Propagation.REQUIRED // prettier-ignore const isolationLevel: IsolationLevel | undefined = options && options.isolationLevel return (target: any, methodName: string | symbol, descriptor: TypedPropertyDescriptor) => { const originalMethod = descriptor.value - descriptor.value = function(...args: any[]) { + descriptor.value = function (this: { container?: ContainerInterface }, ...args: any[]) { const context = getNamespace(NAMESPACE_NAME) + const container = this.container || { get: getFromContainer } + if (!context) { throw new Error( 'No CLS namespace defined in your app ... please call initializeTransactionalContext() before application start.' @@ -49,13 +50,19 @@ export function Transactional(options?: { return result } + const connectionManager = container.get(ConnectionManager) + + const connection = connectionManager + ? connectionManager.get(connectionName) + : getManager(connectionName) + if (isolationLevel) { return await runInNewHookContext(context, () => - getManager(connectionName).transaction(isolationLevel, transactionCallback) + connection.transaction(isolationLevel, transactionCallback) ) } else { return await runInNewHookContext(context, () => - getManager(connectionName).transaction(transactionCallback) + connection.transaction(transactionCallback) ) } } diff --git a/tsconfig.json b/tsconfig.json index 32bde2e..b5ec0ad 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,7 +13,10 @@ "experimentalDecorators": true, "target": "es5", "sourceMap": true, - "outDir": "dist" + "outDir": "dist", + "typeRoots": [ + "./node_modules/@types" + ] }, "include": ["src/**/*"], "exclude": ["node_modules", "test/**/*.ts", "dist", "temp"]