Files
docmost-ryan/apps/server/src/kysely/utils.ts
Philipinho 4913975e99 server: refactor pagination
* fix transaction usgae in repos
* other bug fixes
2024-04-01 01:23:52 +01:00

34 lines
1.1 KiB
TypeScript

import { KyselyDB, KyselyTransaction } from './types/kysely.types';
/*
* Executes a transaction or a callback using the provided database instance.
* If an existing transaction is provided, it directly executes the callback with it.
* Otherwise, it starts a new transaction using the provided database instance and executes the callback within that transaction.
*/
export async function executeTx<T>(
db: KyselyDB,
callback: (trx: KyselyTransaction) => Promise<T>,
existingTrx?: KyselyTransaction,
): Promise<T> {
if (existingTrx) {
return await callback(existingTrx); // Execute callback with existing transaction
} else {
return await db.transaction().execute((trx) => callback(trx)); // Start new transaction and execute callback
}
}
/*
* This function returns either an existing transaction if provided,
* or the normal database instance.
*/
export function dbOrTx(
db: KyselyDB,
existingTrx?: KyselyTransaction,
): KyselyDB | KyselyTransaction {
if (existingTrx) {
return existingTrx; // Use existing transaction
} else {
return db; // Use normal database instance
}
}