Refactor DB (#960)

* Refactor table creation and SQLite connection initialization. Previously, attempts to create the 'docs' table were made on every interaction with it. Now, table creation is attempted only upon the first interaction. Additionally, the SQLite connection is now initialized just once, enhancing performance.

* no longer need createDocsTable

* library no longer used
This commit is contained in:
Jason Jacobs 2024-03-14 13:24:12 -07:00 • committed by GitHub
parent caa7aa9459
commit 9c290e9db8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,7 +2,6 @@ import { Database, open } from "sqlite";
import sqlite3 from "sqlite3";
import { Chunk } from "../..";
import { getDocsSqlitePath, getLanceDbPath } from "../../util/paths";
import { SqliteDb } from "../refreshIndex";
import { downloadPreIndexedDocs } from "./preIndexed";
import { default as configs } from "./preIndexedDocs";
@ -21,12 +20,23 @@ interface LanceDbDocsRow {
[key: string]: any;
}
async function createDocsTable(db: Database<sqlite3.Database>) {
db.exec(`CREATE TABLE IF NOT EXISTS docs (
let dbDocs:Database;
async function getDBDocs() {
if (!dbDocs) {
dbDocs = await open({
filename: getDocsSqlitePath(),
driver: sqlite3.Database,
});
dbDocs.exec(`CREATE TABLE IF NOT EXISTS docs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title STRING NOT NULL,
baseUrl STRING NOT NULL UNIQUE
)`);
}
return dbDocs;
}
export async function retrieveDocs(
@ -37,8 +47,7 @@ export async function retrieveDocs(
nested: boolean = false,
): Promise<Chunk[]> {
const lancedb = await import("vectordb");
const db = await SqliteDb.get();
await createDocsTable(db);
const db = await getDBDocs();
const lance = await lancedb.connect(getLanceDbPath());
const downloadDocs = async () => {
@ -116,11 +125,7 @@ export async function addDocs(
}
// Only after add it to SQLite
const db = await open({
filename: getDocsSqlitePath(),
driver: sqlite3.Database,
});
await createDocsTable(db);
const db = await getDBDocs();
await db.run(
`INSERT INTO docs (title, baseUrl) VALUES (?, ?)`,
title,
@ -131,11 +136,7 @@ export async function addDocs(
export async function listDocs(): Promise<
{ title: string; baseUrl: string }[]
> {
const db = await open({
filename: getDocsSqlitePath(),
driver: sqlite3.Database,
});
await createDocsTable(db);
const docs = await db.all(`SELECT title, baseUrl FROM docs`);
const db = await getDBDocs();
const docs = db.all(`SELECT title, baseUrl FROM docs`);
return docs;
}