Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProjectionThreadPullRequests.ts

266 lines9.0 KB

1import { normalizeThreadPullRequestKey } from "@t3tools/shared/threadPullRequests";
2import {
3 IsoDateTime,
4 PositiveInt,
5 ThreadId,
6 ThreadPullRequestKey,
7 ThreadPullRequestLinkSource,
8 ThreadPullRequestSnapshot,
9 ThreadPullRequestStack,
10 TrimmedNonEmptyString,
11} from "@t3tools/contracts";
12import * as Schema from "effect/Schema";
13import * as Context from "effect/Context";
14import * as Effect from "effect/Effect";
15​
16import { toPersistenceSqlError, type ProjectionRepositoryError } from "./Errors.ts";
17​
18import * as Layer from "effect/Layer";
19import * as Struct from "effect/Struct";
20import * as SqlClient from "effect/unstable/sql/SqlClient";
21import * as SqlSchema from "effect/unstable/sql/SqlSchema";
22​
23export const ProjectionThreadPullRequest = Schema.Struct({
24 threadId: ThreadId,
25 host: TrimmedNonEmptyString,
26 repository: TrimmedNonEmptyString,
27 number: PositiveInt,
28 url: TrimmedNonEmptyString,
29 source: ThreadPullRequestLinkSource,
30 linkedAt: IsoDateTime,
31 snapshot: Schema.NullOr(ThreadPullRequestSnapshot),
32 stack: Schema.NullOr(ThreadPullRequestStack),
33});
34export type ProjectionThreadPullRequest = typeof ProjectionThreadPullRequest.Type;
35​
36export const ListProjectionThreadPullRequestsInput = Schema.Struct({
37 threadId: ThreadId,
38});
39export type ListProjectionThreadPullRequestsInput =
40 typeof ListProjectionThreadPullRequestsInput.Type;
41​
42export const ListProjectionThreadPullRequestsByPullRequestInput = ThreadPullRequestKey;
43export type ListProjectionThreadPullRequestsByPullRequestInput =
44 typeof ListProjectionThreadPullRequestsByPullRequestInput.Type;
45​
46export const DeleteProjectionThreadPullRequestInput = Schema.Struct({
47 threadId: ThreadId,
48 ...ThreadPullRequestKey.fields,
49});
50export type DeleteProjectionThreadPullRequestInput =
51 typeof DeleteProjectionThreadPullRequestInput.Type;
52​
53export const DeleteProjectionThreadPullRequestsInput = Schema.Struct({
54 threadId: ThreadId,
55});
56export type DeleteProjectionThreadPullRequestsInput =
57 typeof DeleteProjectionThreadPullRequestsInput.Type;
58​
59export const DeleteProjectionThreadPullRequestsBySourceInput = Schema.Struct({
60 threadId: ThreadId,
61 source: ThreadPullRequestLinkSource,
62});
63export type DeleteProjectionThreadPullRequestsBySourceInput =
64 typeof DeleteProjectionThreadPullRequestsBySourceInput.Type;
65​
66const ProjectionThreadPullRequestDbRow = ProjectionThreadPullRequest.mapFields(
67 Struct.assign({
68 snapshot: Schema.NullOr(Schema.fromJsonString(ThreadPullRequestSnapshot)),
69 stack: Schema.NullOr(Schema.fromJsonString(ThreadPullRequestStack)),
70 }),
71);
72​
73export class ProjectionThreadPullRequestRepository extends Context.Service<
74 ProjectionThreadPullRequestRepository,
75 {
76 readonly upsert: (
77 row: ProjectionThreadPullRequest,
78 ) => Effect.Effect<void, ProjectionRepositoryError>;
79 readonly listByThreadId: (
80 input: ListProjectionThreadPullRequestsInput,
81 ) => Effect.Effect<ReadonlyArray<ProjectionThreadPullRequest>, ProjectionRepositoryError>;
82 readonly listByPullRequest: (
83 input: ListProjectionThreadPullRequestsByPullRequestInput,
84 ) => Effect.Effect<ReadonlyArray<ProjectionThreadPullRequest>, ProjectionRepositoryError>;
85 readonly delete: (
86 input: DeleteProjectionThreadPullRequestInput,
87 ) => Effect.Effect<void, ProjectionRepositoryError>;
88 readonly deleteByThreadId: (
89 input: DeleteProjectionThreadPullRequestsInput,
90 ) => Effect.Effect<void, ProjectionRepositoryError>;
91 readonly deleteByThreadIdAndSource: (
92 input: DeleteProjectionThreadPullRequestsBySourceInput,
93 ) => Effect.Effect<void, ProjectionRepositoryError>;
94 }
95>()("t3/persistence/ProjectionThreadPullRequests/ProjectionThreadPullRequestRepository") {}
96​
97/** @public Service construction is part of the canonical Effect module API. */
98export const make = Effect.gen(function* () {
99 const sql = yield* SqlClient.SqlClient;
100​
101 const upsertProjectionThreadPullRequestRow = SqlSchema.void({
102 Request: ProjectionThreadPullRequest,
103 execute: (row) => sql`
104 INSERT INTO projection_thread_pull_requests (
105 thread_id,
106 host,
107 repository,
108 number,
109 url,
110 source,
111 linked_at,
112 snapshot_json,
113 stack_json
114 )
115 VALUES (
116 ${row.threadId},
117 ${row.host},
118 ${row.repository},
119 ${row.number},
120 ${row.url},
121 ${row.source},
122 ${row.linkedAt},
123 ${row.snapshot === null ? null : JSON.stringify(row.snapshot)},
124 ${row.stack === null ? null : JSON.stringify(row.stack)}
125 )
126 ON CONFLICT (thread_id, host, repository, number)
127 DO UPDATE SET
128 url = excluded.url,
129 source = excluded.source,
130 linked_at = excluded.linked_at,
131 snapshot_json = excluded.snapshot_json,
132 stack_json = excluded.stack_json
133 `,
134 });
135​
136 const listProjectionThreadPullRequestRows = SqlSchema.findAll({
137 Request: ListProjectionThreadPullRequestsInput,
138 Result: ProjectionThreadPullRequestDbRow,
139 execute: ({ threadId }) => sql`
140 SELECT
141 thread_id AS "threadId",
142 host,
143 repository,
144 number,
145 url,
146 source,
147 linked_at AS "linkedAt",
148 snapshot_json AS "snapshot",
149 stack_json AS "stack"
150 FROM projection_thread_pull_requests
151 WHERE thread_id = ${threadId}
152 ORDER BY linked_at ASC, number ASC
153 `,
154 });
155​
156 const listProjectionThreadPullRequestRowsByPullRequest = SqlSchema.findAll({
157 Request: ListProjectionThreadPullRequestsByPullRequestInput,
158 Result: ProjectionThreadPullRequestDbRow,
159 execute: ({ host, repository, number }) => sql`
160 SELECT
161 thread_id AS "threadId",
162 host,
163 repository,
164 number,
165 url,
166 source,
167 linked_at AS "linkedAt",
168 snapshot_json AS "snapshot",
169 stack_json AS "stack"
170 FROM projection_thread_pull_requests
171 WHERE host = ${host}
172 AND repository = ${repository}
173 AND number = ${number}
174 ORDER BY linked_at ASC, thread_id ASC
175 `,
176 });
177​
178 const deleteProjectionThreadPullRequestRow = SqlSchema.void({
179 Request: DeleteProjectionThreadPullRequestInput,
180 execute: ({ threadId, host, repository, number }) => sql`
181 DELETE FROM projection_thread_pull_requests
182 WHERE thread_id = ${threadId}
183 AND host = ${host}
184 AND repository = ${repository}
185 AND number = ${number}
186 `,
187 });
188​
189 const deleteProjectionThreadPullRequestRows = SqlSchema.void({
190 Request: DeleteProjectionThreadPullRequestsInput,
191 execute: ({ threadId }) => sql`
192 DELETE FROM projection_thread_pull_requests
193 WHERE thread_id = ${threadId}
194 `,
195 });
196​
197 const deleteProjectionThreadPullRequestRowsBySource = SqlSchema.void({
198 Request: DeleteProjectionThreadPullRequestsBySourceInput,
199 execute: ({ threadId, source }) => sql`
200 DELETE FROM projection_thread_pull_requests
201 WHERE thread_id = ${threadId}
202 AND source = ${source}
203 `,
204 });
205​
206 const upsert: ProjectionThreadPullRequestRepository["Service"]["upsert"] = (row) =>
207 upsertProjectionThreadPullRequestRow({ ...row, ...normalizeThreadPullRequestKey(row) }).pipe(
208 Effect.mapError(toPersistenceSqlError("ProjectionThreadPullRequestRepository.upsert:query")),
209 );
210​
211 const listByThreadId: ProjectionThreadPullRequestRepository["Service"]["listByThreadId"] = (
212 input,
213 ) =>
214 listProjectionThreadPullRequestRows(input).pipe(
215 Effect.mapError(
216 toPersistenceSqlError("ProjectionThreadPullRequestRepository.listByThreadId:query"),
217 ),
218 );
219​
220 const listByPullRequest: ProjectionThreadPullRequestRepository["Service"]["listByPullRequest"] = (
221 input,
222 ) =>
223 listProjectionThreadPullRequestRowsByPullRequest(normalizeThreadPullRequestKey(input)).pipe(
224 Effect.mapError(
225 toPersistenceSqlError("ProjectionThreadPullRequestRepository.listByPullRequest:query"),
226 ),
227 );
228​
229 const deleteLink: ProjectionThreadPullRequestRepository["Service"]["delete"] = (input) =>
230 deleteProjectionThreadPullRequestRow({
231 ...input,
232 ...normalizeThreadPullRequestKey(input),
233 }).pipe(
234 Effect.mapError(toPersistenceSqlError("ProjectionThreadPullRequestRepository.delete:query")),
235 );
236​
237 const deleteByThreadId: ProjectionThreadPullRequestRepository["Service"]["deleteByThreadId"] = (
238 input,
239 ) =>
240 deleteProjectionThreadPullRequestRows(input).pipe(
241 Effect.mapError(
242 toPersistenceSqlError("ProjectionThreadPullRequestRepository.deleteByThreadId:query"),
243 ),
244 );
245​
246 const deleteByThreadIdAndSource: ProjectionThreadPullRequestRepository["Service"]["deleteByThreadIdAndSource"] =
247 (input) =>
248 deleteProjectionThreadPullRequestRowsBySource(input).pipe(
249 Effect.mapError(
250 toPersistenceSqlError(
251 "ProjectionThreadPullRequestRepository.deleteByThreadIdAndSource:query",
252 ),
253 ),
254 );
255​
256 return {
257 upsert,
258 listByThreadId,
259 listByPullRequest,
260 delete: deleteLink,
261 deleteByThreadId,
262 deleteByThreadIdAndSource,
263 } satisfies ProjectionThreadPullRequestRepository["Service"];
264});
265​
266export const layer = Layer.effect(ProjectionThreadPullRequestRepository, make);