|
| 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 | + |
| 5 | +import { toPersistenceDecodeError, toPersistenceSqlError } from "../Errors.ts"; |
| 6 | + |
| 7 | +import { |
| 8 | + DeleteSmeDocumentInput, |
| 9 | + GetSmeDocumentInput, |
| 10 | + ListSmeDocumentsByProjectInput, |
| 11 | + SmeKnowledgeDocumentRepository, |
| 12 | + SmeKnowledgeDocumentRow, |
| 13 | + type SmeKnowledgeDocumentRepositoryShape, |
| 14 | +} from "../Services/SmeKnowledgeDocuments.ts"; |
| 15 | + |
| 16 | +function toPersistenceSqlOrDecodeError(sqlOperation: string, decodeOperation: string) { |
| 17 | + return (cause: unknown) => |
| 18 | + Schema.isSchemaError(cause) |
| 19 | + ? toPersistenceDecodeError(decodeOperation)(cause) |
| 20 | + : toPersistenceSqlError(sqlOperation)(cause); |
| 21 | +} |
| 22 | + |
| 23 | +const makeSmeKnowledgeDocumentRepository = Effect.gen(function* () { |
| 24 | + const sql = yield* SqlClient.SqlClient; |
| 25 | + |
| 26 | + const upsertRow = SqlSchema.void({ |
| 27 | + Request: SmeKnowledgeDocumentRow, |
| 28 | + execute: (row) => |
| 29 | + sql` |
| 30 | + INSERT INTO sme_knowledge_documents ( |
| 31 | + document_id, project_id, title, file_name, mime_type, |
| 32 | + size_bytes, content_text, content_hash, created_at, updated_at, deleted_at |
| 33 | + ) |
| 34 | + VALUES ( |
| 35 | + ${row.documentId}, ${row.projectId}, ${row.title}, ${row.fileName}, ${row.mimeType}, |
| 36 | + ${row.sizeBytes}, ${row.contentText}, ${row.contentHash}, ${row.createdAt}, ${row.updatedAt}, ${row.deletedAt} |
| 37 | + ) |
| 38 | + ON CONFLICT (document_id) |
| 39 | + DO UPDATE SET |
| 40 | + title = excluded.title, |
| 41 | + file_name = excluded.file_name, |
| 42 | + mime_type = excluded.mime_type, |
| 43 | + size_bytes = excluded.size_bytes, |
| 44 | + content_text = excluded.content_text, |
| 45 | + content_hash = excluded.content_hash, |
| 46 | + updated_at = excluded.updated_at, |
| 47 | + deleted_at = excluded.deleted_at |
| 48 | + `, |
| 49 | + }); |
| 50 | + |
| 51 | + const getRow = SqlSchema.findOneOption({ |
| 52 | + Request: GetSmeDocumentInput, |
| 53 | + Result: SmeKnowledgeDocumentRow, |
| 54 | + execute: ({ documentId }) => |
| 55 | + sql` |
| 56 | + SELECT |
| 57 | + document_id AS "documentId", |
| 58 | + project_id AS "projectId", |
| 59 | + title, |
| 60 | + file_name AS "fileName", |
| 61 | + mime_type AS "mimeType", |
| 62 | + size_bytes AS "sizeBytes", |
| 63 | + content_text AS "contentText", |
| 64 | + content_hash AS "contentHash", |
| 65 | + created_at AS "createdAt", |
| 66 | + updated_at AS "updatedAt", |
| 67 | + deleted_at AS "deletedAt" |
| 68 | + FROM sme_knowledge_documents |
| 69 | + WHERE document_id = ${documentId} |
| 70 | + `, |
| 71 | + }); |
| 72 | + |
| 73 | + const listRows = SqlSchema.findAll({ |
| 74 | + Request: ListSmeDocumentsByProjectInput, |
| 75 | + Result: SmeKnowledgeDocumentRow, |
| 76 | + execute: ({ projectId }) => |
| 77 | + sql` |
| 78 | + SELECT |
| 79 | + document_id AS "documentId", |
| 80 | + project_id AS "projectId", |
| 81 | + title, |
| 82 | + file_name AS "fileName", |
| 83 | + mime_type AS "mimeType", |
| 84 | + size_bytes AS "sizeBytes", |
| 85 | + content_text AS "contentText", |
| 86 | + content_hash AS "contentHash", |
| 87 | + created_at AS "createdAt", |
| 88 | + updated_at AS "updatedAt", |
| 89 | + deleted_at AS "deletedAt" |
| 90 | + FROM sme_knowledge_documents |
| 91 | + WHERE project_id = ${projectId} AND deleted_at IS NULL |
| 92 | + ORDER BY created_at ASC |
| 93 | + `, |
| 94 | + }); |
| 95 | + |
| 96 | + const deleteRow = SqlSchema.void({ |
| 97 | + Request: DeleteSmeDocumentInput, |
| 98 | + execute: ({ documentId }) => |
| 99 | + sql` |
| 100 | + UPDATE sme_knowledge_documents |
| 101 | + SET deleted_at = datetime('now') |
| 102 | + WHERE document_id = ${documentId} |
| 103 | + `, |
| 104 | + }); |
| 105 | + |
| 106 | + const upsert: SmeKnowledgeDocumentRepositoryShape["upsert"] = (row) => |
| 107 | + upsertRow(row).pipe( |
| 108 | + Effect.mapError( |
| 109 | + toPersistenceSqlOrDecodeError( |
| 110 | + "SmeKnowledgeDocumentRepository.upsert:query", |
| 111 | + "SmeKnowledgeDocumentRepository.upsert:encodeRequest", |
| 112 | + ), |
| 113 | + ), |
| 114 | + ); |
| 115 | + |
| 116 | + const getById: SmeKnowledgeDocumentRepositoryShape["getById"] = (input) => |
| 117 | + getRow(input).pipe( |
| 118 | + Effect.mapError( |
| 119 | + toPersistenceSqlOrDecodeError( |
| 120 | + "SmeKnowledgeDocumentRepository.getById:query", |
| 121 | + "SmeKnowledgeDocumentRepository.getById:decodeRow", |
| 122 | + ), |
| 123 | + ), |
| 124 | + Effect.flatMap((rowOption) => |
| 125 | + Option.match(rowOption, { |
| 126 | + onNone: () => Effect.succeed(Option.none()), |
| 127 | + onSome: (row) => |
| 128 | + Effect.succeed(Option.some(row as Schema.Schema.Type<typeof SmeKnowledgeDocumentRow>)), |
| 129 | + }), |
| 130 | + ), |
| 131 | + ); |
| 132 | + |
| 133 | + const listByProjectId: SmeKnowledgeDocumentRepositoryShape["listByProjectId"] = (input) => |
| 134 | + listRows(input).pipe( |
| 135 | + Effect.mapError( |
| 136 | + toPersistenceSqlOrDecodeError( |
| 137 | + "SmeKnowledgeDocumentRepository.listByProjectId:query", |
| 138 | + "SmeKnowledgeDocumentRepository.listByProjectId:decodeRows", |
| 139 | + ), |
| 140 | + ), |
| 141 | + Effect.map( |
| 142 | + (rows) => rows as ReadonlyArray<Schema.Schema.Type<typeof SmeKnowledgeDocumentRow>>, |
| 143 | + ), |
| 144 | + ); |
| 145 | + |
| 146 | + const deleteById: SmeKnowledgeDocumentRepositoryShape["deleteById"] = (input) => |
| 147 | + deleteRow(input).pipe( |
| 148 | + Effect.mapError(toPersistenceSqlError("SmeKnowledgeDocumentRepository.deleteById:query")), |
| 149 | + ); |
| 150 | + |
| 151 | + return { |
| 152 | + upsert, |
| 153 | + getById, |
| 154 | + listByProjectId, |
| 155 | + deleteById, |
| 156 | + } satisfies SmeKnowledgeDocumentRepositoryShape; |
| 157 | +}); |
| 158 | + |
| 159 | +export const SmeKnowledgeDocumentRepositoryLive = Layer.effect( |
| 160 | + SmeKnowledgeDocumentRepository, |
| 161 | + makeSmeKnowledgeDocumentRepository, |
| 162 | +); |
0 commit comments