|
| 1 | +import { neon } from "@neondatabase/serverless"; |
| 2 | + |
| 3 | +export type DocumentRow = { |
| 4 | + id: number; |
| 5 | + filename: string; |
| 6 | + r2Key: string; |
| 7 | + fileHash: string | null; |
| 8 | + status: string; |
| 9 | + createdAt: string; |
| 10 | +}; |
| 11 | + |
| 12 | +export function createNeonClient(databaseUrl: string) { |
| 13 | + const sql = neon(databaseUrl); |
| 14 | + |
| 15 | + return { |
| 16 | + async insertDocument( |
| 17 | + id: number, |
| 18 | + filename: string, |
| 19 | + r2Key: string, |
| 20 | + status = "ready", |
| 21 | + fileHash: string | null = null, |
| 22 | + ): Promise<void> { |
| 23 | + await sql`INSERT INTO documents (id, filename, r2_key, status, file_hash) VALUES (${id}, ${filename}, ${r2Key}, ${status}, ${fileHash})`; |
| 24 | + }, |
| 25 | + |
| 26 | + async findByHash(hash: string): Promise<DocumentRow | null> { |
| 27 | + const rows = |
| 28 | + await sql`SELECT id, filename, r2_key, file_hash, status, created_at FROM documents WHERE file_hash = ${hash} LIMIT 1`; |
| 29 | + if (rows.length === 0) return null; |
| 30 | + const r = rows[0]; |
| 31 | + return { |
| 32 | + id: r.id, |
| 33 | + filename: r.filename, |
| 34 | + r2Key: r.r2_key, |
| 35 | + fileHash: r.file_hash, |
| 36 | + status: r.status, |
| 37 | + createdAt: r.created_at, |
| 38 | + }; |
| 39 | + }, |
| 40 | + |
| 41 | + async insertChunks( |
| 42 | + chunks: Array<{ |
| 43 | + id: string; |
| 44 | + documentId: number; |
| 45 | + blockId: string; |
| 46 | + targetId: string; |
| 47 | + targetType: string; |
| 48 | + nodeType: string; |
| 49 | + content: string; |
| 50 | + contextType: string; |
| 51 | + metadata: string; |
| 52 | + embedding: number[]; |
| 53 | + }>, |
| 54 | + ): Promise<void> { |
| 55 | + for (const c of chunks) { |
| 56 | + const embeddingStr = `[${c.embedding.join(",")}]`; |
| 57 | + await sql`INSERT INTO chunks (id, document_id, block_id, target_id, target_type, node_type, content, context_type, metadata, embedding) VALUES (${c.id}, ${c.documentId}, ${c.blockId}, ${c.targetId}, ${c.targetType}, ${c.nodeType}, ${c.content}, ${c.contextType}, ${c.metadata}, ${embeddingStr}::vector)`; |
| 58 | + } |
| 59 | + }, |
| 60 | + |
| 61 | + async searchChunks( |
| 62 | + queryEmbedding: number[], |
| 63 | + limit = 8, |
| 64 | + ): Promise< |
| 65 | + Array<{ |
| 66 | + id: string; |
| 67 | + documentId: number; |
| 68 | + filename: string; |
| 69 | + blockId: string; |
| 70 | + targetId: string; |
| 71 | + targetType: string; |
| 72 | + nodeType: string; |
| 73 | + content: string; |
| 74 | + contextType: string; |
| 75 | + metadata: string; |
| 76 | + }> |
| 77 | + > { |
| 78 | + const embeddingStr = `[${queryEmbedding.join(",")}]`; |
| 79 | + const rows = await sql` |
| 80 | + SELECT c.id, c.document_id, d.filename, c.block_id, c.target_id, c.target_type, c.node_type, c.content, c.context_type, c.metadata |
| 81 | + FROM chunks c |
| 82 | + JOIN documents d ON d.id = c.document_id |
| 83 | + ORDER BY c.embedding <=> ${embeddingStr}::vector |
| 84 | + LIMIT ${limit} |
| 85 | + `; |
| 86 | + return rows.map((r: any) => ({ |
| 87 | + id: r.id, |
| 88 | + documentId: r.document_id, |
| 89 | + filename: r.filename, |
| 90 | + blockId: r.block_id, |
| 91 | + targetId: r.target_id, |
| 92 | + targetType: r.target_type, |
| 93 | + nodeType: r.node_type, |
| 94 | + content: r.content, |
| 95 | + contextType: r.context_type, |
| 96 | + metadata: r.metadata, |
| 97 | + })); |
| 98 | + }, |
| 99 | + |
| 100 | + async listDocuments(): Promise<DocumentRow[]> { |
| 101 | + const rows = |
| 102 | + await sql`SELECT id, filename, r2_key, file_hash, status, created_at FROM documents ORDER BY created_at DESC`; |
| 103 | + return rows.map((r: any) => ({ |
| 104 | + id: r.id, |
| 105 | + filename: r.filename, |
| 106 | + r2Key: r.r2_key, |
| 107 | + fileHash: r.file_hash, |
| 108 | + status: r.status, |
| 109 | + createdAt: r.created_at, |
| 110 | + })); |
| 111 | + }, |
| 112 | + |
| 113 | + async getDocument(id: number): Promise<DocumentRow | null> { |
| 114 | + const rows = |
| 115 | + await sql`SELECT id, filename, r2_key, file_hash, status, created_at FROM documents WHERE id = ${id} LIMIT 1`; |
| 116 | + if (rows.length === 0) return null; |
| 117 | + const r = rows[0]; |
| 118 | + return { |
| 119 | + id: r.id, |
| 120 | + filename: r.filename, |
| 121 | + r2Key: r.r2_key, |
| 122 | + fileHash: r.file_hash, |
| 123 | + status: r.status, |
| 124 | + createdAt: r.created_at, |
| 125 | + }; |
| 126 | + }, |
| 127 | + |
| 128 | + async chunkCount(): Promise<number> { |
| 129 | + const rows = await sql`SELECT COUNT(*) as count FROM chunks`; |
| 130 | + return Number(rows[0].count); |
| 131 | + }, |
| 132 | + |
| 133 | + async getChunkIdsByDocument(documentId: number): Promise<string[]> { |
| 134 | + const rows = |
| 135 | + await sql`SELECT id FROM chunks WHERE document_id = ${documentId}`; |
| 136 | + return rows.map((r: any) => r.id); |
| 137 | + }, |
| 138 | + |
| 139 | + async deleteDocument(id: number): Promise<void> { |
| 140 | + await sql`DELETE FROM documents WHERE id = ${id}`; |
| 141 | + }, |
| 142 | + |
| 143 | + async updateDocumentStatus(id: number, status: string): Promise<void> { |
| 144 | + await sql`UPDATE documents SET status = ${status} WHERE id = ${id}`; |
| 145 | + }, |
| 146 | + }; |
| 147 | +} |
0 commit comments