hnswsqlite
Version:
Vector search with HNSWlib and SQLite in TypeScript.
35 lines (34 loc) • 1.13 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SQLiteDB = void 0;
const better_sqlite3_1 = __importDefault(require("better-sqlite3"));
class SQLiteDB {
constructor(dbPath) {
this.db = new better_sqlite3_1.default(dbPath);
this._initTables();
}
_initTables() {
this.db.prepare(`CREATE TABLE IF NOT EXISTS documents (
id INTEGER PRIMARY KEY AUTOINCREMENT,
text TEXT,
embedding BLOB
)`).run();
}
addDocument(text, embedding) {
const result = this.db.prepare('INSERT INTO documents (text, embedding) VALUES (?, ?)').run(text, embedding);
return Number(result.lastInsertRowid);
}
getDocument(id) {
return this.db.prepare('SELECT * FROM documents WHERE id = ?').get(id);
}
getAllEmbeddings() {
return this.db.prepare('SELECT id, embedding FROM documents').all();
}
close() {
this.db.close();
}
}
exports.SQLiteDB = SQLiteDB;