perf: set global prisma transaction timeouts and reduce transaction scope (#2607)

Configure default transaction options (5s maxWait, 10s timeout) on the
PrismaClient instead of per-transaction overrides. Move side effects
like email sending, webhook triggers, and job dispatches out of
$transaction blocks to avoid holding database connections open during
network I/O.

Also extracts the direct template email into a background job and fixes
a bug where prisma was used instead of tx inside a transaction.
This commit is contained in:
Lucas Smith
2026-03-13 14:51:53 +11:00
committed by GitHub
parent 76d96d2f65
commit 9f680c7a61
20 changed files with 455 additions and 364 deletions
@@ -528,7 +528,7 @@ export const createDocumentFromTemplate = async ({
}),
});
return await prisma.$transaction(async (tx) => {
const { envelope, createdEnvelope } = await prisma.$transaction(async (tx) => {
const envelope = await tx.envelope.create({
data: {
id: prefixedId('envelope'),
@@ -761,13 +761,17 @@ export const createDocumentFromTemplate = async ({
throw new Error('Document not found');
}
await triggerWebhook({
event: WebhookTriggerEvents.DOCUMENT_CREATED,
data: ZWebhookDocumentSchema.parse(mapEnvelopeToWebhookDocumentPayload(createdEnvelope)),
userId,
teamId,
});
return envelope;
return { envelope, createdEnvelope };
});
// Trigger webhook outside the transaction to avoid holding the connection
// open during network I/O.
await triggerWebhook({
event: WebhookTriggerEvents.DOCUMENT_CREATED,
data: ZWebhookDocumentSchema.parse(mapEnvelopeToWebhookDocumentPayload(createdEnvelope)),
userId,
teamId,
});
return envelope;
};