server: refactor pagination

* fix transaction usgae in repos
* other bug fixes
This commit is contained in:
Philipinho
2024-04-01 01:23:52 +01:00
parent ade3a5b589
commit 4913975e99
38 changed files with 648 additions and 756 deletions

View File

@ -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
}
}