Spaces:
Sleeping

File size: 1,424 Bytes
7f81110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { eq, and } from "drizzle-orm";
import { embeddings, Embedding, NewEmbedding } from "../schema/embeddings";
import { IPgEmbeddingRepository } from "./IPgEmbeddingRepository";

export class PgEmbeddingRepository implements IPgEmbeddingRepository {
    constructor(private readonly db: any) { }

    async upsert(input: NewEmbedding): Promise<void> {
        await this.db
            .insert(embeddings)
            .values(input)
            .onConflictDoUpdate({
                target: [embeddings.storeId, embeddings.documentId, embeddings.chunkId],
                set: {
                    embedding: input.embedding,
                    text: input.text,
                    model: input.model,
                    metadata: input.metadata,
                },
            });
    }

    async findByDocAndChunk(storeId: string, documentId: string, chunkId: string): Promise<Embedding | null> {
        const [result] = await this.db
            .select()
            .from(embeddings)
            .where(and(
                eq(embeddings.storeId, storeId),
                eq(embeddings.documentId, documentId),
                eq(embeddings.chunkId, chunkId)
            ))
            .limit(1);
        return result || null;
    }

    async deleteByStore(storeId: string): Promise<void> {
        await this.db
            .delete(embeddings)
            .where(eq(embeddings.storeId, storeId));
    }
}