UNPKG

miter

Version:

A typescript web framework based on ExpressJs based loosely on SailsJs

41 lines 2.48 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const transaction_1 = require("../../metadata/orm/transaction"); const clc_1 = require("../../util/clc"); function Transaction(transactionName, detach = false) { if (typeof transactionName !== 'string' && typeof transactionName !== 'undefined') { detach = !!transactionName; transactionName = undefined; } return function (prototype, methodName, routeFn) { let transactionFns = Reflect.getOwnMetadata(transaction_1.TransactionFunctionsSym, prototype) || []; if (!transactionFns.find(name => name == methodName)) transactionFns.push(methodName); Reflect.defineMetadata(transaction_1.TransactionFunctionsSym, transactionFns, prototype); let originalMethod = routeFn.value; routeFn.value = function (...args) { return __awaiter(this, void 0, void 0, function* () { let transactionService = this.transactionService || this['__transaction_service']; if (!transactionService) { let protoName = (prototype && prototype.constructor && prototype.constructor.name) || prototype; console.error(`${clc_1.clc.red('ERROR')}: Failed to wrap call to ${protoName}.${methodName} in a transaction. You need to dependency inject the TransactionService and call it 'transactionService'.`); return yield originalMethod.call(this, ...args); } return yield transactionService.run(transactionName || methodName, detach, () => __awaiter(this, void 0, void 0, function* () { return yield originalMethod.call(this, ...args); })); }); }; return routeFn; }; } exports.Transaction = Transaction; //# sourceMappingURL=transaction.decorator.js.map