typeorm-test-transactions
Version:
A transactional wrapper for tests that use TypeORM that automatically rolls back the transaction at the end of the test.
108 lines • 4.76 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runInMultiConnectionTransaction = exports.runInTransaction = void 0;
const typeorm_transactional_cls_hooked_1 = require("typeorm-transactional-cls-hooked");
const rollback_error_exception_1 = require("./exceptions/rollback-error-exception");
const cls_hooked_1 = require("cls-hooked");
const scope = (0, cls_hooked_1.createNamespace)('recursiveContext');
/**
* Runs the code in a transaction and runs rollback on the transaction at the
* end of it.
* @param func The function you want run in a transaction
*/
function runInTransaction(func) {
return async () => {
try {
await TransactionCreator.run(func);
}
catch (e) {
if (e instanceof rollback_error_exception_1.RollbackErrorException) {
// Do nothing here, the transaction has now been rolled back.
}
else {
throw e;
}
}
};
}
exports.runInTransaction = runInTransaction;
/**
* Runs a function in a nested transaction for each connection specified
* @param connections The connections to run the transactions in
* @param func The function you want run in a transaction
*
* Thanks @Dzeri96 for this contribution
* https://github.com/Dzeri96/typeorm-test-transactions/tree/nested-transactions
*/
function runInMultiConnectionTransaction(connections, func) {
return async () => {
if (connections && connections.length != 0) {
await scope.runPromise(async () => {
scope.set('connections', connections);
try {
await TransactionCreator.runWithMultipleConnections(func);
}
catch (e) {
if (e instanceof rollback_error_exception_1.RollbackErrorException) {
// Do nothing here, the transaction has now been rolled back.
}
else {
throw e;
}
}
});
}
else {
throw 'Connection array is empty. Consider using runInTransaction() instead.';
}
};
}
exports.runInMultiConnectionTransaction = runInMultiConnectionTransaction;
class TransactionCreator {
static async run(func) {
await func();
// Once the function has run, we throw an exception to ensure that the
// transaction rolls back.
throw new rollback_error_exception_1.RollbackErrorException(`This is thrown to cause a rollback on the transaction.`);
}
static async runWithConnection(func) {
await func();
// Once the function has run, we throw an exception to ensure that the
// transaction rolls back.
throw new rollback_error_exception_1.RollbackErrorException(`This is thrown to cause a rollback on the transaction.`);
}
static async runWithMultipleConnections(func) {
const connections = scope.get('connections') || [];
if (connections.length != 0) {
await TransactionCreator.runWithConnection(async () => await TransactionCreator.runWithMultipleConnections(func));
}
else {
await TransactionCreator.runWithConnection(func);
}
}
}
__decorate([
(0, typeorm_transactional_cls_hooked_1.Transactional)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Function]),
__metadata("design:returntype", Promise)
], TransactionCreator, "run", null);
__decorate([
(0, typeorm_transactional_cls_hooked_1.Transactional)({
connectionName: () => scope.get('connections').pop(),
propagation: typeorm_transactional_cls_hooked_1.Propagation.NESTED,
}),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Function]),
__metadata("design:returntype", Promise)
], TransactionCreator, "runWithConnection", null);
//# sourceMappingURL=run-in-transaction.js.map