|
| 1 | +import * as SqlClient from "effect/unstable/sql/SqlClient"; |
| 2 | +import * as SqlSchema from "effect/unstable/sql/SqlSchema"; |
| 3 | +import { Effect, Layer, Option, Schema } from "effect"; |
| 4 | +import { toPersistenceDecodeError, toPersistenceSqlError } from "../Errors.ts"; |
| 5 | +import { |
| 6 | + DecisionCaseRepository, |
| 7 | + DecisionCaseRow, |
| 8 | + GetDecisionCaseInput, |
| 9 | + ListDecisionCasesInput, |
| 10 | + type DecisionCaseRepositoryShape, |
| 11 | +} from "../Services/DecisionCases.ts"; |
| 12 | + |
| 13 | +function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: string) { |
| 14 | + return (cause: unknown) => |
| 15 | + Schema.isSchemaError(cause) |
| 16 | + ? toPersistenceDecodeError(decodeOperation)(cause) |
| 17 | + : toPersistenceSqlError(sqlOperation)(cause); |
| 18 | +} |
| 19 | + |
| 20 | +const makeDecisionCaseRepository = Effect.gen(function* () { |
| 21 | + const sql = yield* SqlClient.SqlClient; |
| 22 | + |
| 23 | + const upsertRow = SqlSchema.void({ |
| 24 | + Request: DecisionCaseRow, |
| 25 | + execute: (row) => |
| 26 | + sql` |
| 27 | + INSERT INTO decision_cases ( |
| 28 | + case_id, project_id, cwd, source_kind, source_id, title, conflict_kind, |
| 29 | + linked_thread_id, created_at, updated_at |
| 30 | + ) VALUES ( |
| 31 | + ${row.caseId}, ${row.projectId}, ${row.cwd}, ${row.sourceKind}, ${row.sourceId}, ${row.title}, |
| 32 | + ${row.conflictKind}, ${row.linkedThreadId}, ${row.createdAt}, ${row.updatedAt} |
| 33 | + ) |
| 34 | + ON CONFLICT (case_id) |
| 35 | + DO UPDATE SET |
| 36 | + project_id = excluded.project_id, |
| 37 | + cwd = excluded.cwd, |
| 38 | + source_kind = excluded.source_kind, |
| 39 | + source_id = excluded.source_id, |
| 40 | + title = excluded.title, |
| 41 | + conflict_kind = excluded.conflict_kind, |
| 42 | + linked_thread_id = excluded.linked_thread_id, |
| 43 | + updated_at = excluded.updated_at |
| 44 | + `, |
| 45 | + }); |
| 46 | + |
| 47 | + const getRow = SqlSchema.findOneOption({ |
| 48 | + Request: GetDecisionCaseInput, |
| 49 | + Result: DecisionCaseRow, |
| 50 | + execute: ({ caseId }) => |
| 51 | + sql` |
| 52 | + SELECT |
| 53 | + case_id AS "caseId", |
| 54 | + project_id AS "projectId", |
| 55 | + cwd, |
| 56 | + source_kind AS "sourceKind", |
| 57 | + source_id AS "sourceId", |
| 58 | + title, |
| 59 | + conflict_kind AS "conflictKind", |
| 60 | + linked_thread_id AS "linkedThreadId", |
| 61 | + created_at AS "createdAt", |
| 62 | + updated_at AS "updatedAt" |
| 63 | + FROM decision_cases |
| 64 | + WHERE case_id = ${caseId} |
| 65 | + `, |
| 66 | + }); |
| 67 | + |
| 68 | + const listRows = SqlSchema.findAll({ |
| 69 | + Request: ListDecisionCasesInput, |
| 70 | + Result: DecisionCaseRow, |
| 71 | + execute: ({ cwd }) => |
| 72 | + sql` |
| 73 | + SELECT |
| 74 | + case_id AS "caseId", |
| 75 | + project_id AS "projectId", |
| 76 | + cwd, |
| 77 | + source_kind AS "sourceKind", |
| 78 | + source_id AS "sourceId", |
| 79 | + title, |
| 80 | + conflict_kind AS "conflictKind", |
| 81 | + linked_thread_id AS "linkedThreadId", |
| 82 | + created_at AS "createdAt", |
| 83 | + updated_at AS "updatedAt" |
| 84 | + FROM decision_cases |
| 85 | + WHERE cwd = ${cwd} |
| 86 | + ORDER BY updated_at DESC |
| 87 | + `, |
| 88 | + }); |
| 89 | + |
| 90 | + const upsert: DecisionCaseRepositoryShape["upsert"] = (row) => |
| 91 | + upsertRow(row).pipe( |
| 92 | + Effect.mapError( |
| 93 | + toPersistenceSqlOrDecodeError( |
| 94 | + "DecisionCaseRepository.upsert:query", |
| 95 | + "DecisionCaseRepository.upsert:encodeRequest", |
| 96 | + ), |
| 97 | + ), |
| 98 | + ); |
| 99 | + |
| 100 | + const getById: DecisionCaseRepositoryShape["getById"] = (input) => |
| 101 | + getRow(input).pipe( |
| 102 | + Effect.mapError( |
| 103 | + toPersistenceSqlOrDecodeError( |
| 104 | + "DecisionCaseRepository.getById:query", |
| 105 | + "DecisionCaseRepository.getById:decodeRow", |
| 106 | + ), |
| 107 | + ), |
| 108 | + Effect.flatMap((rowOption) => |
| 109 | + Option.match(rowOption, { |
| 110 | + onNone: () => Effect.succeed(Option.none()), |
| 111 | + onSome: (row) => |
| 112 | + Effect.succeed(Option.some(row as Schema.Schema.Type<typeof DecisionCaseRow>)), |
| 113 | + }), |
| 114 | + ), |
| 115 | + ); |
| 116 | + |
| 117 | + const listByCwd: DecisionCaseRepositoryShape["listByCwd"] = (input) => |
| 118 | + listRows(input).pipe( |
| 119 | + Effect.mapError( |
| 120 | + toPersistenceSqlOrDecodeError( |
| 121 | + "DecisionCaseRepository.listByCwd:query", |
| 122 | + "DecisionCaseRepository.listByCwd:decodeRows", |
| 123 | + ), |
| 124 | + ), |
| 125 | + Effect.map((rows) => rows as ReadonlyArray<Schema.Schema.Type<typeof DecisionCaseRow>>), |
| 126 | + ); |
| 127 | + |
| 128 | + return { upsert, getById, listByCwd } satisfies DecisionCaseRepositoryShape; |
| 129 | +}); |
| 130 | + |
| 131 | +export const DecisionCaseRepositoryLive = Layer.effect( |
| 132 | + DecisionCaseRepository, |
| 133 | + makeDecisionCaseRepository, |
| 134 | +); |
0 commit comments