forked from pingdotgg/t3code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectionThreadProposedPlans.ts
More file actions
127 lines (115 loc) · 4.17 KB
/
ProjectionThreadProposedPlans.ts
File metadata and controls
127 lines (115 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { Effect, Layer } from "effect";
import * as SqlClient from "effect/unstable/sql/SqlClient";
import * as SqlSchema from "effect/unstable/sql/SqlSchema";
import { toPersistenceSqlError } from "../Errors.ts";
import {
DeleteProjectionThreadProposedPlanByIdInput,
DeleteProjectionThreadProposedPlansInput,
ListProjectionThreadProposedPlansInput,
ProjectionThreadProposedPlan,
ProjectionThreadProposedPlanRepository,
type ProjectionThreadProposedPlanRepositoryShape,
} from "../Services/ProjectionThreadProposedPlans.ts";
const makeProjectionThreadProposedPlanRepository = Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
const upsertProjectionThreadProposedPlanRow = SqlSchema.void({
Request: ProjectionThreadProposedPlan,
execute: (row) => sql`
INSERT INTO projection_thread_proposed_plans (
plan_id,
thread_id,
turn_id,
plan_markdown,
implemented_at,
implementation_thread_id,
created_at,
updated_at
)
VALUES (
${row.planId},
${row.threadId},
${row.turnId},
${row.planMarkdown},
${row.implementedAt},
${row.implementationThreadId},
${row.createdAt},
${row.updatedAt}
)
ON CONFLICT (plan_id)
DO UPDATE SET
thread_id = excluded.thread_id,
turn_id = excluded.turn_id,
plan_markdown = excluded.plan_markdown,
implemented_at = excluded.implemented_at,
implementation_thread_id = excluded.implementation_thread_id,
created_at = excluded.created_at,
updated_at = excluded.updated_at
`,
});
const listProjectionThreadProposedPlanRows = SqlSchema.findAll({
Request: ListProjectionThreadProposedPlansInput,
Result: ProjectionThreadProposedPlan,
execute: ({ threadId }) => sql`
SELECT
plan_id AS "planId",
thread_id AS "threadId",
turn_id AS "turnId",
plan_markdown AS "planMarkdown",
implemented_at AS "implementedAt",
implementation_thread_id AS "implementationThreadId",
created_at AS "createdAt",
updated_at AS "updatedAt"
FROM projection_thread_proposed_plans
WHERE thread_id = ${threadId}
ORDER BY created_at ASC, plan_id ASC
`,
});
const deleteProjectionThreadProposedPlanRows = SqlSchema.void({
Request: DeleteProjectionThreadProposedPlansInput,
execute: ({ threadId }) => sql`
DELETE FROM projection_thread_proposed_plans
WHERE thread_id = ${threadId}
`,
});
const deleteProjectionThreadProposedPlanRowById = SqlSchema.void({
Request: DeleteProjectionThreadProposedPlanByIdInput,
execute: ({ planId }) => sql`
DELETE FROM projection_thread_proposed_plans
WHERE plan_id = ${planId}
`,
});
const upsert: ProjectionThreadProposedPlanRepositoryShape["upsert"] = (row) =>
upsertProjectionThreadProposedPlanRow(row).pipe(
Effect.mapError(toPersistenceSqlError("ProjectionThreadProposedPlanRepository.upsert:query")),
);
const listByThreadId: ProjectionThreadProposedPlanRepositoryShape["listByThreadId"] = (input) =>
listProjectionThreadProposedPlanRows(input).pipe(
Effect.mapError(
toPersistenceSqlError("ProjectionThreadProposedPlanRepository.listByThreadId:query"),
),
);
const deleteByThreadId: ProjectionThreadProposedPlanRepositoryShape["deleteByThreadId"] = (
input,
) =>
deleteProjectionThreadProposedPlanRows(input).pipe(
Effect.mapError(
toPersistenceSqlError("ProjectionThreadProposedPlanRepository.deleteByThreadId:query"),
),
);
const deleteByPlanId: ProjectionThreadProposedPlanRepositoryShape["deleteByPlanId"] = (input) =>
deleteProjectionThreadProposedPlanRowById(input).pipe(
Effect.mapError(
toPersistenceSqlError("ProjectionThreadProposedPlanRepository.deleteByPlanId:query"),
),
);
return {
upsert,
listByThreadId,
deleteByThreadId,
deleteByPlanId,
} satisfies ProjectionThreadProposedPlanRepositoryShape;
});
export const ProjectionThreadProposedPlanRepositoryLive = Layer.effect(
ProjectionThreadProposedPlanRepository,
makeProjectionThreadProposedPlanRepository,
);