Fix merge conflicts and add withTransaction helper

- Add withTransaction helper that gracefully handles nested transactions
  (reuses existing transaction in tests, starts new one otherwise)
- Update auth procedures to use withTransaction instead of direct .transaction()
- Add email config to all e2e test contexts (required by merged code)
- Remove duplicate verification token code from signup procedure

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
igm
2026-01-12 17:07:14 +08:00
parent 8e65c2e698
commit e43c006bb1
10 changed files with 82 additions and 20 deletions

View File

@@ -0,0 +1,34 @@
import type { Database } from "@reviq/db-schema";
import type { Kysely, Transaction } from "kysely";
/**
* Type for a database connection that could be either a Kysely instance or a Transaction
*/
export type DbConnection = Kysely<Database> | Transaction<Database>;
/**
* Execute a callback within a transaction, handling nested transaction scenarios.
*
* If the provided db is already a transaction, the callback is executed directly
* without starting a new transaction (since Kysely doesn't support nested transactions).
*
* If the provided db is a regular Kysely instance, a new transaction is started.
*
* @param db - Database connection (Kysely instance or Transaction)
* @param callback - Function to execute within the transaction
* @returns The result of the callback
*/
export async function withTransaction<T>(
db: DbConnection,
callback: (trx: Transaction<Database>) => Promise<T>,
): Promise<T> {
// Check if db is already a transaction
// Kysely Transaction objects have isTransaction = true
if ("isTransaction" in db && db.isTransaction) {
// Already in a transaction, execute callback directly
return callback(db as Transaction<Database>);
}
// Not in a transaction, start one
return (db as Kysely<Database>).transaction().execute(callback);
}

View File

@@ -33,6 +33,10 @@ export {
parseToken,
TOKEN_PREFIX,
} from "./helpers/token.js";
export {
type DbConnection,
withTransaction,
} from "./helpers/with-transaction.js";
/**
* Export model operations
*/