First stable version
This commit is contained in:
@@ -8,7 +8,7 @@ import type {
|
||||
BUN_SQLITE_DatabaseSchemaType,
|
||||
} from "../types";
|
||||
|
||||
export default async function init(): Promise<BunSQLiteConfigReturn> {
|
||||
export default function init(): BunSQLiteConfigReturn {
|
||||
try {
|
||||
const { ROOT_DIR } = grabDirNames();
|
||||
const { ConfigFileName } = AppData;
|
||||
@@ -24,7 +24,7 @@ export default async function init(): Promise<BunSQLiteConfigReturn> {
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const ConfigImport = await import(ConfigFilePath);
|
||||
const ConfigImport = require(ConfigFilePath);
|
||||
const Config = ConfigImport["default"] as BunSQLiteConfig;
|
||||
|
||||
if (!Config.db_name) {
|
||||
@@ -48,7 +48,7 @@ export default async function init(): Promise<BunSQLiteConfigReturn> {
|
||||
}
|
||||
|
||||
const DBSchemaFilePath = path.join(db_dir, Config.db_schema_file_name);
|
||||
const DbSchemaImport = await import(DBSchemaFilePath);
|
||||
const DbSchemaImport = require(DBSchemaFilePath);
|
||||
const DbSchema = DbSchemaImport[
|
||||
"default"
|
||||
] as BUN_SQLITE_DatabaseSchemaType;
|
||||
|
||||
@@ -46,7 +46,7 @@ export default async function DbDelete<
|
||||
if (whereClause) {
|
||||
let sql = `DELETE FROM ${table} ${whereClause}`;
|
||||
|
||||
const res = DbClient.run(sql, sqlQueryObj.values);
|
||||
const res = DbClient.prepare(sql).run(...sqlQueryObj.values);
|
||||
|
||||
return {
|
||||
success: Boolean(res.changes),
|
||||
|
||||
@@ -26,7 +26,9 @@ export default async function DbInsert<
|
||||
data: finalData as any[],
|
||||
});
|
||||
|
||||
const res = DbClient.run(sqlObj?.query || "", sqlObj?.values || []);
|
||||
const res = DbClient.prepare(sqlObj?.query || "").run(
|
||||
...(sqlObj?.values || []),
|
||||
);
|
||||
|
||||
return {
|
||||
success: Boolean(Number(res.lastInsertRowid)),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env bun
|
||||
|
||||
import { Database } from "bun:sqlite";
|
||||
import { type Database } from "better-sqlite3";
|
||||
import _ from "lodash";
|
||||
import DbClient from ".";
|
||||
import type {
|
||||
@@ -25,14 +25,14 @@ class SQLiteSchemaManager {
|
||||
}) {
|
||||
this.db = DbClient;
|
||||
this.db_manager_table_name = "__db_schema_manager__";
|
||||
this.db.run("PRAGMA foreign_keys = ON;");
|
||||
this.db.exec("PRAGMA foreign_keys = ON;");
|
||||
this.recreate_vector_table = recreate_vector_table;
|
||||
this.createDbManagerTable();
|
||||
this.db_schema = schema;
|
||||
}
|
||||
|
||||
private createDbManagerTable() {
|
||||
this.db.run(`
|
||||
this.db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS ${this.db_manager_table_name} (
|
||||
table_name TEXT NOT NULL,
|
||||
created_at INTEGER NOT NULL,
|
||||
@@ -42,17 +42,19 @@ class SQLiteSchemaManager {
|
||||
}
|
||||
|
||||
private insertDbManagerTable(tableName: string) {
|
||||
this.db.run(
|
||||
`INSERT INTO ${this.db_manager_table_name} (table_name,created_at,updated_at) VALUES (?, ?, ?)`,
|
||||
[tableName, Date.now(), Date.now()],
|
||||
);
|
||||
this.db
|
||||
.prepare(
|
||||
`INSERT INTO ${this.db_manager_table_name} (table_name,created_at,updated_at) VALUES (?, ?, ?)`,
|
||||
)
|
||||
.run(...[tableName, Date.now(), Date.now()]);
|
||||
}
|
||||
|
||||
private removeDbManagerTable(tableName: string) {
|
||||
this.db.run(
|
||||
`DELETE FROM ${this.db_manager_table_name} WHERE table_name = ?`,
|
||||
[tableName],
|
||||
);
|
||||
this.db
|
||||
.prepare(
|
||||
`DELETE FROM ${this.db_manager_table_name} WHERE table_name = ?`,
|
||||
)
|
||||
.run(...[tableName]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,7 +83,7 @@ class SQLiteSchemaManager {
|
||||
private getExistingTables(): string[] {
|
||||
let sql = `SELECT table_name FROM ${this.db_manager_table_name}`;
|
||||
|
||||
const query = this.db.query(sql);
|
||||
const query = this.db.prepare(sql);
|
||||
const results = query.all() as { table_name: string }[];
|
||||
|
||||
return results.map((r) => r.table_name);
|
||||
@@ -102,8 +104,8 @@ class SQLiteSchemaManager {
|
||||
|
||||
for (const tableName of tablesToDrop) {
|
||||
console.log(`Dropping table: ${tableName}`);
|
||||
this.db.run(`DROP TABLE IF EXISTS "${tableName}"`);
|
||||
this.db.run(
|
||||
this.db.exec(`DROP TABLE IF EXISTS "${tableName}"`);
|
||||
this.db.exec(
|
||||
`DELETE FROM ${this.db_manager_table_name} WHERE table_name = "${tableName}"`,
|
||||
);
|
||||
}
|
||||
@@ -124,7 +126,7 @@ class SQLiteSchemaManager {
|
||||
console.log(
|
||||
`Renaming table: ${table.tableNameOld} -> ${table.tableName}`,
|
||||
);
|
||||
this.db.run(
|
||||
this.db.exec(
|
||||
`ALTER TABLE "${table.tableNameOld}" RENAME TO "${table.tableName}"`,
|
||||
);
|
||||
this.insertDbManagerTable(table.tableName);
|
||||
@@ -211,7 +213,7 @@ class SQLiteSchemaManager {
|
||||
? `CREATE VIRTUAL TABLE "${new_table.tableName}" USING ${new_table.vectorType || "vec0"}(${allConstraints.join(", ")})`
|
||||
: `CREATE TABLE "${new_table.tableName}" (${allConstraints.join(", ")})`;
|
||||
|
||||
this.db.run(sql);
|
||||
this.db.exec(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -266,7 +268,7 @@ class SQLiteSchemaManager {
|
||||
private getTableColumns(
|
||||
tableName: string,
|
||||
): { name: string; type: string }[] {
|
||||
const query = this.db.query(`PRAGMA table_info("${tableName}")`);
|
||||
const query = this.db.prepare(`PRAGMA table_info("${tableName}")`);
|
||||
const results = query.all() as { name: string; type: string }[];
|
||||
return results;
|
||||
}
|
||||
@@ -290,7 +292,7 @@ class SQLiteSchemaManager {
|
||||
|
||||
const sql = `ALTER TABLE "${tableName}" ADD COLUMN ${cleanDef}`;
|
||||
|
||||
this.db.run(sql);
|
||||
this.db.exec(sql);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -307,10 +309,10 @@ class SQLiteSchemaManager {
|
||||
console.log(`Recreating vector table: ${table.tableName}`);
|
||||
|
||||
const existingRows = this.db
|
||||
.query(`SELECT * FROM "${table.tableName}"`)
|
||||
.prepare(`SELECT * FROM "${table.tableName}"`)
|
||||
.all() as { [k: string]: any }[];
|
||||
|
||||
this.db.run(`DROP TABLE "${table.tableName}"`);
|
||||
this.db.exec(`DROP TABLE "${table.tableName}"`);
|
||||
await this.createTable(table);
|
||||
|
||||
if (existingRows.length > 0) {
|
||||
@@ -321,10 +323,11 @@ class SQLiteSchemaManager {
|
||||
const columns = Object.keys(row);
|
||||
const placeholders = columns.map(() => "?").join(", ");
|
||||
|
||||
this.db.run(
|
||||
`INSERT INTO "${table.tableName}" (${columns.join(", ")}) VALUES (${placeholders})`,
|
||||
Object.values(row),
|
||||
);
|
||||
this.db
|
||||
.prepare(
|
||||
`INSERT INTO "${table.tableName}" (${columns.join(", ")}) VALUES (${placeholders})`,
|
||||
)
|
||||
.run(...Object.values(row));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -354,16 +357,16 @@ class SQLiteSchemaManager {
|
||||
// Copy data if there are common columns
|
||||
if (columnsToKeep.length > 0) {
|
||||
const columnList = columnsToKeep.map((c) => `"${c}"`).join(", ");
|
||||
this.db.run(
|
||||
this.db.exec(
|
||||
`INSERT INTO "${tempTableName}" (${columnList}) SELECT ${columnList} FROM "${table.tableName}"`,
|
||||
);
|
||||
}
|
||||
|
||||
// Drop old table
|
||||
this.db.run(`DROP TABLE "${table.tableName}"`);
|
||||
this.db.exec(`DROP TABLE "${table.tableName}"`);
|
||||
|
||||
// Rename temp table
|
||||
this.db.run(
|
||||
this.db.exec(
|
||||
`ALTER TABLE "${tempTableName}" RENAME TO "${table.tableName}"`,
|
||||
);
|
||||
}
|
||||
@@ -506,7 +509,7 @@ class SQLiteSchemaManager {
|
||||
}
|
||||
|
||||
// Get existing indexes
|
||||
const query = this.db.query(
|
||||
const query = this.db.prepare(
|
||||
`SELECT name FROM sqlite_master WHERE type='index' AND tbl_name='${table.tableName}' AND name NOT LIKE 'sqlite_%'`,
|
||||
);
|
||||
const existingIndexes = (query.all() as { name: string }[]).map(
|
||||
@@ -520,7 +523,7 @@ class SQLiteSchemaManager {
|
||||
);
|
||||
if (!stillExists) {
|
||||
console.log(`Dropping index: ${indexName}`);
|
||||
this.db.run(`DROP INDEX IF EXISTS "${indexName}"`);
|
||||
this.db.exec(`DROP INDEX IF EXISTS "${indexName}"`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -540,7 +543,7 @@ class SQLiteSchemaManager {
|
||||
.map((f) => `"${f.value}"`)
|
||||
.join(", ");
|
||||
const unique = index.indexType === "regular" ? "" : ""; // SQLite doesn't have FULLTEXT in CREATE INDEX
|
||||
this.db.run(
|
||||
this.db.exec(
|
||||
`CREATE ${unique}INDEX "${index.indexName}" ON "${table.tableName}" (${fields})`,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -47,8 +47,7 @@ export default async function DbSelect<
|
||||
|
||||
const sql = mysql.format(sqlObj.string, sqlObj.values);
|
||||
|
||||
const res = DbClient.query<Schema, Schema[]>(sql);
|
||||
const batchRes = res.all();
|
||||
const batchRes = DbClient.prepare(sql).all() as Schema[];
|
||||
|
||||
let resp: APIResponseObject<Schema> = {
|
||||
success: Boolean(batchRes[0]),
|
||||
|
||||
@@ -12,8 +12,8 @@ export default async function DbSQL<
|
||||
>({ sql, values }: Params): Promise<APIResponseObject<T>> {
|
||||
try {
|
||||
const res = sql.match(/^select/i)
|
||||
? DbClient.query(sql).all(...(values || []))
|
||||
: DbClient.run(sql, values || []);
|
||||
? DbClient.prepare(sql).all(...(values || []))
|
||||
: DbClient.prepare(sql).run(...(values || []));
|
||||
|
||||
return {
|
||||
success: true,
|
||||
|
||||
@@ -76,7 +76,7 @@ export default async function DbUpdate<
|
||||
sql += ` ${whereClause}`;
|
||||
values = [...values, ...sqlQueryObj.values];
|
||||
|
||||
const res = DbClient.run(sql, values);
|
||||
const res = DbClient.prepare(sql).run(...values);
|
||||
|
||||
return {
|
||||
success: Boolean(res.changes),
|
||||
|
||||
@@ -5,7 +5,7 @@ import init from "../../functions/init";
|
||||
import grabDBDir from "../../utils/grab-db-dir";
|
||||
|
||||
const { ROOT_DIR } = grabDirNames();
|
||||
const { config } = await init();
|
||||
const { config } = init();
|
||||
|
||||
let db_dir = ROOT_DIR;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user