|
1 | | -// Database client for NeonDB |
2 | | -// TODO: Implement NeonDB connection |
| 1 | +import postgres from "postgres"; |
| 2 | +import type { SearchResult, SpecContent } from "../types"; |
3 | 3 |
|
4 | | -export function createDbClient(_connectionString: string) { |
5 | | - throw new Error("Not implemented yet"); |
| 4 | +export type DbClient = ReturnType<typeof createDbClient>; |
| 5 | + |
| 6 | +export function createDbClient(connectionString: string) { |
| 7 | + const sql = postgres(connectionString); |
| 8 | + |
| 9 | + return { |
| 10 | + sql, |
| 11 | + |
| 12 | + async close() { |
| 13 | + await sql.end(); |
| 14 | + }, |
| 15 | + |
| 16 | + // Insert content |
| 17 | + async insert(content: Omit<SpecContent, "id">) { |
| 18 | + const [result] = await sql<[{ id: number }]>` |
| 19 | + INSERT INTO spec_content (part_number, section_id, title, content, content_type, embedding) |
| 20 | + VALUES ( |
| 21 | + ${content.partNumber}, |
| 22 | + ${content.sectionId}, |
| 23 | + ${content.title}, |
| 24 | + ${content.content}, |
| 25 | + ${content.contentType}, |
| 26 | + ${content.embedding ? `[${content.embedding.join(",")}]` : null} |
| 27 | + ) |
| 28 | + RETURNING id |
| 29 | + `; |
| 30 | + return result.id; |
| 31 | + }, |
| 32 | + |
| 33 | + // Insert multiple (batch) |
| 34 | + async insertBatch(items: Omit<SpecContent, "id">[]) { |
| 35 | + const values = items.map((item) => ({ |
| 36 | + part_number: item.partNumber, |
| 37 | + section_id: item.sectionId, |
| 38 | + title: item.title, |
| 39 | + content: item.content, |
| 40 | + content_type: item.contentType, |
| 41 | + embedding: item.embedding ? `[${item.embedding.join(",")}]` : null, |
| 42 | + })); |
| 43 | + |
| 44 | + const result = await sql` |
| 45 | + INSERT INTO spec_content ${sql(values)} |
| 46 | + RETURNING id |
| 47 | + `; |
| 48 | + return result.map((r) => r.id as number); |
| 49 | + }, |
| 50 | + |
| 51 | + // Update embedding |
| 52 | + async updateEmbedding(id: number, embedding: number[]) { |
| 53 | + await sql` |
| 54 | + UPDATE spec_content |
| 55 | + SET embedding = ${`[${embedding.join(",")}]`} |
| 56 | + WHERE id = ${id} |
| 57 | + `; |
| 58 | + }, |
| 59 | + |
| 60 | + // Semantic search |
| 61 | + async search( |
| 62 | + queryEmbedding: number[], |
| 63 | + options: { limit?: number; partNumber?: number; contentType?: string } = {}, |
| 64 | + ): Promise<SearchResult[]> { |
| 65 | + const { limit = 5, partNumber, contentType } = options; |
| 66 | + const embeddingStr = `[${queryEmbedding.join(",")}]`; |
| 67 | + |
| 68 | + const results = await sql< |
| 69 | + Array<{ |
| 70 | + id: number; |
| 71 | + part_number: number; |
| 72 | + section_id: string | null; |
| 73 | + title: string | null; |
| 74 | + content: string; |
| 75 | + content_type: string; |
| 76 | + score: number; |
| 77 | + }> |
| 78 | + >` |
| 79 | + SELECT |
| 80 | + id, part_number, section_id, title, content, content_type, |
| 81 | + 1 - (embedding <=> ${embeddingStr}::vector) as score |
| 82 | + FROM spec_content |
| 83 | + WHERE embedding IS NOT NULL |
| 84 | + ${partNumber ? sql`AND part_number = ${partNumber}` : sql``} |
| 85 | + ${contentType ? sql`AND content_type = ${contentType}` : sql``} |
| 86 | + ORDER BY embedding <=> ${embeddingStr}::vector |
| 87 | + LIMIT ${limit} |
| 88 | + `; |
| 89 | + |
| 90 | + return results.map((r) => ({ |
| 91 | + id: r.id, |
| 92 | + partNumber: r.part_number, |
| 93 | + sectionId: r.section_id, |
| 94 | + title: r.title, |
| 95 | + content: r.content, |
| 96 | + contentType: r.content_type, |
| 97 | + score: r.score, |
| 98 | + })); |
| 99 | + }, |
| 100 | + |
| 101 | + // Get by section |
| 102 | + async getBySection(partNumber: number, sectionId: string): Promise<SpecContent[]> { |
| 103 | + const results = await sql< |
| 104 | + Array<{ |
| 105 | + id: number; |
| 106 | + part_number: number; |
| 107 | + section_id: string | null; |
| 108 | + title: string | null; |
| 109 | + content: string; |
| 110 | + content_type: string; |
| 111 | + }> |
| 112 | + >` |
| 113 | + SELECT id, part_number, section_id, title, content, content_type |
| 114 | + FROM spec_content |
| 115 | + WHERE part_number = ${partNumber} AND section_id = ${sectionId} |
| 116 | + ORDER BY id |
| 117 | + `; |
| 118 | + |
| 119 | + return results.map((r) => ({ |
| 120 | + id: r.id, |
| 121 | + partNumber: r.part_number, |
| 122 | + sectionId: r.section_id, |
| 123 | + title: r.title, |
| 124 | + content: r.content, |
| 125 | + contentType: r.content_type, |
| 126 | + })); |
| 127 | + }, |
| 128 | + |
| 129 | + // Get stats |
| 130 | + async getStats() { |
| 131 | + const [stats] = await sql<[{ total: number; embedded: number }]>` |
| 132 | + SELECT |
| 133 | + COUNT(*) as total, |
| 134 | + COUNT(*) FILTER (WHERE embedding IS NOT NULL) as embedded |
| 135 | + FROM spec_content |
| 136 | + `; |
| 137 | + return { |
| 138 | + total: Number(stats.total), |
| 139 | + embedded: Number(stats.embedded), |
| 140 | + }; |
| 141 | + }, |
| 142 | + |
| 143 | + // Clear all |
| 144 | + async clearAll() { |
| 145 | + await sql`TRUNCATE spec_content RESTART IDENTITY`; |
| 146 | + }, |
| 147 | + }; |
6 | 148 | } |
0 commit comments