diff --git a/apps/api/src/db.ts b/apps/api/src/db.ts index 336fc70..16d85fe 100644 --- a/apps/api/src/db.ts +++ b/apps/api/src/db.ts @@ -3,7 +3,17 @@ import { PrismaClient } from "./generated/prisma/client.js"; export type Db = PrismaClient; -/** `connections`: the pool's size (node-postgres keeps 10 by default); the worker sizes it to what it runs at once. */ +/** + * `connections`: the pool's size (node-postgres keeps 10 by default); the worker sizes it to what it runs at once. + * + * `transactionOptions` raises Prisma's default `maxWait` (2000ms) for any call that doesn't set its own — + * including the implicit transaction Prisma opens around a single `upsert()` — so brief pool contention + * doesn't surface as "Unable to start a transaction in the given time". Call sites with their own long-running + * `$transaction(fn, { maxWait, timeout })` options (e.g. `rebuildDirectory`, `assignGalleries`) override this. + */ export function createDb(databaseUrl: string, connections?: number): Db { - return new PrismaClient({ adapter: new PrismaPg({ connectionString: databaseUrl, ...(connections && { max: connections }) }) }); + return new PrismaClient({ + adapter: new PrismaPg({ connectionString: databaseUrl, ...(connections && { max: connections }) }), + transactionOptions: { maxWait: 10_000, timeout: 10_000 }, + }); }