Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@
"reflect-metadata": ">= 0.1.12",
"typeorm": ">= 0.2.8"
}
}
}
25 changes: 16 additions & 9 deletions src/Transactional.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<any>) => {
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.'
Expand All @@ -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)
)
}
}
Expand Down
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down