mirror of
https://github.com/Shadowfita/docmost.git
synced 2025-11-16 01:31:09 +10:00
server: refactor pagination
* fix transaction usgae in repos * other bug fixes
This commit is contained in:
@ -1,13 +1,33 @@
|
||||
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);
|
||||
return await callback(existingTrx); // Execute callback with existing transaction
|
||||
} else {
|
||||
return await db.transaction().execute((trx) => callback(trx));
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user