| 17 | } |
| 18 | |
| 19 | export class SQLiteVectorStore implements VectorStore { |
| 20 | private table: string; |
| 21 | |
| 22 | constructor(private db: DbOps, tableName: string = "vectors") { |
| 23 | this.table = tableName; |
| 24 | } |
| 25 | |
| 26 | async storeVector(id: string, sector: string, vector: number[], dim: number, user_id?: string): Promise<void> { |
| 27 | const v = vectorToBuffer(vector); |
| 28 | // SQLite: insert or replace. |
| 29 | // Logic mirrors backend PostgresVectorStore but explicitly compatible with SQLite syntax |
| 30 | const sql = `insert or replace into ${this.table}(id,sector,user_id,v,dim) values(?,?,?,?,?)`; |
| 31 | await this.db.run_async(sql, [id, sector, user_id || "anonymous", v, dim]); |
| 32 | } |
| 33 | |
| 34 | async deleteVector(id: string, sector: string): Promise<void> { |
| 35 | await this.db.run_async(`delete from ${this.table} where id=? and sector=?`, [id, sector]); |
| 36 | } |
| 37 | |
| 38 | async deleteVectors(id: string): Promise<void> { |
| 39 | await this.db.run_async(`delete from ${this.table} where id=?`, [id]); |
| 40 | } |
| 41 | |
| 42 | async searchSimilar(sector: string, queryVec: number[], topK: number): Promise<Array<{ id: string; score: number }>> { |
| 43 | // In-memory cosine similarity for SQLite (since no native vector extension assumed) |
| 44 | const rows = await this.db.all_async(`select id,v,dim from ${this.table} where sector=?`, [sector]); |
| 45 | const sims: Array<{ id: string; score: number }> = []; |
| 46 | for (const row of rows) { |
| 47 | const vec = bufferToVector(row.v); |
| 48 | const sim = cosineSimilarity(queryVec, vec); |
| 49 | sims.push({ id: row.id, score: sim }); |
| 50 | } |
| 51 | sims.sort((a, b) => b.score - a.score); |
| 52 | return sims.slice(0, topK); |
| 53 | } |
| 54 | |
| 55 | async getVector(id: string, sector: string): Promise<{ vector: number[]; dim: number } | null> { |
| 56 | const row = await this.db.get_async(`select v,dim from ${this.table} where id=? and sector=?`, [id, sector]); |
| 57 | if (!row) return null; |
| 58 | return { vector: bufferToVector(row.v), dim: row.dim }; |
| 59 | } |
| 60 | |
| 61 | async getVectorsById(id: string): Promise<Array<{ sector: string; vector: number[]; dim: number }>> { |
| 62 | const rows = await this.db.all_async(`select sector,v,dim from ${this.table} where id=?`, [id]); |
| 63 | return rows.map(row => ({ sector: row.sector, vector: bufferToVector(row.v), dim: row.dim })); |
| 64 | } |
| 65 | |
| 66 | async getVectorsBySector(sector: string): Promise<Array<{ id: string; vector: number[]; dim: number }>> { |
| 67 | const rows = await this.db.all_async(`select id,v,dim from ${this.table} where sector=?`, [sector]); |
| 68 | return rows.map(row => ({ id: row.id, vector: bufferToVector(row.v), dim: row.dim })); |
| 69 | } |
| 70 | } |
nothing calls this directly
no outgoing calls
no test coverage detected