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