mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
add server to monorepo.
This commit is contained in:
36
packages/server/src/services/UnitOfWork/TransactionsHooks.ts
Normal file
36
packages/server/src/services/UnitOfWork/TransactionsHooks.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* @param {any} maybeTrx
|
||||
* @returns {maybeTrx is import('objection').TransactionOrKnex & { executionPromise: Promise<any> }}
|
||||
*/
|
||||
function checkIsTransaction(maybeTrx) {
|
||||
return Boolean(maybeTrx && maybeTrx.executionPromise);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for a transaction to be complete.
|
||||
* @param {import('objection').TransactionOrKnex} [trx]
|
||||
*/
|
||||
export async function waitForTransaction(trx) {
|
||||
return Promise.resolve(checkIsTransaction(trx) ? trx.executionPromise : null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a callback when the transaction is done.
|
||||
* @param {import('objection').TransactionOrKnex | undefined} trx
|
||||
* @param {Function} callback
|
||||
*/
|
||||
export function runAfterTransaction(trx, callback) {
|
||||
waitForTransaction(trx).then(
|
||||
() => {
|
||||
// If transaction success, then run action
|
||||
return Promise.resolve(callback()).catch((error) => {
|
||||
setTimeout(() => {
|
||||
throw error;
|
||||
});
|
||||
});
|
||||
},
|
||||
() => {
|
||||
// Ignore transaction error
|
||||
}
|
||||
);
|
||||
}
|
||||
56
packages/server/src/services/UnitOfWork/index.ts
Normal file
56
packages/server/src/services/UnitOfWork/index.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { Service, Inject } from 'typedi';
|
||||
import TenancyService from '@/services/Tenancy/TenancyService';
|
||||
|
||||
/**
|
||||
* Enumeration that represents transaction isolation levels for use with the {@link Transactional} annotation
|
||||
*/
|
||||
export enum IsolationLevel {
|
||||
/**
|
||||
* A constant indicating that dirty reads, non-repeatable reads and phantom reads can occur.
|
||||
*/
|
||||
READ_UNCOMMITTED = 'read uncommitted',
|
||||
/**
|
||||
* A constant indicating that dirty reads are prevented; non-repeatable reads and phantom reads can occur.
|
||||
*/
|
||||
READ_COMMITTED = 'read committed',
|
||||
/**
|
||||
* A constant indicating that dirty reads and non-repeatable reads are prevented; phantom reads can occur.
|
||||
*/
|
||||
REPEATABLE_READ = 'repeatable read',
|
||||
/**
|
||||
* A constant indicating that dirty reads, non-repeatable reads and phantom reads are prevented.
|
||||
*/
|
||||
SERIALIZABLE = 'serializable',
|
||||
}
|
||||
|
||||
@Service()
|
||||
export default class UnitOfWork {
|
||||
@Inject()
|
||||
tenancy: TenancyService;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {number} tenantId
|
||||
* @param {} work
|
||||
* @param {IsolationLevel} isolationLevel
|
||||
* @returns {}
|
||||
*/
|
||||
public withTransaction = async (
|
||||
tenantId: number,
|
||||
work,
|
||||
isolationLevel: IsolationLevel = IsolationLevel.READ_UNCOMMITTED
|
||||
) => {
|
||||
const knex = this.tenancy.knex(tenantId);
|
||||
const trx = await knex.transaction({ isolationLevel });
|
||||
|
||||
try {
|
||||
const result = await work(trx);
|
||||
trx.commit();
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
trx.rollback();
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user