@nodeboot/starter-persistence
Version:
Nodeboot starter package for persistence. Supports data access layer auto-configuration providing features like database initialization, consistency check, entity mapping, repository pattern, transactions, paging, migrations, persistence listeners, persis
90 lines • 2.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.runInTransaction = exports.runOnTransactionComplete = exports.runOnTransactionRollback = exports.runOnTransactionCommit = void 0;
const typeorm_transactional_1 = require("typeorm-transactional");
/**
* Registers a callback to be executed when the current transaction is successfully committed.
*
* @param cb - The callback function to execute on transaction commit.
*
* @example
* ```ts
* runOnTransactionCommit(() => {
* console.log("Transaction committed successfully");
* });
* ```
*
* @author
* Manuel Santos <https://github.com/manusant>
*/
const runOnTransactionCommit = (cb) => {
return (0, typeorm_transactional_1.runOnTransactionCommit)(cb);
};
exports.runOnTransactionCommit = runOnTransactionCommit;
/**
* Registers a callback to be executed when the current transaction is rolled back.
*
* @param cb - The callback function that receives the error that caused the rollback.
*
* @example
* ```ts
* runOnTransactionRollback((err) => {
* console.error("Transaction rolled back", err);
* });
* ```
*
* @author
* Manuel Santos <https://github.com/manusant>
*/
const runOnTransactionRollback = (cb) => {
return (0, typeorm_transactional_1.runOnTransactionRollback)(cb);
};
exports.runOnTransactionRollback = runOnTransactionRollback;
/**
* Registers a callback to be executed when the current transaction is either committed or rolled back.
*
* @param cb - The callback function that receives an optional error (if the transaction was rolled back).
*
* @example
* ```ts
* runOnTransactionComplete((err) => {
* if (err) {
* console.error("Transaction failed", err);
* } else {
* console.log("Transaction completed successfully");
* }
* });
* ```
*
* @author
* Manuel Santos <https://github.com/manusant>
*/
const runOnTransactionComplete = (cb) => {
return (0, typeorm_transactional_1.runOnTransactionComplete)(cb);
};
exports.runOnTransactionComplete = runOnTransactionComplete;
/**
* Executes a given function within a transaction.
*
* @template F - The function type to be executed inside the transaction.
* @param fn - The function to be executed within the transaction.
* @param options - Optional transaction options (e.g., isolation level, propagation).
* @returns The result of the function execution within the transaction.
*
* @example
* ```ts
* const result = await runInTransaction(async () => {
* await userRepo.save(user);
* await auditRepo.log("User created");
* return "done";
* });
* ```
*
* @author
* Manuel Santos <https://github.com/manusant>
*/
const runInTransaction = (fn, options) => {
return (0, typeorm_transactional_1.wrapInTransaction)(fn, options)();
};
exports.runInTransaction = runInTransaction;
//# sourceMappingURL=transaction.hooks.js.map