Skip to content

Commit 2a4bb9c

Browse files
committed
read replica for all remaining v3 presenters
1 parent fb81d06 commit 2a4bb9c

13 files changed

+55
-123
lines changed

apps/webapp/app/presenters/v3/ApiKeysPresenter.server.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
import { PrismaClient, prisma } from "~/db.server";
21
import { Project } from "~/models/project.server";
32
import { User } from "~/models/user.server";
43
import { sortEnvironments } from "~/utils/environmentSort";
4+
import { BasePresenter } from "./basePresenter.server";
55

6-
export class ApiKeysPresenter {
7-
#prismaClient: PrismaClient;
8-
9-
constructor(prismaClient: PrismaClient = prisma) {
10-
this.#prismaClient = prismaClient;
11-
}
12-
6+
export class ApiKeysPresenter extends BasePresenter {
137
public async call({ userId, projectSlug }: { userId: User["id"]; projectSlug: Project["slug"] }) {
14-
const environments = await this.#prismaClient.runtimeEnvironment.findMany({
8+
const environments = await this._replica.runtimeEnvironment.findMany({
159
select: {
1610
id: true,
1711
apiKey: true,

apps/webapp/app/presenters/v3/DeploymentListPresenter.server.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
import { WorkerDeploymentStatus } from "@trigger.dev/database";
2-
import { sqlDatabaseSchema, PrismaClient, prisma } from "~/db.server";
2+
import { sqlDatabaseSchema } from "~/db.server";
33
import { Organization } from "~/models/organization.server";
44
import { Project } from "~/models/project.server";
55
import { User } from "~/models/user.server";
66
import { getUsername } from "~/utils/username";
7+
import { BasePresenter } from "./basePresenter.server";
78

89
const pageSize = 20;
910

1011
export type DeploymentList = Awaited<ReturnType<DeploymentListPresenter["call"]>>;
1112
export type DeploymentListItem = DeploymentList["deployments"][0];
1213

13-
export class DeploymentListPresenter {
14-
#prismaClient: PrismaClient;
15-
16-
constructor(prismaClient: PrismaClient = prisma) {
17-
this.#prismaClient = prismaClient;
18-
}
19-
14+
export class DeploymentListPresenter extends BasePresenter {
2015
public async call({
2116
userId,
2217
projectSlug,
@@ -28,7 +23,7 @@ export class DeploymentListPresenter {
2823
organizationSlug: Organization["slug"];
2924
page?: number;
3025
}) {
31-
const project = await this.#prismaClient.project.findFirstOrThrow({
26+
const project = await this._replica.project.findFirstOrThrow({
3227
select: {
3328
id: true,
3429
environments: {
@@ -63,13 +58,13 @@ export class DeploymentListPresenter {
6358
},
6459
});
6560

66-
const totalCount = await this.#prismaClient.workerDeployment.count({
61+
const totalCount = await this._replica.workerDeployment.count({
6762
where: {
6863
projectId: project.id,
6964
},
7065
});
7166

72-
const labeledDeployments = await this.#prismaClient.workerDeploymentPromotion.findMany({
67+
const labeledDeployments = await this._replica.workerDeploymentPromotion.findMany({
7368
where: {
7469
environmentId: {
7570
in: project.environments.map((env) => env.id),
@@ -81,7 +76,7 @@ export class DeploymentListPresenter {
8176
},
8277
});
8378

84-
const deployments = await this.#prismaClient.$queryRaw<
79+
const deployments = await this._replica.$queryRaw<
8580
{
8681
id: string;
8782
shortCode: string;

apps/webapp/app/presenters/v3/DeploymentPresenter.server.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,22 @@ import {
44
TaskMetadataFailedToParseData,
55
groupTaskMetadataIssuesByTask,
66
} from "@trigger.dev/core/v3";
7-
import { WorkerDeployment, WorkerDeploymentStatus } from "@trigger.dev/database";
7+
import { WorkerDeployment } from "@trigger.dev/database";
88
import { z } from "zod";
9-
import { PrismaClient, prisma } from "~/db.server";
109
import { Organization } from "~/models/organization.server";
1110
import { Project } from "~/models/project.server";
1211
import { User } from "~/models/user.server";
1312
import { safeJsonParse } from "~/utils/json";
1413
import { getUsername } from "~/utils/username";
14+
import { BasePresenter } from "./basePresenter.server";
1515

1616
export type ErrorData = {
1717
name: string;
1818
message: string;
1919
stack?: string;
2020
};
2121

22-
export class DeploymentPresenter {
23-
#prismaClient: PrismaClient;
24-
25-
constructor(prismaClient: PrismaClient = prisma) {
26-
this.#prismaClient = prismaClient;
27-
}
28-
22+
export class DeploymentPresenter extends BasePresenter {
2923
public async call({
3024
userId,
3125
projectSlug,
@@ -37,7 +31,7 @@ export class DeploymentPresenter {
3731
organizationSlug: Organization["slug"];
3832
deploymentShortCode: WorkerDeployment["shortCode"];
3933
}) {
40-
const project = await this.#prismaClient.project.findFirstOrThrow({
34+
const project = await this._replica.project.findFirstOrThrow({
4135
select: {
4236
id: true,
4337
organizationId: true,
@@ -55,7 +49,7 @@ export class DeploymentPresenter {
5549
},
5650
});
5751

58-
const deployment = await this.#prismaClient.workerDeployment.findUniqueOrThrow({
52+
const deployment = await this._replica.workerDeployment.findUniqueOrThrow({
5953
where: {
6054
projectId_shortCode: {
6155
projectId: project.id,

apps/webapp/app/presenters/v3/EditSchedulePresenter.server.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { RuntimeEnvironmentType } from "@trigger.dev/database";
2-
import { PrismaClient, prisma } from "~/db.server";
32
import { displayableEnvironment } from "~/models/runtimeEnvironment.server";
3+
import { BasePresenter } from "./basePresenter.server";
44

55
type EditScheduleOptions = {
66
userId: string;
@@ -16,16 +16,10 @@ type Environment = {
1616
userName?: string;
1717
};
1818

19-
export class EditSchedulePresenter {
20-
#prismaClient: PrismaClient;
21-
22-
constructor(prismaClient: PrismaClient = prisma) {
23-
this.#prismaClient = prismaClient;
24-
}
25-
19+
export class EditSchedulePresenter extends BasePresenter {
2620
public async call({ userId, projectSlug, friendlyId }: EditScheduleOptions) {
2721
// Find the project scoped to the organization
28-
const project = await this.#prismaClient.project.findFirstOrThrow({
22+
const project = await this._replica.project.findFirstOrThrow({
2923
select: {
3024
id: true,
3125
environments: {
@@ -59,7 +53,7 @@ export class EditSchedulePresenter {
5953
},
6054
});
6155

62-
const possibleTasks = await this.#prismaClient.backgroundWorkerTask.findMany({
56+
const possibleTasks = await this._replica.backgroundWorkerTask.findMany({
6357
distinct: ["slug"],
6458
where: {
6559
projectId: project.id,
@@ -83,7 +77,7 @@ export class EditSchedulePresenter {
8377
return undefined;
8478
}
8579

86-
const schedule = await this.#prismaClient.taskSchedule.findFirst({
80+
const schedule = await this._replica.taskSchedule.findFirst({
8781
select: {
8882
id: true,
8983
friendlyId: true,

apps/webapp/app/presenters/v3/EnvironmentVariablesPresenter.server.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
import { PrismaClient, prisma } from "~/db.server";
21
import { Project } from "~/models/project.server";
32
import { User } from "~/models/user.server";
43
import { sortEnvironments } from "~/utils/environmentSort";
54
import { EnvironmentVariablesRepository } from "~/v3/environmentVariables/environmentVariablesRepository.server";
5+
import { BasePresenter } from "./basePresenter.server";
66

77
type Result = Awaited<ReturnType<EnvironmentVariablesPresenter["call"]>>;
88
export type EnvironmentVariableWithSetValues = Result["environmentVariables"][number];
99

10-
export class EnvironmentVariablesPresenter {
11-
#prismaClient: PrismaClient;
12-
13-
constructor(prismaClient: PrismaClient = prisma) {
14-
this.#prismaClient = prismaClient;
15-
}
16-
10+
export class EnvironmentVariablesPresenter extends BasePresenter {
1711
public async call({ userId, projectSlug }: { userId: User["id"]; projectSlug: Project["slug"] }) {
18-
const project = await this.#prismaClient.project.findUnique({
12+
const project = await this._replica.project.findUnique({
1913
select: {
2014
id: true,
2115
},
@@ -35,7 +29,7 @@ export class EnvironmentVariablesPresenter {
3529
throw new Error("Project not found");
3630
}
3731

38-
const environmentVariables = await this.#prismaClient.environmentVariable.findMany({
32+
const environmentVariables = await this._replica.environmentVariable.findMany({
3933
select: {
4034
id: true,
4135
key: true,
@@ -65,7 +59,7 @@ export class EnvironmentVariablesPresenter {
6559
},
6660
});
6761

68-
const environments = await this.#prismaClient.runtimeEnvironment.findMany({
62+
const environments = await this._replica.runtimeEnvironment.findMany({
6963
select: {
7064
id: true,
7165
type: true,
@@ -99,7 +93,7 @@ export class EnvironmentVariablesPresenter {
9993
(e) => e.orgMember?.userId === userId || e.orgMember === null
10094
);
10195

102-
const repository = new EnvironmentVariablesRepository(this.#prismaClient);
96+
const repository = new EnvironmentVariablesRepository(this._replica);
10397
const variables = await repository.getProject(project.id);
10498

10599
return {

apps/webapp/app/presenters/v3/RunPresenter.server.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
import { millisecondsToNanoseconds } from "@trigger.dev/core/v3";
22
import { createTreeFromFlatItems, flattenTree } from "~/components/primitives/TreeView/TreeView";
3-
import { PrismaClient, prisma } from "~/db.server";
43
import { getUsername } from "~/utils/username";
54
import { eventRepository } from "~/v3/eventRepository.server";
5+
import { BasePresenter } from "./basePresenter.server";
66

77
type Result = Awaited<ReturnType<RunPresenter["call"]>>;
88
export type Run = Result["run"];
99
export type RunEvent = NonNullable<Result["trace"]>["events"][0];
1010

11-
export class RunPresenter {
12-
#prismaClient: PrismaClient;
13-
14-
constructor(prismaClient: PrismaClient = prisma) {
15-
this.#prismaClient = prismaClient;
16-
}
17-
11+
export class RunPresenter extends BasePresenter {
1812
public async call({
1913
userId,
2014
projectSlug,
@@ -26,7 +20,7 @@ export class RunPresenter {
2620
organizationSlug: string;
2721
runFriendlyId: string;
2822
}) {
29-
const run = await this.#prismaClient.taskRun.findFirstOrThrow({
23+
const run = await this._replica.taskRun.findFirstOrThrow({
3024
select: {
3125
id: true,
3226
number: true,

apps/webapp/app/presenters/v3/RunStreamPresenter.server.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
import { TaskRun } from "@trigger.dev/database";
22
import { eventStream } from "remix-utils/sse/server";
3-
import { PrismaClient, prisma } from "~/db.server";
43
import { logger } from "~/services/logger.server";
54
import { throttle } from "~/utils/throttle";
65
import { eventRepository } from "~/v3/eventRepository.server";
6+
import { BasePresenter } from "./basePresenter.server";
77

88
const pingInterval = 1000;
99

10-
export class RunStreamPresenter {
11-
#prismaClient: PrismaClient;
12-
13-
constructor(prismaClient: PrismaClient = prisma) {
14-
this.#prismaClient = prismaClient;
15-
}
16-
10+
export class RunStreamPresenter extends BasePresenter {
1711
public async call({
1812
request,
1913
runFriendlyId,
2014
}: {
2115
request: Request;
2216
runFriendlyId: TaskRun["friendlyId"];
2317
}) {
24-
const run = await this.#prismaClient.taskRun.findUnique({
18+
const run = await this._replica.taskRun.findUnique({
2519
where: {
2620
friendlyId: runFriendlyId,
2721
},

apps/webapp/app/presenters/v3/TaskPresenter.server.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
import { BackgroundWorkerTask } from "@trigger.dev/database";
2-
import { PrismaClient, prisma } from "~/db.server";
32
import { Project } from "~/models/project.server";
43
import { User } from "~/models/user.server";
4+
import { BasePresenter } from "./basePresenter.server";
55

6-
export class TaskPresenter {
7-
#prismaClient: PrismaClient;
8-
9-
constructor(prismaClient: PrismaClient = prisma) {
10-
this.#prismaClient = prismaClient;
11-
}
12-
6+
export class TaskPresenter extends BasePresenter {
137
public async call({
148
userId,
159
taskFriendlyId,
@@ -19,7 +13,7 @@ export class TaskPresenter {
1913
taskFriendlyId: BackgroundWorkerTask["friendlyId"];
2014
projectSlug: Project["slug"];
2115
}) {
22-
const task = await this.#prismaClient.backgroundWorkerTask.findFirst({
16+
const task = await this._replica.backgroundWorkerTask.findFirst({
2317
select: {
2418
id: true,
2519
slug: true,

apps/webapp/app/presenters/v3/TasksStreamPresenter.server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ type RunWithAttempts = {
1616
const pingInterval = 1000;
1717

1818
export class TasksStreamPresenter {
19-
#prismaClient: PrismaClient;
19+
_replica: PrismaClient;
2020

2121
constructor(prismaClient: PrismaClient = prisma) {
22-
this.#prismaClient = prismaClient;
22+
this._replica = prismaClient;
2323
}
2424

2525
public async call({
@@ -33,7 +33,7 @@ export class TasksStreamPresenter {
3333
projectSlug: string;
3434
userId: string;
3535
}) {
36-
const project = await this.#prismaClient.project.findUnique({
36+
const project = await this._replica.project.findUnique({
3737
where: {
3838
slug: projectSlug,
3939
organization: {

apps/webapp/app/presenters/v3/TestPresenter.server.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { TestSearchParams } from "~/routes/_app.orgs.$organizationSlug.projects.
44
import { sortEnvironments } from "~/utils/environmentSort";
55
import { createSearchParams } from "~/utils/searchParams";
66
import { findCurrentWorkerDeployment } from "~/v3/models/workerDeployment.server";
7+
import { BasePresenter } from "./basePresenter.server";
78

89
type TaskListOptions = {
910
userId: string;
@@ -15,16 +16,10 @@ export type TaskList = Awaited<ReturnType<TestPresenter["call"]>>;
1516
export type TaskListItem = NonNullable<TaskList["tasks"]>[0];
1617
export type SelectedEnvironment = NonNullable<TaskList["selectedEnvironment"]>;
1718

18-
export class TestPresenter {
19-
#prismaClient: PrismaClient;
20-
21-
constructor(prismaClient: PrismaClient = prisma) {
22-
this.#prismaClient = prismaClient;
23-
}
24-
19+
export class TestPresenter extends BasePresenter {
2520
public async call({ userId, projectSlug, url }: TaskListOptions) {
2621
// Find the project scoped to the organization
27-
const project = await this.#prismaClient.project.findFirstOrThrow({
22+
const project = await this._replica.project.findFirstOrThrow({
2823
select: {
2924
id: true,
3025
environments: {
@@ -107,7 +102,7 @@ export class TestPresenter {
107102

108103
async #getTasks(envId: string, isDev: boolean) {
109104
if (isDev) {
110-
return await this.#prismaClient.$queryRaw<
105+
return await this._replica.$queryRaw<
111106
{
112107
id: string;
113108
version: string;

0 commit comments

Comments
 (0)