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BAAa,CAAC,cAAc,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,cAAc,EAAE,mBAAmB,CAAC,CAAC;aACjG;iBAAM;gBACH,OAAO,iBAAa,CAAC,cAAc,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,mBAAmB,CAAC,CAAC;aACjF;QACL,CAAC,CAAC;IACN,CAAC,CAAC;AACN,CAAC;AAlFD,kCAkFC","file":"Transaction.js","sourcesContent":["import {getConnection, getMetadataArgsStorage, MongoRepository, Repository, TreeRepository, EntityManager} from \"../../\";\nimport {TransactionOptions} from \"../options/TransactionOptions\";\nimport {IsolationLevel} from \"../../driver/types/IsolationLevel\";\n\n/**\n * Wraps some method into the transaction.\n *\n * Method result will return a promise if this decorator applied.\n * All database operations in the wrapped method should be executed using entity managed passed\n * as a first parameter into the wrapped method.\n *\n * If you want to control at what position in your method parameters entity manager should be injected,\n * then use @TransactionEntityManager() decorator.\n *\n * If you want to use repositories instead of bare entity manager,\n * then use @TransactionRepository() decorator.\n */\nexport function Transaction(connectionName?: string): MethodDecorator;\nexport function Transaction(options?: TransactionOptions): MethodDecorator;\nexport function Transaction(connectionOrOptions?: string | TransactionOptions): MethodDecorator {\n    return function (target: Object, methodName: string, descriptor: PropertyDescriptor) {\n\n        // save original method - we gonna need it\n        const originalMethod = descriptor.value;\n\n        // override method descriptor with proxy method\n        descriptor.value = function(...args: any[]) {\n            let connectionName = \"default\";\n            let isolationLevel: IsolationLevel | undefined = undefined;\n            if (connectionOrOptions) {\n                if (typeof connectionOrOptions === \"string\") {\n                    connectionName = connectionOrOptions;\n                } else {\n                    if (connectionOrOptions.connectionName) {\n                        connectionName = connectionOrOptions.connectionName;\n                    }\n                    if (connectionOrOptions.isolation) {\n                        isolationLevel = connectionOrOptions.isolation;\n                    }\n                }\n            }\n\n            const transactionCallback = (entityManager: EntityManager) => {\n                let argsWithInjectedTransactionManagerAndRepositories: any[];\n\n                // filter all @TransactionEntityManager() and @TransactionRepository() decorator usages for this method\n                const transactionEntityManagerMetadatas = getMetadataArgsStorage()\n                    .filterTransactionEntityManagers(target.constructor, methodName)\n                    .reverse();\n                const transactionRepositoryMetadatas = getMetadataArgsStorage()\n                    .filterTransactionRepository(target.constructor, methodName)\n                    .reverse();\n\n                // if there are @TransactionEntityManager() decorator usages the inject them\n                if (transactionEntityManagerMetadatas.length > 0) {\n                    argsWithInjectedTransactionManagerAndRepositories = [...args];\n                    // replace method params with injection of transactionEntityManager\n                    transactionEntityManagerMetadatas.forEach(metadata => {\n                        argsWithInjectedTransactionManagerAndRepositories.splice(metadata.index, 0, entityManager);\n                    });\n\n                } else if (transactionRepositoryMetadatas.length === 0) { // otherwise if there's no transaction repositories in use, inject it as a first parameter\n                    argsWithInjectedTransactionManagerAndRepositories = [entityManager, ...args];\n\n                } else {\n                    argsWithInjectedTransactionManagerAndRepositories = [...args];\n                }\n\n                // for every usage of @TransactionRepository decorator\n                transactionRepositoryMetadatas.forEach(metadata => {\n                    let repositoryInstance: any;\n\n                    // detect type of the repository and get instance from transaction entity manager\n                    switch (metadata.repositoryType) {\n                        case Repository:\n                            repositoryInstance = entityManager.getRepository(metadata.entityType!);\n                            break;\n                        case MongoRepository:\n                            repositoryInstance = entityManager.getMongoRepository(metadata.entityType!);\n                            break;\n                        case TreeRepository:\n                            repositoryInstance = entityManager.getTreeRepository(metadata.entityType!);\n                            break;\n                        // if not the TypeORM's ones, there must be custom repository classes\n                        default:\n                            repositoryInstance = entityManager.getCustomRepository(metadata.repositoryType);\n                    }\n\n                    // replace method param with injection of repository instance\n                    argsWithInjectedTransactionManagerAndRepositories.splice(metadata.index, 0, repositoryInstance);\n                });\n\n                return originalMethod.apply(this, argsWithInjectedTransactionManagerAndRepositories);\n            };\n            if (isolationLevel) {\n                return getConnection(connectionName).manager.transaction(isolationLevel, transactionCallback);\n            } else {\n                return getConnection(connectionName).manager.transaction(transactionCallback);\n            }\n        };\n    };\n}\n"],"sourceRoot":"../.."}