parent
2732ade178
commit
5e56e072c1
@ -1,50 +1,50 @@ |
|||||||
import { ConnectionOptions } from "typeorm" |
import { ConnectionOptions } from "typeorm" |
||||||
import { |
import { |
||||||
initializeTransactionalContext, |
initializeTransactionalContext, |
||||||
patchTypeORMRepositoryWithBaseRepository, |
patchTypeORMRepositoryWithBaseRepository, |
||||||
Propagation, |
Propagation, |
||||||
Transactional, |
Transactional, |
||||||
} from "typeorm-transactional-cls-hooked" |
} from "typeorm-transactional-cls-hooked" |
||||||
import { connectionOptions } from "./connection" |
import { connectionOptions } from "./connection" |
||||||
|
|
||||||
export const testingConnectionOptions = () => { |
export const testingConnectionOptions = () => { |
||||||
const database = process.env.DB_NAME_TESING as string |
const database = process.env.DB_NAME_TESING as string |
||||||
|
|
||||||
return { ...connectionOptions(), database } as ConnectionOptions |
return { ...connectionOptions(), database } as ConnectionOptions |
||||||
} |
} |
||||||
|
|
||||||
export const initializeRollbackTransactions = () => { |
export const initializeRollbackTransactions = () => { |
||||||
initializeTransactionalContext() |
initializeTransactionalContext() |
||||||
patchTypeORMRepositoryWithBaseRepository() |
patchTypeORMRepositoryWithBaseRepository() |
||||||
} |
} |
||||||
|
|
||||||
type RunFunction = () => Promise<void> | void |
type RunFunction = () => Promise<void> | void |
||||||
|
|
||||||
class RollbackError extends Error { |
class RollbackError extends Error { |
||||||
constructor(message: string) { |
constructor(message: string) { |
||||||
super(message) |
super(message) |
||||||
|
|
||||||
this.name = this.constructor.name |
this.name = this.constructor.name |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
class TransactionCreator { |
class TransactionCreator { |
||||||
@Transactional({ propagation: Propagation.REQUIRED }) |
@Transactional({ propagation: Propagation.REQUIRED }) |
||||||
static async run(func: RunFunction) { |
static async run(func: RunFunction) { |
||||||
await func() |
await func() |
||||||
throw new RollbackError(`This is thrown to cause a rollback on the transaction.`) |
throw new RollbackError(`This is thrown to cause a rollback on the transaction.`) |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
export function runInRollbackTransaction(func: RunFunction) { |
export function runInRollbackTransaction(func: RunFunction) { |
||||||
return async () => { |
return async () => { |
||||||
try { |
try { |
||||||
await TransactionCreator.run(func) |
await TransactionCreator.run(func) |
||||||
} catch (e) { |
} catch (e) { |
||||||
/* istanbul ignore next */ |
/* istanbul ignore next */ |
||||||
if (!(e instanceof RollbackError)) { |
if (!(e instanceof RollbackError)) { |
||||||
throw e |
throw e |
||||||
} |
} |
||||||
} |
} |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue