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

OrchestrationCommandReceipts.ts

126 lines3.8 KB

1import * as SqlClient from "effect/unstable/sql/SqlClient";
2import * as SqlSchema from "effect/unstable/sql/SqlSchema";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5​
6import { toPersistenceSqlError } from "../Errors.ts";
7​
8import {
9 GetByCommandIdInput,
10 OrchestrationCommandReceipt,
11 OrchestrationCommandReceiptRepository,
12 type OrchestrationCommandReceiptRepositoryShape,
13} from "../Services/OrchestrationCommandReceipts.ts";
14​
15const makeOrchestrationCommandReceiptRepository = Effect.gen(function* () {
16 const sql = yield* SqlClient.SqlClient;
17​
18 const upsertReceiptRow = SqlSchema.void({
19 Request: OrchestrationCommandReceipt,
20 execute: (receipt) =>
21 sql`
22 INSERT INTO orchestration_command_receipts (
23 command_id,
24 aggregate_kind,
25 aggregate_id,
26 command_type,
27 accepted_at,
28 result_sequence,
29 status,
30 error
31 )
32 VALUES (
33 ${receipt.commandId},
34 ${receipt.aggregateKind},
35 ${receipt.aggregateId},
36 ${receipt.commandType},
37 ${receipt.acceptedAt},
38 ${receipt.resultSequence},
39 ${receipt.status},
40 ${receipt.error}
41 )
42 ON CONFLICT (command_id)
43 DO UPDATE SET
44 aggregate_kind = excluded.aggregate_kind,
45 aggregate_id = excluded.aggregate_id,
46 command_type = excluded.command_type,
47 accepted_at = excluded.accepted_at,
48 result_sequence = excluded.result_sequence,
49 status = excluded.status,
50 error = excluded.error
51 `,
52 });
53​
54 const findReceiptByCommandId = SqlSchema.findOneOption({
55 Request: GetByCommandIdInput,
56 Result: OrchestrationCommandReceipt,
57 execute: ({ commandId }) =>
58 sql`
59 SELECT
60 command_id AS "commandId",
61 aggregate_kind AS "aggregateKind",
62 aggregate_id AS "aggregateId",
63 command_type AS "commandType",
64 accepted_at AS "acceptedAt",
65 result_sequence AS "resultSequence",
66 status,
67 error
68 FROM orchestration_command_receipts
69 WHERE command_id = ${commandId}
70 `,
71 });
72​
73 const upsert: OrchestrationCommandReceiptRepositoryShape["upsert"] = (receipt) =>
74 upsertReceiptRow(receipt).pipe(
75 Effect.mapError(toPersistenceSqlError("OrchestrationCommandReceiptRepository.upsert:query")),
76 );
77​
78 const insertIfAbsent: OrchestrationCommandReceiptRepositoryShape["insertIfAbsent"] = (receipt) =>
79 sql<{ readonly command_id: string }>`
80 INSERT INTO orchestration_command_receipts (
81 command_id,
82 aggregate_kind,
83 aggregate_id,
84 command_type,
85 accepted_at,
86 result_sequence,
87 status,
88 error
89 )
90 VALUES (
91 ${receipt.commandId},
92 ${receipt.aggregateKind},
93 ${receipt.aggregateId},
94 ${receipt.commandType},
95 ${receipt.acceptedAt},
96 ${receipt.resultSequence},
97 ${receipt.status},
98 ${receipt.error}
99 )
100 ON CONFLICT(command_id) DO NOTHING
101 RETURNING command_id
102 `.pipe(
103 Effect.map((rows) => rows.length === 1),
104 Effect.mapError(
105 toPersistenceSqlError("OrchestrationCommandReceiptRepository.insertIfAbsent:query"),
106 ),
107 );
108​
109 const getByCommandId: OrchestrationCommandReceiptRepositoryShape["getByCommandId"] = (input) =>
110 findReceiptByCommandId(input).pipe(
111 Effect.mapError(
112 toPersistenceSqlError("OrchestrationCommandReceiptRepository.getByCommandId:query"),
113 ),
114 );
115​
116 return {
117 insertIfAbsent,
118 upsert,
119 getByCommandId,
120 } satisfies OrchestrationCommandReceiptRepositoryShape;
121});
122​
123export const OrchestrationCommandReceiptRepositoryLive = Layer.effect(
124 OrchestrationCommandReceiptRepository,
125 makeOrchestrationCommandReceiptRepository,
126);