From a6c053fa960c978dac91b069f4450e870776e8b5 Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Mon, 13 May 2019 11:36:58 +0000 Subject: [PATCH 1/2] feat: allow use of scoped containers --- package.json | 2 ++ src/Transactional.ts | 27 ++++++++++++++++++--------- tsconfig.json | 5 ++++- yarn.lock | 5 +++++ 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 8f095a6..efc3b8f 100644 --- a/package.json +++ b/package.json @@ -44,12 +44,14 @@ "ts-node": "^7.0.1", "tslint": "^5.11.0", "tslint-config-prettier": "^1.15.0", + "typedi": "^0.8.0", "typedoc": "^0.12.0", "typeorm": "^0.2.8", "typescript": "^3.0.3" }, "peerDependencies": { "reflect-metadata": ">= 0.1.12", + "typedi": ">=0.8.0", "typeorm": ">= 0.2.8" } } diff --git a/src/Transactional.ts b/src/Transactional.ts index 2536241..8cb1938 100644 --- a/src/Transactional.ts +++ b/src/Transactional.ts @@ -1,8 +1,11 @@ import { getNamespace } from 'cls-hooked' -import { EntityManager, getManager } from 'typeorm' +import Container from 'typedi' +import { ContainerInstance } from 'typedi' +import { ConnectionManager, EntityManager, getManager } from 'typeorm' + import { - getEntityManagerForConnection, NAMESPACE_NAME, + getEntityManagerForConnection, setEntityManagerForConnection, } from './common' import { runInNewHookContext } from './hook' @@ -21,17 +24,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?: ContainerInstance }, ...args: any[]) { const context = getNamespace(NAMESPACE_NAME) + const container = this.container || Container.of(undefined) + if (!context) { throw new Error( 'No CLS namespace defined in your app ... please call initializeTransactionalContext() before application start.' @@ -49,13 +52,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"] diff --git a/yarn.lock b/yarn.lock index 86204c8..33e21ac 100644 --- a/yarn.lock +++ b/yarn.lock @@ -887,6 +887,11 @@ tsutils@^2.27.2: dependencies: tslib "^1.8.1" +typedi@^0.8.0: + version "0.8.0" + resolved "https://registry.npmjs.org/typedi/-/typedi-0.8.0.tgz#d8e203bd1d41a96e2b0a5c6295147d74b2b2d03e" + integrity sha512-/c7Bxnm6eh5kXx2I+mTuO+2OvoWni5+rXA3PhXwVWCtJRYmz3hMok5s1AKLzoDvNAZqj/Q/acGstN0ri5aQoOA== + typedoc-default-themes@^0.5.0: version "0.5.0" resolved "https://registry.yarnpkg.com/typedoc-default-themes/-/typedoc-default-themes-0.5.0.tgz#6dc2433e78ed8bea8e887a3acde2f31785bd6227" From 99f632faaa324b6d118e9dd1dd496e701fd86bb6 Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Mon, 13 May 2019 16:02:45 +0000 Subject: [PATCH 2/2] chore: remove typedi as a dependency --- package.json | 4 +--- src/Transactional.ts | 10 ++++------ yarn.lock | 5 ----- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index efc3b8f..34b25be 100644 --- a/package.json +++ b/package.json @@ -44,14 +44,12 @@ "ts-node": "^7.0.1", "tslint": "^5.11.0", "tslint-config-prettier": "^1.15.0", - "typedi": "^0.8.0", "typedoc": "^0.12.0", "typeorm": "^0.2.8", "typescript": "^3.0.3" }, "peerDependencies": { "reflect-metadata": ">= 0.1.12", - "typedi": ">=0.8.0", "typeorm": ">= 0.2.8" } -} +} \ No newline at end of file diff --git a/src/Transactional.ts b/src/Transactional.ts index 8cb1938..f9c5376 100644 --- a/src/Transactional.ts +++ b/src/Transactional.ts @@ -1,7 +1,5 @@ import { getNamespace } from 'cls-hooked' -import Container from 'typedi' -import { ContainerInstance } from 'typedi' -import { ConnectionManager, EntityManager, getManager } from 'typeorm' +import { ConnectionManager, ContainerInterface, EntityManager, getFromContainer, getManager } from 'typeorm' import { NAMESPACE_NAME, @@ -31,10 +29,10 @@ export function Transactional(options?: { return (target: any, methodName: string | symbol, descriptor: TypedPropertyDescriptor) => { const originalMethod = descriptor.value - descriptor.value = function(this: { container?: ContainerInstance }, ...args: any[]) { + descriptor.value = function (this: { container?: ContainerInterface }, ...args: any[]) { const context = getNamespace(NAMESPACE_NAME) - const container = this.container || Container.of(undefined) - + const container = this.container || { get: getFromContainer } + if (!context) { throw new Error( 'No CLS namespace defined in your app ... please call initializeTransactionalContext() before application start.' diff --git a/yarn.lock b/yarn.lock index 33e21ac..86204c8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -887,11 +887,6 @@ tsutils@^2.27.2: dependencies: tslib "^1.8.1" -typedi@^0.8.0: - version "0.8.0" - resolved "https://registry.npmjs.org/typedi/-/typedi-0.8.0.tgz#d8e203bd1d41a96e2b0a5c6295147d74b2b2d03e" - integrity sha512-/c7Bxnm6eh5kXx2I+mTuO+2OvoWni5+rXA3PhXwVWCtJRYmz3hMok5s1AKLzoDvNAZqj/Q/acGstN0ri5aQoOA== - typedoc-default-themes@^0.5.0: version "0.5.0" resolved "https://registry.yarnpkg.com/typedoc-default-themes/-/typedoc-default-themes-0.5.0.tgz#6dc2433e78ed8bea8e887a3acde2f31785bd6227"