Skip to content

Commit cf1c311

Browse files
authored
Use the replica for API auth queries (#2932)
When we auth API keys we get the environment, project and org. This is a very hot path so even though these queries are fast they contribute a significant percentage of total load. This moves them to use the read replica instead.
1 parent fb94e17 commit cf1c311

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

apps/webapp/app/models/project.server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { nanoid, customAlphabet } from "nanoid";
22
import slug from "slug";
3-
import { prisma } from "~/db.server";
3+
import { $replica, prisma } from "~/db.server";
44
import type { Project } from "@trigger.dev/database";
55
import { Organization, createEnvironment } from "./organization.server";
66
import { env } from "~/env.server";
@@ -135,7 +135,7 @@ export async function createProject(
135135

136136
export async function findProjectBySlug(orgSlug: string, projectSlug: string, userId: string) {
137137
// Find the project scoped to the organization, making sure the user belongs to that org
138-
return await prisma.project.findFirst({
138+
return await $replica.project.findFirst({
139139
where: {
140140
slug: projectSlug,
141141
organization: {
@@ -148,7 +148,7 @@ export async function findProjectBySlug(orgSlug: string, projectSlug: string, us
148148

149149
export async function findProjectByRef(externalRef: string, userId: string) {
150150
// Find the project scoped to the organization, making sure the user belongs to that org
151-
return await prisma.project.findFirst({
151+
return await $replica.project.findFirst({
152152
where: {
153153
externalRef,
154154
organization: {

apps/webapp/app/models/runtimeEnvironment.server.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { AuthenticatedEnvironment } from "@internal/run-engine";
22
import type { Prisma, PrismaClientOrTransaction, RuntimeEnvironment } from "@trigger.dev/database";
3-
import { prisma } from "~/db.server";
3+
import { $replica, prisma } from "~/db.server";
44
import { logger } from "~/services/logger.server";
55
import { getUsername } from "~/utils/username";
66
import { sanitizeBranchName } from "~/v3/gitBranch";
@@ -11,7 +11,7 @@ export async function findEnvironmentByApiKey(
1111
apiKey: string,
1212
branchName: string | undefined
1313
): Promise<AuthenticatedEnvironment | null> {
14-
const environment = await prisma.runtimeEnvironment.findFirst({
14+
const environment = await $replica.runtimeEnvironment.findFirst({
1515
where: {
1616
apiKey,
1717
},
@@ -67,7 +67,7 @@ export async function findEnvironmentByPublicApiKey(
6767
apiKey: string,
6868
branchName: string | undefined
6969
): Promise<AuthenticatedEnvironment | null> {
70-
const environment = await prisma.runtimeEnvironment.findFirst({
70+
const environment = await $replica.runtimeEnvironment.findFirst({
7171
where: {
7272
pkApiKey: apiKey,
7373
},
@@ -89,7 +89,7 @@ export async function findEnvironmentByPublicApiKey(
8989
export async function findEnvironmentById(
9090
id: string
9191
): Promise<(AuthenticatedEnvironment & { parentEnvironment: { apiKey: string } | null }) | null> {
92-
const environment = await prisma.runtimeEnvironment.findFirst({
92+
const environment = await $replica.runtimeEnvironment.findFirst({
9393
where: {
9494
id,
9595
},
@@ -118,7 +118,7 @@ export async function findEnvironmentBySlug(
118118
envSlug: string,
119119
userId: string
120120
): Promise<AuthenticatedEnvironment | null> {
121-
return prisma.runtimeEnvironment.findFirst({
121+
return $replica.runtimeEnvironment.findFirst({
122122
where: {
123123
projectId: projectId,
124124
slug: envSlug,
@@ -148,7 +148,7 @@ export async function findEnvironmentFromRun(
148148
runId: string,
149149
tx?: PrismaClientOrTransaction
150150
): Promise<AuthenticatedEnvironment | null> {
151-
const taskRun = await (tx ?? prisma).taskRun.findFirst({
151+
const taskRun = await (tx ?? $replica).taskRun.findFirst({
152152
where: {
153153
id: runId,
154154
},
@@ -223,7 +223,7 @@ export async function disconnectSession(environmentId: string) {
223223
}
224224

225225
export async function findLatestSession(environmentId: string) {
226-
const session = await prisma.runtimeEnvironmentSession.findFirst({
226+
const session = await $replica.runtimeEnvironmentSession.findFirst({
227227
where: {
228228
environmentId,
229229
},
@@ -280,7 +280,7 @@ export async function findDisplayableEnvironment(
280280
environmentId: string,
281281
userId: string | undefined
282282
) {
283-
const environment = await prisma.runtimeEnvironment.findFirst({
283+
const environment = await $replica.runtimeEnvironment.findFirst({
284284
where: {
285285
id: environmentId,
286286
},

apps/webapp/app/services/apiAuth.server.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { type Prettify } from "@trigger.dev/core";
33
import { SignJWT, errors, jwtVerify } from "jose";
44
import { z } from "zod";
55

6-
import { prisma } from "~/db.server";
6+
import { $replica } from "~/db.server";
77
import { env } from "~/env.server";
88
import { findProjectByRef } from "~/models/project.server";
99
import {
@@ -479,7 +479,7 @@ export async function authenticatedEnvironmentForAuthentication(
479479
return auth.result.environment;
480480
}
481481
case "personalAccessToken": {
482-
const user = await prisma.user.findUnique({
482+
const user = await $replica.user.findUnique({
483483
where: {
484484
id: auth.result.userId,
485485
},
@@ -498,7 +498,7 @@ export async function authenticatedEnvironmentForAuthentication(
498498
const sanitizedBranch = sanitizeBranchName(branch);
499499

500500
if (!sanitizedBranch) {
501-
const environment = await prisma.runtimeEnvironment.findFirst({
501+
const environment = await $replica.runtimeEnvironment.findFirst({
502502
where: {
503503
projectId: project.id,
504504
slug: slug,
@@ -523,7 +523,7 @@ export async function authenticatedEnvironmentForAuthentication(
523523
return environment;
524524
}
525525

526-
const environment = await prisma.runtimeEnvironment.findFirst({
526+
const environment = await $replica.runtimeEnvironment.findFirst({
527527
where: {
528528
projectId: project.id,
529529
type: "PREVIEW",
@@ -553,7 +553,7 @@ export async function authenticatedEnvironmentForAuthentication(
553553
};
554554
}
555555
case "organizationAccessToken": {
556-
const organization = await prisma.organization.findUnique({
556+
const organization = await $replica.organization.findUnique({
557557
where: {
558558
id: auth.result.organizationId,
559559
},
@@ -563,7 +563,7 @@ export async function authenticatedEnvironmentForAuthentication(
563563
throw json({ error: "Invalid or missing organization access token" }, { status: 401 });
564564
}
565565

566-
const project = await prisma.project.findFirst({
566+
const project = await $replica.project.findFirst({
567567
where: {
568568
organizationId: organization.id,
569569
externalRef: projectRef,
@@ -577,7 +577,7 @@ export async function authenticatedEnvironmentForAuthentication(
577577
const sanitizedBranch = sanitizeBranchName(branch);
578578

579579
if (!sanitizedBranch) {
580-
const environment = await prisma.runtimeEnvironment.findFirst({
580+
const environment = await $replica.runtimeEnvironment.findFirst({
581581
where: {
582582
projectId: project.id,
583583
slug: slug,
@@ -595,7 +595,7 @@ export async function authenticatedEnvironmentForAuthentication(
595595
return environment;
596596
}
597597

598-
const environment = await prisma.runtimeEnvironment.findFirst({
598+
const environment = await $replica.runtimeEnvironment.findFirst({
599599
where: {
600600
projectId: project.id,
601601
type: "PREVIEW",

0 commit comments

Comments
 (0)