Major version update. Major bugfixes. Security vulnerabilites fixed
This commit is contained in:
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
import type { APIResponseObject, BunSQLiteConfig, SQLInsertGenValueType } from "../types";
|
||||
type Param = {
|
||||
query: string;
|
||||
values?: SQLInsertGenValueType[];
|
||||
config?: BunSQLiteConfig;
|
||||
};
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
* @description Any statement that produces columns (SELECT, WITH, PRAGMA,
|
||||
* RETURNING, ...) returns rows. Everything else goes through `run`, which
|
||||
* also handles multiple statements.
|
||||
*/
|
||||
export default function dbHandler<T extends {
|
||||
[k: string]: any;
|
||||
} = {
|
||||
[k: string]: any;
|
||||
}>({ query, values, config }: Param): APIResponseObject<T>;
|
||||
export {};
|
||||
Vendored
+53
@@ -0,0 +1,53 @@
|
||||
import init from "../functions/init";
|
||||
import grabSQLiteClient from "../functions/grab-sqlite-client";
|
||||
/**
|
||||
* Resolve the SQLite client. The global client is used unless a `config`
|
||||
* for another database is passed.
|
||||
*/
|
||||
function resolveClient(config) {
|
||||
if (config) {
|
||||
return grabSQLiteClient({ config });
|
||||
}
|
||||
init();
|
||||
return global.SQLITE_CLIENT;
|
||||
}
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
* @description Any statement that produces columns (SELECT, WITH, PRAGMA,
|
||||
* RETURNING, ...) returns rows. Everything else goes through `run`, which
|
||||
* also handles multiple statements.
|
||||
*/
|
||||
export default function dbHandler({ query, values, config }) {
|
||||
try {
|
||||
const CLIENT = resolveClient(config);
|
||||
const stmt = CLIENT.prepare(query);
|
||||
const returnsRows = stmt.columnNames.length > 0;
|
||||
try {
|
||||
if (returnsRows) {
|
||||
const rows = stmt.all(...(values || []));
|
||||
return {
|
||||
success: true,
|
||||
payload: rows,
|
||||
singleRes: rows[0],
|
||||
};
|
||||
}
|
||||
}
|
||||
finally {
|
||||
stmt.finalize();
|
||||
}
|
||||
const res = CLIENT.run(query, (values || []));
|
||||
return {
|
||||
success: true,
|
||||
postInsertReturn: {
|
||||
affectedRows: res.changes,
|
||||
insertId: Number(res.lastInsertRowid),
|
||||
},
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
+17
-4
@@ -1,22 +1,35 @@
|
||||
import init from "../functions/init";
|
||||
import { sqlIdent, sqlQuoteIdent } from "../utils/sql-safe";
|
||||
export default async function ({ sql: passed_sql, table, data }) {
|
||||
let sql = passed_sql;
|
||||
const { dbSchema } = await init();
|
||||
const { dbSchema } = init();
|
||||
const table_schema = dbSchema.tables.find((t) => t.tableName == table);
|
||||
const now = Date.now();
|
||||
if (table_schema?.tableName) {
|
||||
const set_sql_arr = Object.keys(Array.isArray(data) ? data[0] : data).map((field) => `${field} = excluded.${field}`);
|
||||
const set_sql_arr = Object.keys(Array.isArray(data) ? data[0] : data)
|
||||
.filter((field) => field !== "updated_at")
|
||||
.map((field) => {
|
||||
const column = sqlIdent(field, "column name");
|
||||
return `${column} = excluded.${column}`;
|
||||
});
|
||||
set_sql_arr.push(`updated_at = ${now}`);
|
||||
const set_sql = set_sql_arr.join(", ");
|
||||
const unique_fields = table_schema.fields.filter((f) => f.unique);
|
||||
for (let i = 0; i < unique_fields.length; i++) {
|
||||
const field = unique_fields[i];
|
||||
sql += ` ON CONFLICT(${field?.fieldName}) DO UPDATE SET ${set_sql}`;
|
||||
if (!field?.fieldName)
|
||||
continue;
|
||||
sql += ` ON CONFLICT(${sqlQuoteIdent(field.fieldName)}) DO UPDATE SET ${set_sql}`;
|
||||
}
|
||||
if (table_schema.uniqueConstraints?.[0]) {
|
||||
for (let i = 0; i < table_schema.uniqueConstraints.length; i++) {
|
||||
const constraint = table_schema.uniqueConstraints[i];
|
||||
sql += ` ON CONFLICT(${constraint?.constraintTableFields?.map((c) => c.value)?.join(", ")}) DO UPDATE SET ${set_sql}`;
|
||||
const columns = constraint?.constraintTableFields
|
||||
?.map((c) => sqlQuoteIdent(String(c.value)))
|
||||
?.join(", ");
|
||||
if (!columns)
|
||||
continue;
|
||||
sql += ` ON CONFLICT(${columns}) DO UPDATE SET ${set_sql}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
import type { BUN_SQLITE_FieldSchemaType } from "../../types";
|
||||
/**
|
||||
* Build column definition SQL
|
||||
*/
|
||||
export default function buildColumnDefinition(field: BUN_SQLITE_FieldSchemaType, is_vector?: boolean): string;
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
import mapDataType from "./map-data-types";
|
||||
/**
|
||||
* Build column definition SQL
|
||||
*/
|
||||
export default function buildColumnDefinition(field, is_vector) {
|
||||
if (!field.fieldName) {
|
||||
throw new Error("Field name is required");
|
||||
}
|
||||
// vec0 can't parse quoted column names
|
||||
const fieldName = is_vector
|
||||
? field.sideCar
|
||||
? `+${field.fieldName}`
|
||||
: `${field.fieldName}`
|
||||
: q(field.fieldName);
|
||||
const parts = [fieldName];
|
||||
// Data type mapping
|
||||
parts.push(mapDataType(field));
|
||||
// Primary key
|
||||
if (field.primaryKey) {
|
||||
parts.push("PRIMARY KEY");
|
||||
if (field.autoIncrement) {
|
||||
parts.push("AUTOINCREMENT");
|
||||
}
|
||||
}
|
||||
// Not null
|
||||
if (field.notNullValue && !field.primaryKey) {
|
||||
parts.push("NOT NULL");
|
||||
}
|
||||
// Unique
|
||||
if (field.unique && !field.primaryKey) {
|
||||
parts.push("UNIQUE");
|
||||
}
|
||||
if (field.vectorDistanceMetric) {
|
||||
parts.push(`distance_metric=${field.vectorDistanceMetric}`);
|
||||
}
|
||||
// Default value
|
||||
if (field.defaultValue !== undefined) {
|
||||
if (typeof field.defaultValue === "string") {
|
||||
parts.push(
|
||||
// Escape single quotes by doubling them to prevent SQL injection and wrap in single quotes
|
||||
`DEFAULT '${field.defaultValue.replace(/'/g, "''")}'`);
|
||||
}
|
||||
else {
|
||||
parts.push(`DEFAULT ${field.defaultValue}`);
|
||||
}
|
||||
}
|
||||
else if (field.defaultValueLiteral) {
|
||||
parts.push(`DEFAULT ${field.defaultValueLiteral}`);
|
||||
}
|
||||
return parts.join(" ");
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import type { BUN_SQLITE_DatabaseSchemaType, BUN_SQLITE_TableSchemaType } from "../../types";
|
||||
type Params = {
|
||||
db_schema: BUN_SQLITE_DatabaseSchemaType;
|
||||
table: BUN_SQLITE_TableSchemaType;
|
||||
};
|
||||
/**
|
||||
* Build the `CREATE TABLE` (or `CREATE VIRTUAL TABLE`) statement for a table
|
||||
*/
|
||||
export default function buildCreateTableSql({ db_schema, table }: Params): string;
|
||||
export {};
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
import buildColumnDefinition from "./build-column-definition";
|
||||
import buildForeignKeyConstraint from "./build-foreign-key-constraint";
|
||||
import resolveTable from "./resolve-table";
|
||||
/**
|
||||
* Build the `CREATE TABLE` (or `CREATE VIRTUAL TABLE`) statement for a table
|
||||
*/
|
||||
export default function buildCreateTableSql({ db_schema, table }) {
|
||||
const new_table = resolveTable({ table, db_schema });
|
||||
const columns = [];
|
||||
const foreignKeys = [];
|
||||
for (const field of new_table.fields) {
|
||||
columns.push(buildColumnDefinition(field, new_table.isVector));
|
||||
if (field.foreignKey && !new_table.isVector) {
|
||||
foreignKeys.push(buildForeignKeyConstraint(field));
|
||||
}
|
||||
}
|
||||
// Add unique constraints
|
||||
for (const constraint of new_table.uniqueConstraints || []) {
|
||||
if (!constraint.constraintTableFields?.length)
|
||||
continue;
|
||||
const fieldNames = constraint.constraintTableFields.map((f) => String(f.value));
|
||||
const constraintName = constraint.constraintName || `unique_${fieldNames.join(", ")}`;
|
||||
columns.push(`CONSTRAINT ${q(constraintName)} UNIQUE (${fieldNames
|
||||
.map(q)
|
||||
.join(", ")})`);
|
||||
}
|
||||
const allConstraints = [...columns, ...foreignKeys];
|
||||
return new_table.isVector
|
||||
? `CREATE VIRTUAL TABLE ${q(new_table.tableName)} USING ${new_table.vectorType || "vec0"}(${allConstraints.join(", ")})`
|
||||
: `CREATE TABLE ${q(new_table.tableName)} (${allConstraints.join(", ")})`;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import type { BUN_SQLITE_FieldSchemaType } from "../../types";
|
||||
/**
|
||||
* Build foreign key constraint
|
||||
*/
|
||||
export default function buildForeignKeyConstraint(field: BUN_SQLITE_FieldSchemaType): string;
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
/**
|
||||
* Build foreign key constraint
|
||||
*/
|
||||
export default function buildForeignKeyConstraint(field) {
|
||||
const fk = field.foreignKey;
|
||||
let constraint = `FOREIGN KEY (${q(field.fieldName)}) REFERENCES ${q(String(fk.destinationTableName))}(${q(String(fk.destinationTableColumnName))})`;
|
||||
if (fk.cascadeDelete) {
|
||||
constraint += " ON DELETE CASCADE";
|
||||
}
|
||||
if (fk.cascadeUpdate) {
|
||||
constraint += " ON UPDATE CASCADE";
|
||||
}
|
||||
return constraint;
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
import type { Database } from "bun:sqlite";
|
||||
type Params = {
|
||||
db: Database;
|
||||
table_name: string;
|
||||
};
|
||||
export default function checkIfTableExists({ db, table_name }: Params): boolean;
|
||||
export {};
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
export default function checkIfTableExists({ db, table_name }) {
|
||||
const tableExists = db
|
||||
.query(`SELECT name FROM sqlite_master WHERE type='table' AND name=?`)
|
||||
.get(table_name);
|
||||
return Boolean(tableExists?.name);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import type { Database } from "bun:sqlite";
|
||||
export default function createDBManagerTable({ db }: {
|
||||
db: Database;
|
||||
}): void;
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import { AppData } from "../../data/app-data";
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
export default function createDBManagerTable({ db }) {
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS ${q(AppData["DbSchemaManagerTableName"])} (
|
||||
table_name TEXT NOT NULL,
|
||||
created_at INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL
|
||||
)
|
||||
`);
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import type { CreateDBSchemaParams } from "../../types";
|
||||
/**
|
||||
* # Sync the database to the schema
|
||||
* @description Changed tables are rebuilt by copying into a new table and
|
||||
* dropping the old one. With foreign keys ON, `DROP TABLE` runs an implicit
|
||||
* `DELETE FROM` that fires `ON DELETE CASCADE` on child tables, so keys are
|
||||
* disabled for the sync and checked before committing. (`PRAGMA
|
||||
* foreign_keys` is a no-op inside a transaction.) With `dry_run` the
|
||||
* transaction is rolled back, so `changes` is the exact plan.
|
||||
*/
|
||||
export default function createDBSchema(params: CreateDBSchemaParams): void;
|
||||
Vendored
+66
@@ -0,0 +1,66 @@
|
||||
import _ from "lodash";
|
||||
import createDBManagerTable from "./create-db-manager-table";
|
||||
import dropRemovedTables from "./drop-removed-tables";
|
||||
import getExistingTablesFromTablesManagerTable from "./get-existing-tables-from-tables-manager-table";
|
||||
import handleDBSchemaTable from "./handle-db-schema-table";
|
||||
import orderDBSchema from "./order-db-schema";
|
||||
/**
|
||||
* # Sync the database to the schema
|
||||
* @description Changed tables are rebuilt by copying into a new table and
|
||||
* dropping the old one. With foreign keys ON, `DROP TABLE` runs an implicit
|
||||
* `DELETE FROM` that fires `ON DELETE CASCADE` on child tables, so keys are
|
||||
* disabled for the sync and checked before committing. (`PRAGMA
|
||||
* foreign_keys` is a no-op inside a transaction.) With `dry_run` the
|
||||
* transaction is rolled back, so `changes` is the exact plan.
|
||||
*/
|
||||
export default function createDBSchema(params) {
|
||||
const { db, dry_run } = params;
|
||||
/**
|
||||
* Reorder Tables (parents before children with FKs)
|
||||
*/
|
||||
const db_schema = orderDBSchema(params);
|
||||
db.run("PRAGMA foreign_keys = OFF;");
|
||||
db.run("BEGIN");
|
||||
try {
|
||||
/**
|
||||
* Create Schema Manager Table
|
||||
*/
|
||||
createDBManagerTable({ db });
|
||||
const existing_tables = getExistingTablesFromTablesManagerTable({
|
||||
db,
|
||||
});
|
||||
/**
|
||||
* Create or update tables
|
||||
*/
|
||||
for (const table of db_schema.tables) {
|
||||
handleDBSchemaTable({
|
||||
...params,
|
||||
db_schema,
|
||||
table,
|
||||
existing_tables,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Drop tables that no longer exist in schema
|
||||
*/
|
||||
dropRemovedTables({
|
||||
...params,
|
||||
existing_tables,
|
||||
schema_tables: db_schema.tables.map((t) => t.tableName),
|
||||
});
|
||||
const fkViolations = db.query(`PRAGMA foreign_key_check`).all();
|
||||
if (fkViolations.length) {
|
||||
const tables = _.uniq(fkViolations.map((v) => `${v.table} -> ${v.parent}`));
|
||||
throw new Error(`Foreign key violations after schema sync: ${tables.join(", ")}`);
|
||||
}
|
||||
db.run(dry_run ? "ROLLBACK" : "COMMIT");
|
||||
}
|
||||
catch (error) {
|
||||
if (db.inTransaction)
|
||||
db.run("ROLLBACK");
|
||||
throw error;
|
||||
}
|
||||
finally {
|
||||
db.run("PRAGMA foreign_keys = ON;");
|
||||
}
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
import type { BUN_SQLITE_TableSchemaType, CreateDBSchemaParams } from "../../types";
|
||||
type Params = CreateDBSchemaParams & {
|
||||
table: BUN_SQLITE_TableSchemaType;
|
||||
};
|
||||
/**
|
||||
* Create a new table
|
||||
*/
|
||||
export default function createTable(params: Params): void;
|
||||
export {};
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
import buildCreateTableSql from "./build-create-table-sql";
|
||||
import recordSchemaChange from "./record-schema-change";
|
||||
/**
|
||||
* Create a new table
|
||||
*/
|
||||
export default function createTable(params) {
|
||||
const { db, db_schema, table } = params;
|
||||
recordSchemaChange(params, {
|
||||
description: `Create table "${table.tableName}"`,
|
||||
});
|
||||
db.run(buildCreateTableSql({ db_schema, table }));
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import type { CreateDBSchemaParams } from "../../types";
|
||||
type Params = CreateDBSchemaParams & {
|
||||
existing_tables: string[];
|
||||
schema_tables: string[];
|
||||
};
|
||||
/**
|
||||
* Drop tables that are no longer in the schema
|
||||
*/
|
||||
export default function dropRemovedTables(params: Params): void;
|
||||
export {};
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
import _ from "lodash";
|
||||
import { readLiveSchema } from "../../functions/live-schema";
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
import removeDBManagerTable from "./remove-db-manager-table";
|
||||
import recordSchemaChange from "./record-schema-change";
|
||||
import checkIfTableExists from "./check-if-table-exists";
|
||||
/**
|
||||
* Drop tables that are no longer in the schema
|
||||
*/
|
||||
export default function dropRemovedTables(params) {
|
||||
const { db, existing_tables, schema_tables } = params;
|
||||
const tablesToDrop = existing_tables.filter((t) => !schema_tables.includes(t) &&
|
||||
!schema_tables.find((scT) => t.startsWith(scT + "_")));
|
||||
const currentSchema = readLiveSchema();
|
||||
for (const table of currentSchema?.tables || []) {
|
||||
if (!table?.tableName)
|
||||
continue;
|
||||
if (!schema_tables.includes(table.tableName)) {
|
||||
tablesToDrop.push(table.tableName);
|
||||
}
|
||||
}
|
||||
for (const tableName of _.uniq(tablesToDrop)) {
|
||||
if (!checkIfTableExists({ db, table_name: tableName })) {
|
||||
removeDBManagerTable({ db, table_name: tableName });
|
||||
continue;
|
||||
}
|
||||
recordSchemaChange(params, {
|
||||
description: `Drop table "${tableName}"`,
|
||||
destructive: true,
|
||||
});
|
||||
db.run(`DROP TABLE IF EXISTS ${q(tableName)}`);
|
||||
removeDBManagerTable({ db, table_name: tableName });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import type { Database } from "bun:sqlite";
|
||||
export default function getExistingTablesFromTablesManagerTable({ db, }: {
|
||||
db: Database;
|
||||
}): string[];
|
||||
@@ -0,0 +1,8 @@
|
||||
import { AppData } from "../../data/app-data";
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
export default function getExistingTablesFromTablesManagerTable({ db, }) {
|
||||
const results = db
|
||||
.query(`SELECT table_name FROM ${q(AppData["DbSchemaManagerTableName"])}`)
|
||||
.all();
|
||||
return results.map((r) => r.table_name);
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import type { Database } from "bun:sqlite";
|
||||
type Params = {
|
||||
db: Database;
|
||||
table_name: string;
|
||||
};
|
||||
export default function getTableColumns({ db, table_name, }: Params): {
|
||||
name: string;
|
||||
type: string;
|
||||
}[];
|
||||
export {};
|
||||
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
export default function getTableColumns({ db, table_name, }) {
|
||||
return db.query(`PRAGMA table_info(${q(table_name)})`).all();
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import type { BUN_SQLITE_TableSchemaType, CreateDBSchemaParams } from "../../types";
|
||||
type Params = CreateDBSchemaParams & {
|
||||
table: BUN_SQLITE_TableSchemaType;
|
||||
existing_tables: string[];
|
||||
};
|
||||
/**
|
||||
* Sync a single table (rename, create or update, then indexes)
|
||||
*/
|
||||
export default function handleDBSchemaTable(params: Params): void;
|
||||
export {};
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
import createTable from "./create-table";
|
||||
import insertDBManagerTable from "./insert-db-manager-table";
|
||||
import recordSchemaChange from "./record-schema-change";
|
||||
import removeDBManagerTable from "./remove-db-manager-table";
|
||||
import syncIndexes from "./sync-indexes";
|
||||
import updateTable from "./update-table";
|
||||
/**
|
||||
* Sync a single table (rename, create or update, then indexes)
|
||||
*/
|
||||
export default function handleDBSchemaTable(params) {
|
||||
const { db, table, existing_tables } = params;
|
||||
let tableExists = existing_tables.includes(table.tableName);
|
||||
// Handle table rename
|
||||
if (table.tableNameOld &&
|
||||
table.tableNameOld !== table.tableName &&
|
||||
existing_tables.includes(table.tableNameOld)) {
|
||||
recordSchemaChange(params, {
|
||||
description: `Rename table "${table.tableNameOld}" -> "${table.tableName}"`,
|
||||
});
|
||||
db.run(`ALTER TABLE ${q(table.tableNameOld)} RENAME TO ${q(table.tableName)}`);
|
||||
insertDBManagerTable({ db, table_name: table.tableName });
|
||||
removeDBManagerTable({ db, table_name: table.tableNameOld });
|
||||
tableExists = true;
|
||||
}
|
||||
if (!tableExists) {
|
||||
createTable(params);
|
||||
insertDBManagerTable({ db, table_name: table.tableName });
|
||||
}
|
||||
else {
|
||||
updateTable(params);
|
||||
}
|
||||
syncIndexes(params);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import type { Database } from "bun:sqlite";
|
||||
type Params = {
|
||||
db: Database;
|
||||
table_name: string;
|
||||
};
|
||||
export default function insertDBManagerTable({ db, table_name }: Params): void;
|
||||
export {};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import { AppData } from "../../data/app-data";
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
export default function insertDBManagerTable({ db, table_name }) {
|
||||
db.run(`INSERT INTO ${q(AppData["DbSchemaManagerTableName"])} (table_name,created_at,updated_at) VALUES (?, ?, ?)`, [table_name, Date.now(), Date.now()]);
|
||||
}
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
import type { BUN_SQLITE_DATATYPES, BUN_SQLITE_FieldSchemaType } from "../../types";
|
||||
/**
|
||||
* Map DSQL data types to SQLite types
|
||||
*/
|
||||
export default function mapDataType(field: BUN_SQLITE_FieldSchemaType): (typeof BUN_SQLITE_DATATYPES)[number]["value"];
|
||||
Vendored
+40
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Map DSQL data types to SQLite types
|
||||
*/
|
||||
export default function mapDataType(field) {
|
||||
const dataType = field.dataType?.toLowerCase() || "text";
|
||||
const vectorSize = field.vectorSize || 1536;
|
||||
// Vector Embeddings
|
||||
if (field.isVector) {
|
||||
return `FLOAT[${vectorSize}]`;
|
||||
}
|
||||
// Integer types
|
||||
if (dataType.includes("int") ||
|
||||
dataType === "bigint" ||
|
||||
dataType === "smallint" ||
|
||||
dataType === "tinyint") {
|
||||
return "INTEGER";
|
||||
}
|
||||
// Real/Float types
|
||||
if (dataType.includes("real") ||
|
||||
dataType.includes("float") ||
|
||||
dataType.includes("double") ||
|
||||
dataType === "decimal" ||
|
||||
dataType === "numeric") {
|
||||
return "REAL";
|
||||
}
|
||||
// Blob types
|
||||
if (dataType.includes("blob") || dataType.includes("binary")) {
|
||||
return "BLOB";
|
||||
}
|
||||
// Boolean
|
||||
if (dataType === "boolean" || dataType === "bool") {
|
||||
return "INTEGER"; // SQLite uses INTEGER for boolean (0/1)
|
||||
}
|
||||
// Date/Time types
|
||||
if (dataType.includes("date") || dataType.includes("time")) {
|
||||
return "TEXT"; // SQLite stores dates as TEXT or INTEGER
|
||||
}
|
||||
// Default to TEXT for all text-based types
|
||||
return "TEXT";
|
||||
}
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
import type { BUN_SQLITE_DatabaseSchemaType, CreateDBSchemaParams } from "../../types";
|
||||
/**
|
||||
* Topologically order tables so referenced (parent) tables come before the
|
||||
* tables that reference them. Declaration order is kept wherever the FK
|
||||
* graph allows it.
|
||||
*
|
||||
* - Self references (e.g. `comments.parent_comment_id`) are ignored.
|
||||
* - References to tables outside the schema are ignored.
|
||||
* - Cycles do not throw: a table already on the current path is skipped,
|
||||
* so every table is still emitted exactly once. Foreign keys are not
|
||||
* enforced during the sync, so cycles are safe.
|
||||
*/
|
||||
export default function orderDBSchema({ db_schema, }: Pick<CreateDBSchemaParams, "db_schema">): BUN_SQLITE_DatabaseSchemaType;
|
||||
Vendored
+49
@@ -0,0 +1,49 @@
|
||||
import _ from "lodash";
|
||||
/**
|
||||
* Topologically order tables so referenced (parent) tables come before the
|
||||
* tables that reference them. Declaration order is kept wherever the FK
|
||||
* graph allows it.
|
||||
*
|
||||
* - Self references (e.g. `comments.parent_comment_id`) are ignored.
|
||||
* - References to tables outside the schema are ignored.
|
||||
* - Cycles do not throw: a table already on the current path is skipped,
|
||||
* so every table is still emitted exactly once. Foreign keys are not
|
||||
* enforced during the sync, so cycles are safe.
|
||||
*/
|
||||
export default function orderDBSchema({ db_schema, }) {
|
||||
const new_db_schema = _.cloneDeep(db_schema);
|
||||
const tables = new_db_schema.tables;
|
||||
const tables_by_name = new Map();
|
||||
for (const table of tables) {
|
||||
if (table)
|
||||
tables_by_name.set(table.tableName, table);
|
||||
}
|
||||
const ordered = [];
|
||||
const visited = new Set();
|
||||
const visiting = new Set();
|
||||
function visit(table) {
|
||||
if (visited.has(table.tableName) || visiting.has(table.tableName)) {
|
||||
return;
|
||||
}
|
||||
visiting.add(table.tableName);
|
||||
for (const field of table.fields || []) {
|
||||
const dst_table_name = field?.foreignKey?.destinationTableName;
|
||||
if (!dst_table_name || dst_table_name === table.tableName) {
|
||||
continue;
|
||||
}
|
||||
const fk_table = tables_by_name.get(dst_table_name);
|
||||
if (fk_table) {
|
||||
visit(fk_table);
|
||||
}
|
||||
}
|
||||
visiting.delete(table.tableName);
|
||||
visited.add(table.tableName);
|
||||
ordered.push(table);
|
||||
}
|
||||
for (const table of tables) {
|
||||
if (table)
|
||||
visit(table);
|
||||
}
|
||||
new_db_schema.tables = ordered;
|
||||
return new_db_schema;
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
import type { CreateDBSchemaParams, SchemaChange } from "../../types";
|
||||
/**
|
||||
* # Record a schema change before it runs
|
||||
* @description Logs the change (unless this is a dry run) and refuses
|
||||
* destructive changes that were not approved.
|
||||
*/
|
||||
export default function recordSchemaChange(params: Pick<CreateDBSchemaParams, "dry_run" | "changes" | "allow_destructive">, change: SchemaChange): void;
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* # Record a schema change before it runs
|
||||
* @description Logs the change (unless this is a dry run) and refuses
|
||||
* destructive changes that were not approved.
|
||||
*/
|
||||
export default function recordSchemaChange(params, change) {
|
||||
params.changes?.push(change);
|
||||
if (params.dry_run) {
|
||||
return;
|
||||
}
|
||||
const allowed = params.allow_destructive;
|
||||
if (change.destructive &&
|
||||
allowed !== undefined &&
|
||||
allowed !== true &&
|
||||
!allowed.has(change.description)) {
|
||||
throw new Error(`Refusing unapproved destructive change: ${change.description}. Re-run with --dry-run to review, or --yes to allow it.`);
|
||||
}
|
||||
console.log(change.description);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import type { Database } from "bun:sqlite";
|
||||
type Params = {
|
||||
db: Database;
|
||||
table_name: string;
|
||||
};
|
||||
export default function removeDBManagerTable({ db, table_name }: Params): void;
|
||||
export {};
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import { AppData } from "../../data/app-data";
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
export default function removeDBManagerTable({ db, table_name }) {
|
||||
db.run(`DELETE FROM ${q(AppData["DbSchemaManagerTableName"])} WHERE table_name = ?`, [table_name]);
|
||||
}
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
import type { BUN_SQLITE_DatabaseSchemaType, BUN_SQLITE_TableSchemaType } from "../../types";
|
||||
type Params = {
|
||||
table: BUN_SQLITE_TableSchemaType;
|
||||
db_schema: BUN_SQLITE_DatabaseSchemaType;
|
||||
};
|
||||
/**
|
||||
* Resolve a table's effective definition. Tables with `parentTableName`
|
||||
* take their structure from the parent. Always returns a copy: the schema
|
||||
* objects are never mutated.
|
||||
*/
|
||||
export default function resolveTable({ table, db_schema, }: Params): BUN_SQLITE_TableSchemaType;
|
||||
export {};
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
import _ from "lodash";
|
||||
/**
|
||||
* Resolve a table's effective definition. Tables with `parentTableName`
|
||||
* take their structure from the parent. Always returns a copy: the schema
|
||||
* objects are never mutated.
|
||||
*/
|
||||
export default function resolveTable({ table, db_schema, }) {
|
||||
if (!table.parentTableName) {
|
||||
return _.cloneDeep(table);
|
||||
}
|
||||
const parent_table = db_schema.tables.find((t) => t.tableName === table.parentTableName);
|
||||
if (!parent_table) {
|
||||
throw new Error(`Parent table \`${table.parentTableName}\` not found for \`${table.tableName}\``);
|
||||
}
|
||||
return _.merge(_.cloneDeep(parent_table), {
|
||||
tableName: table.tableName,
|
||||
tableDescription: table.tableDescription,
|
||||
});
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
import type { BUN_SQLITE_TableSchemaType, CreateDBSchemaParams } from "../../types";
|
||||
type Params = CreateDBSchemaParams & {
|
||||
table: BUN_SQLITE_TableSchemaType;
|
||||
};
|
||||
/**
|
||||
* Sync indexes for a table
|
||||
*/
|
||||
export default function syncIndexes(params: Params): void;
|
||||
export {};
|
||||
Vendored
+39
@@ -0,0 +1,39 @@
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
import recordSchemaChange from "./record-schema-change";
|
||||
/**
|
||||
* Sync indexes for a table
|
||||
*/
|
||||
export default function syncIndexes(params) {
|
||||
const { db, table } = params;
|
||||
if (!table.indexes || table.indexes.length === 0) {
|
||||
return;
|
||||
}
|
||||
const existingIndexes = db
|
||||
.query(`SELECT name FROM sqlite_master WHERE type='index' AND tbl_name=? AND name NOT LIKE 'sqlite_%'`)
|
||||
.all(table.tableName).map((r) => r.name);
|
||||
// Drop indexes not in schema
|
||||
for (const indexName of existingIndexes) {
|
||||
const stillExists = table.indexes.some((idx) => idx.indexName === indexName);
|
||||
if (!stillExists) {
|
||||
recordSchemaChange(params, {
|
||||
description: `Drop index "${indexName}"`,
|
||||
});
|
||||
db.run(`DROP INDEX IF EXISTS ${q(indexName)}`);
|
||||
}
|
||||
}
|
||||
// Create new indexes
|
||||
for (const index of table.indexes) {
|
||||
if (!index.indexName || !index.indexTableFields?.length) {
|
||||
continue;
|
||||
}
|
||||
if (!existingIndexes.includes(index.indexName)) {
|
||||
recordSchemaChange(params, {
|
||||
description: `Create index "${index.indexName}" on "${table.tableName}"`,
|
||||
});
|
||||
const fields = index.indexTableFields
|
||||
.map((f) => q(String(f)))
|
||||
.join(", ");
|
||||
db.run(`CREATE INDEX ${q(index.indexName)} ON ${q(table.tableName)} (${fields})`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+13
@@ -0,0 +1,13 @@
|
||||
import type { BUN_SQLITE_TableSchemaType, CreateDBSchemaParams } from "../../types";
|
||||
type Params = CreateDBSchemaParams & {
|
||||
table: BUN_SQLITE_TableSchemaType;
|
||||
};
|
||||
/**
|
||||
* # Update an existing table
|
||||
* @description SQLite has limited ALTER TABLE support, so a changed table is
|
||||
* rebuilt: create a temp table with the new schema, copy the data over,
|
||||
* drop the old table and rename the temp table. Unchanged tables are left
|
||||
* alone.
|
||||
*/
|
||||
export default function updateTable(params: Params): void;
|
||||
export {};
|
||||
Vendored
+88
@@ -0,0 +1,88 @@
|
||||
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
||||
import buildCreateTableSql from "./build-create-table-sql";
|
||||
import checkIfTableExists from "./check-if-table-exists";
|
||||
import getTableColumns from "./get-table-columns";
|
||||
import recordSchemaChange from "./record-schema-change";
|
||||
import resolveTable from "./resolve-table";
|
||||
/**
|
||||
* # Update an existing table
|
||||
* @description SQLite has limited ALTER TABLE support, so a changed table is
|
||||
* rebuilt: create a temp table with the new schema, copy the data over,
|
||||
* drop the old table and rename the temp table. Unchanged tables are left
|
||||
* alone.
|
||||
*/
|
||||
export default function updateTable(params) {
|
||||
const { db, db_schema, table } = params;
|
||||
if (table.isVector) {
|
||||
recreateVectorTable(params);
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* SQLite keeps the `CREATE TABLE` text (with the renamed table name), so
|
||||
* an identical statement means the table already matches the schema.
|
||||
*/
|
||||
const current = db
|
||||
.query(`SELECT sql FROM sqlite_master WHERE type='table' AND name=?`)
|
||||
.get(table.tableName);
|
||||
if (current?.sql === buildCreateTableSql({ db_schema, table })) {
|
||||
return;
|
||||
}
|
||||
const tempTableName = `${table.tableName}_temp_${Date.now()}`;
|
||||
// Columns are matched by name only: SQLite converts values to the new
|
||||
// column affinity on copy, so a type change keeps the data instead of
|
||||
// silently dropping the column.
|
||||
const existingColumns = getTableColumns({ db, table_name: table.tableName });
|
||||
const schemaColumns = resolveTable({ table, db_schema }).fields.map((f) => f.fieldName);
|
||||
const columnsToKeep = existingColumns
|
||||
.map((c) => c.name)
|
||||
.filter((name) => schemaColumns.includes(name));
|
||||
const columnsToDrop = existingColumns
|
||||
.map((c) => c.name)
|
||||
.filter((name) => !schemaColumns.includes(name));
|
||||
recordSchemaChange(params, {
|
||||
description: `Rebuild table "${table.tableName}"`,
|
||||
});
|
||||
for (const column of columnsToDrop) {
|
||||
recordSchemaChange(params, {
|
||||
description: `Drop column "${table.tableName}"."${column}"`,
|
||||
destructive: true,
|
||||
});
|
||||
}
|
||||
// Create temp table with new schema
|
||||
db.run(buildCreateTableSql({
|
||||
db_schema,
|
||||
table: { ...table, tableName: tempTableName },
|
||||
}));
|
||||
// Copy data if there are common columns
|
||||
if (columnsToKeep.length > 0) {
|
||||
const columnList = columnsToKeep.map(q).join(", ");
|
||||
db.run(`INSERT INTO ${q(tempTableName)} (${columnList}) SELECT ${columnList} FROM ${q(table.tableName)}`);
|
||||
}
|
||||
db.run(`DROP TABLE ${q(table.tableName)}`);
|
||||
db.run(`ALTER TABLE ${q(tempTableName)} RENAME TO ${q(table.tableName)}`);
|
||||
}
|
||||
/**
|
||||
* Vector tables are only rebuilt when `recreate_vector_table` is set.
|
||||
*/
|
||||
function recreateVectorTable(params) {
|
||||
const { db, db_schema, table, recreate_vector_table } = params;
|
||||
if (!recreate_vector_table) {
|
||||
return;
|
||||
}
|
||||
recordSchemaChange(params, {
|
||||
description: `Recreate vector table "${table.tableName}"`,
|
||||
});
|
||||
let existingRows = [];
|
||||
if (checkIfTableExists({ db, table_name: table.tableName })) {
|
||||
existingRows = db
|
||||
.query(`SELECT * FROM ${q(table.tableName)}`)
|
||||
.all();
|
||||
db.run(`DROP TABLE ${q(table.tableName)}`);
|
||||
}
|
||||
db.run(buildCreateTableSql({ db_schema, table }));
|
||||
for (const row of existingRows) {
|
||||
const columns = Object.keys(row);
|
||||
const placeholders = columns.map(() => "?").join(", ");
|
||||
db.run(`INSERT INTO ${q(table.tableName)} (${columns.map(q).join(", ")}) VALUES (${placeholders})`, Object.values(row));
|
||||
}
|
||||
}
|
||||
Vendored
+3
-2
@@ -1,4 +1,4 @@
|
||||
import type { APIResponseObject, ServerQueryParam } from "../../types";
|
||||
import type { APIResponseObject, BunSQLiteConfig, ServerQueryParam } from "../../types";
|
||||
type Params<Schema extends {
|
||||
[k: string]: any;
|
||||
} = {
|
||||
@@ -7,10 +7,11 @@ type Params<Schema extends {
|
||||
table: Table;
|
||||
query?: ServerQueryParam<Schema>;
|
||||
targetId?: number | string;
|
||||
config?: BunSQLiteConfig;
|
||||
};
|
||||
export default function DbDelete<Schema extends {
|
||||
[k: string]: any;
|
||||
} = {
|
||||
[k: string]: any;
|
||||
}, Table extends string = string>({ table, query, targetId, }: Params<Schema, Table>): Promise<APIResponseObject>;
|
||||
}, Table extends string = string>({ table, query, targetId, config, }: Params<Schema, Table>): Promise<APIResponseObject>;
|
||||
export {};
|
||||
|
||||
Vendored
+19
-21
@@ -1,34 +1,32 @@
|
||||
import DbClient from ".";
|
||||
import dbHandler from "../db-handler";
|
||||
import _ from "lodash";
|
||||
import sqlGenerator from "../../utils/sql-generator";
|
||||
export default async function DbDelete({ table, query, targetId, }) {
|
||||
import { sqlIdent } from "../../utils/sql-safe";
|
||||
export default async function DbDelete({ table, query, targetId, config, }) {
|
||||
let sqlObj = null;
|
||||
try {
|
||||
let finalQuery = query || {};
|
||||
if (targetId) {
|
||||
finalQuery = _.merge(finalQuery, {
|
||||
query: {
|
||||
id: {
|
||||
value: String(targetId),
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
const finalQuery = query || {};
|
||||
sqlObj = sqlGenerator({
|
||||
tableName: table,
|
||||
genObject: finalQuery,
|
||||
genObject: _.pick(finalQuery, ["query", "searchOperator"]),
|
||||
targetId,
|
||||
});
|
||||
const whereClause = sqlObj.string.match(/WHERE .*/)?.[0];
|
||||
const whereClause = sqlObj.where.string;
|
||||
if (whereClause) {
|
||||
let sql = `DELETE FROM ${table} ${whereClause}`;
|
||||
let sql = `DELETE FROM ${sqlIdent(table, "table name")} ${whereClause}`;
|
||||
sqlObj.string = sql;
|
||||
const res = DbClient.run(sql, sqlObj.values);
|
||||
sqlObj.values = sqlObj.where.values;
|
||||
const res = dbHandler({
|
||||
query: sql,
|
||||
values: sqlObj.values,
|
||||
config,
|
||||
});
|
||||
if (!res.success) {
|
||||
return { ...res, debug: { sqlObj } };
|
||||
}
|
||||
return {
|
||||
success: Boolean(res.changes),
|
||||
postInsertReturn: {
|
||||
affectedRows: res.changes,
|
||||
insertId: Number(res.lastInsertRowid),
|
||||
},
|
||||
success: Boolean(res.postInsertReturn?.affectedRows),
|
||||
postInsertReturn: res.postInsertReturn,
|
||||
debug: {
|
||||
sqlObj,
|
||||
},
|
||||
|
||||
Vendored
+3
-2
@@ -1,4 +1,4 @@
|
||||
import type { APIResponseObject } from "../../types";
|
||||
import type { APIResponseObject, BunSQLiteConfig } from "../../types";
|
||||
type Params<Schema extends {
|
||||
[k: string]: any;
|
||||
} = {
|
||||
@@ -7,10 +7,11 @@ type Params<Schema extends {
|
||||
table: Table;
|
||||
data: Schema[];
|
||||
update_on_duplicate?: boolean;
|
||||
config?: BunSQLiteConfig;
|
||||
};
|
||||
export default function DbInsert<Schema extends {
|
||||
[k: string]: any;
|
||||
} = {
|
||||
[k: string]: any;
|
||||
}, Table extends string = string>({ table, data, update_on_duplicate, }: Params<Schema, Table>): Promise<APIResponseObject>;
|
||||
}, Table extends string = string>({ table, data, update_on_duplicate, config, }: Params<Schema, Table>): Promise<APIResponseObject>;
|
||||
export {};
|
||||
|
||||
Vendored
+12
-8
@@ -1,7 +1,7 @@
|
||||
import DbClient from ".";
|
||||
import dbHandler from "../db-handler";
|
||||
import sqlInsertGenerator from "../../utils/sql-insert-generator";
|
||||
import grabDuplicateSafeInsertSql from "../grab-duplicate-safe-insert-sql";
|
||||
export default async function DbInsert({ table, data, update_on_duplicate, }) {
|
||||
export default async function DbInsert({ table, data, update_on_duplicate, config, }) {
|
||||
let sqlObj = null;
|
||||
try {
|
||||
const finalData = data.map((d) => ({
|
||||
@@ -19,13 +19,17 @@ export default async function DbInsert({ table, data, update_on_duplicate, }) {
|
||||
sql = await grabDuplicateSafeInsertSql({ data, table, sql });
|
||||
}
|
||||
(sqlObj || {}).query = sql;
|
||||
const res = DbClient.run(sql, sqlObj?.values || []);
|
||||
const res = dbHandler({
|
||||
query: sql,
|
||||
values: sqlObj?.values || [],
|
||||
config,
|
||||
});
|
||||
if (!res.success) {
|
||||
return { ...res, debug: { sqlObj } };
|
||||
}
|
||||
return {
|
||||
success: Boolean(Number(res.lastInsertRowid)),
|
||||
postInsertReturn: {
|
||||
affectedRows: res.changes,
|
||||
insertId: Number(res.lastInsertRowid),
|
||||
},
|
||||
success: Boolean(res.postInsertReturn?.insertId),
|
||||
postInsertReturn: res.postInsertReturn,
|
||||
debug: {
|
||||
sqlObj,
|
||||
},
|
||||
|
||||
-73
@@ -1,73 +0,0 @@
|
||||
#!/usr/bin/env bun
|
||||
import type { BUN_SQLITE_DatabaseSchemaType } from "../../types";
|
||||
declare class SQLiteSchemaManager {
|
||||
private db;
|
||||
private db_manager_table_name;
|
||||
private recreate_vector_table;
|
||||
private db_schema;
|
||||
constructor({ schema, recreate_vector_table, }: {
|
||||
schema: BUN_SQLITE_DatabaseSchemaType;
|
||||
recreate_vector_table?: boolean;
|
||||
});
|
||||
private createDbManagerTable;
|
||||
private insertDbManagerTable;
|
||||
private removeDbManagerTable;
|
||||
/**
|
||||
* Main synchronization method
|
||||
*/
|
||||
syncSchema(): Promise<void>;
|
||||
/**
|
||||
* Get list of existing tables in the database
|
||||
*/
|
||||
private getExistingTables;
|
||||
/**
|
||||
* Drop tables that are no longer in the schema
|
||||
*/
|
||||
private dropRemovedTables;
|
||||
/**
|
||||
* Sync a single table (create or update)
|
||||
*/
|
||||
private syncTable;
|
||||
/**
|
||||
* Create a new table
|
||||
*/
|
||||
private createTable;
|
||||
/**
|
||||
* Update an existing table
|
||||
*/
|
||||
private updateTable;
|
||||
/**
|
||||
* Get existing columns for a table
|
||||
*/
|
||||
private getTableColumns;
|
||||
/**
|
||||
* Add a new column to existing table
|
||||
*/
|
||||
private addColumn;
|
||||
private checkIfTableExists;
|
||||
/**
|
||||
* Recreate table (for complex schema changes)
|
||||
*/
|
||||
private recreateTable;
|
||||
/**
|
||||
* Build column definition SQL
|
||||
*/
|
||||
private buildColumnDefinition;
|
||||
/**
|
||||
* Map DSQL data types to SQLite types
|
||||
*/
|
||||
private mapDataType;
|
||||
/**
|
||||
* Build foreign key constraint
|
||||
*/
|
||||
private buildForeignKeyConstraint;
|
||||
/**
|
||||
* Sync indexes for a table
|
||||
*/
|
||||
private syncIndexes;
|
||||
/**
|
||||
* Close database connection
|
||||
*/
|
||||
close(): void;
|
||||
}
|
||||
export { SQLiteSchemaManager };
|
||||
Vendored
-425
@@ -1,425 +0,0 @@
|
||||
#!/usr/bin/env bun
|
||||
import { Database } from "bun:sqlite";
|
||||
import _ from "lodash";
|
||||
import DbClient from ".";
|
||||
import { AppData } from "../../data/app-data";
|
||||
import { readLiveSchema } from "../../functions/live-schema";
|
||||
// Schema Manager Class
|
||||
class SQLiteSchemaManager {
|
||||
db;
|
||||
db_manager_table_name;
|
||||
recreate_vector_table;
|
||||
db_schema;
|
||||
constructor({ schema, recreate_vector_table = false, }) {
|
||||
this.db = DbClient;
|
||||
this.db_manager_table_name = AppData["DbSchemaManagerTableName"];
|
||||
this.db.run("PRAGMA foreign_keys = ON;");
|
||||
this.recreate_vector_table = recreate_vector_table;
|
||||
this.createDbManagerTable();
|
||||
this.db_schema = schema;
|
||||
}
|
||||
createDbManagerTable() {
|
||||
this.db.run(`
|
||||
CREATE TABLE IF NOT EXISTS ${this.db_manager_table_name} (
|
||||
table_name TEXT NOT NULL,
|
||||
created_at INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL
|
||||
)
|
||||
`);
|
||||
}
|
||||
insertDbManagerTable(tableName) {
|
||||
this.db.run(`INSERT INTO ${this.db_manager_table_name} (table_name,created_at,updated_at) VALUES (?, ?, ?)`, [tableName, Date.now(), Date.now()]);
|
||||
}
|
||||
removeDbManagerTable(tableName) {
|
||||
this.db.run(`DELETE FROM ${this.db_manager_table_name} WHERE table_name = ?`, [tableName]);
|
||||
}
|
||||
/**
|
||||
* Main synchronization method
|
||||
*/
|
||||
async syncSchema() {
|
||||
console.log("Starting schema synchronization...");
|
||||
const existingTables = this.getExistingTables();
|
||||
const schemaTables = this.db_schema.tables.map((t) => t.tableName);
|
||||
// 2. Create or update tables
|
||||
for (const table of this.db_schema.tables) {
|
||||
await this.syncTable(table, existingTables);
|
||||
}
|
||||
// 1. Drop tables that no longer exist in schema
|
||||
await this.dropRemovedTables(existingTables, schemaTables);
|
||||
console.log("Schema synchronization complete!");
|
||||
}
|
||||
/**
|
||||
* Get list of existing tables in the database
|
||||
*/
|
||||
getExistingTables() {
|
||||
let sql = `SELECT table_name FROM ${this.db_manager_table_name}`;
|
||||
const query = this.db.query(sql);
|
||||
const results = query.all();
|
||||
return results.map((r) => r.table_name);
|
||||
}
|
||||
/**
|
||||
* Drop tables that are no longer in the schema
|
||||
*/
|
||||
async dropRemovedTables(existingTables, schemaTables) {
|
||||
console.log(`Cleaning up tables ...`);
|
||||
const tablesToDrop = existingTables.filter((t) => !schemaTables.includes(t) &&
|
||||
!schemaTables.find((scT) => t.startsWith(scT + "_")));
|
||||
const currentSchema = readLiveSchema();
|
||||
if (currentSchema?.tables?.[0]) {
|
||||
for (let i = 0; i < currentSchema.tables.length; i++) {
|
||||
const table = currentSchema.tables[i];
|
||||
if (!table?.tableName)
|
||||
continue;
|
||||
const does_table_exist = schemaTables.find((t) => t == table.tableName);
|
||||
if (!does_table_exist) {
|
||||
tablesToDrop.push(table.tableName);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const tableName of tablesToDrop) {
|
||||
console.log(`Dropping table: ${tableName}`);
|
||||
this.db.run(`DROP TABLE IF EXISTS "${tableName}"`);
|
||||
this.db.run(`DELETE FROM ${this.db_manager_table_name} WHERE table_name = "${tableName}"`);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Sync a single table (create or update)
|
||||
*/
|
||||
async syncTable(table, existingTables) {
|
||||
let tableExists = existingTables.includes(table.tableName);
|
||||
// Handle table rename
|
||||
if (table.tableNameOld && table.tableNameOld !== table.tableName) {
|
||||
if (existingTables.includes(table.tableNameOld)) {
|
||||
console.log(`Renaming table: ${table.tableNameOld} -> ${table.tableName}`);
|
||||
this.db.run(`ALTER TABLE "${table.tableNameOld}" RENAME TO "${table.tableName}"`);
|
||||
this.insertDbManagerTable(table.tableName);
|
||||
this.removeDbManagerTable(table.tableNameOld);
|
||||
tableExists = true;
|
||||
}
|
||||
}
|
||||
if (!tableExists) {
|
||||
// Create new table
|
||||
await this.createTable(table);
|
||||
this.insertDbManagerTable(table.tableName);
|
||||
}
|
||||
else {
|
||||
// Update existing table
|
||||
await this.updateTable(table);
|
||||
}
|
||||
// Sync indexes
|
||||
await this.syncIndexes(table);
|
||||
}
|
||||
/**
|
||||
* Create a new table
|
||||
*/
|
||||
async createTable(table) {
|
||||
if (!table.tableName.match(/_temp_\d+$/)) {
|
||||
console.log(`Creating table: ${table.tableName}`);
|
||||
}
|
||||
let new_table = _.cloneDeep(table);
|
||||
if (new_table.parentTableName) {
|
||||
const parent_table = this.db_schema.tables.find((t) => t.tableName === new_table.parentTableName);
|
||||
if (!parent_table) {
|
||||
throw new Error(`Parent table \`${new_table.parentTableName}\` not found for \`${new_table.tableName}\``);
|
||||
}
|
||||
new_table = _.merge(parent_table, {
|
||||
tableName: new_table.tableName,
|
||||
tableDescription: new_table.tableDescription,
|
||||
});
|
||||
}
|
||||
const columns = [];
|
||||
const foreignKeys = [];
|
||||
for (const field of new_table.fields) {
|
||||
const columnDef = this.buildColumnDefinition(field, table.isVector);
|
||||
columns.push(columnDef);
|
||||
if (field.foreignKey && !table.isVector) {
|
||||
foreignKeys.push(this.buildForeignKeyConstraint(field));
|
||||
}
|
||||
}
|
||||
// Add unique constraints
|
||||
if (new_table.uniqueConstraints) {
|
||||
for (const constraint of new_table.uniqueConstraints) {
|
||||
if (constraint.constraintTableFields &&
|
||||
constraint.constraintTableFields.length > 0) {
|
||||
const fields = constraint.constraintTableFields
|
||||
.map((f) => `"${f.value}"`)
|
||||
.join(", ");
|
||||
const constraintName = constraint.constraintName ||
|
||||
`unique_${fields.replace(/"/g, "")}`;
|
||||
columns.push(`CONSTRAINT "${constraintName}" UNIQUE (${fields})`);
|
||||
}
|
||||
}
|
||||
}
|
||||
const allConstraints = [...columns, ...foreignKeys];
|
||||
const sql = new_table.isVector
|
||||
? `CREATE VIRTUAL TABLE "${new_table.tableName}" USING ${new_table.vectorType || "vec0"}(${allConstraints.join(", ")})`
|
||||
: `CREATE TABLE "${new_table.tableName}" (${allConstraints.join(", ")})`;
|
||||
this.db.run(sql);
|
||||
}
|
||||
/**
|
||||
* Update an existing table
|
||||
*/
|
||||
async updateTable(table) {
|
||||
console.log(`Updating table: ${table.tableName}`);
|
||||
// const existingColumns = this.getTableColumns(table.tableName);
|
||||
// const schemaColumns = table.fields.map((f) => f.fieldName || "");
|
||||
// // SQLite has limited ALTER TABLE support
|
||||
// // We need to use the recreation strategy for complex changes
|
||||
// const columnsToAdd = table.fields.filter(
|
||||
// (f) =>
|
||||
// f.fieldName &&
|
||||
// !existingColumns.find(
|
||||
// (c) =>
|
||||
// c.name == f.fieldName && c.type == this.mapDataType(f),
|
||||
// ),
|
||||
// );
|
||||
// // const columnsToRemove = existingColumns.filter(
|
||||
// // (c) => !schemaColumns.includes(c.name),
|
||||
// // );
|
||||
// // const columnsToUpdate = table.fields.filter(
|
||||
// // (f) =>
|
||||
// // f.fieldName &&
|
||||
// // f.updatedField &&
|
||||
// // existingColumns.find(
|
||||
// // (c) =>
|
||||
// // c.name == f.fieldName && c.type == this.mapDataType(f),
|
||||
// // ),
|
||||
// // );
|
||||
// for (const field of columnsToAdd) {
|
||||
// await this.addColumn(table.tableName, field);
|
||||
// }
|
||||
// // // Simple case: only adding columns
|
||||
// // if (columnsToRemove.length === 0 && columnsToUpdate.length === 0) {
|
||||
// // for (const field of columnsToAdd) {
|
||||
// // await this.addColumn(table.tableName, field);
|
||||
// // }
|
||||
// // } else {
|
||||
// // // Complex case: need to recreate table
|
||||
// // }
|
||||
await this.recreateTable(table);
|
||||
}
|
||||
/**
|
||||
* Get existing columns for a table
|
||||
*/
|
||||
getTableColumns(tableName) {
|
||||
const query = this.db.query(`PRAGMA table_info("${tableName}")`);
|
||||
const results = query.all();
|
||||
return results;
|
||||
}
|
||||
/**
|
||||
* Add a new column to existing table
|
||||
*/
|
||||
async addColumn(tableName, field) {
|
||||
console.log(`Adding column: ${tableName}.${field.fieldName}`);
|
||||
const columnDef = this.buildColumnDefinition(field);
|
||||
// Remove PRIMARY KEY and UNIQUE constraints for ALTER TABLE ADD COLUMN
|
||||
const cleanDef = columnDef
|
||||
.replace(/PRIMARY KEY/gi, "")
|
||||
.replace(/AUTOINCREMENT/gi, "")
|
||||
.replace(/UNIQUE/gi, "")
|
||||
.trim();
|
||||
const sql = `ALTER TABLE "${tableName}" ADD COLUMN ${cleanDef}`;
|
||||
this.db.run(sql);
|
||||
}
|
||||
checkIfTableExists(table) {
|
||||
const tableExists = this.db
|
||||
.query(`SELECT name FROM sqlite_master WHERE type='table' AND name=?`)
|
||||
.get(table);
|
||||
return Boolean(tableExists?.name);
|
||||
}
|
||||
/**
|
||||
* Recreate table (for complex schema changes)
|
||||
*/
|
||||
async recreateTable(table) {
|
||||
if (table.isVector) {
|
||||
if (!this.recreate_vector_table) {
|
||||
return;
|
||||
}
|
||||
const does_table_exist = this.checkIfTableExists(table.tableName);
|
||||
let existingRows = [];
|
||||
if (does_table_exist) {
|
||||
existingRows = this.db
|
||||
.query(`SELECT * FROM "${table.tableName}"`)
|
||||
.all();
|
||||
this.db.run(`DROP TABLE "${table.tableName}"`);
|
||||
}
|
||||
await this.createTable(table);
|
||||
if (existingRows.length > 0) {
|
||||
for (let i = 0; i < existingRows.length; i++) {
|
||||
const row = existingRows[i];
|
||||
if (!row)
|
||||
continue;
|
||||
const columns = Object.keys(row);
|
||||
const placeholders = columns.map(() => "?").join(", ");
|
||||
this.db.run(`INSERT INTO "${table.tableName}" (${columns.join(", ")}) VALUES (${placeholders})`, Object.values(row));
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
const tempTableName = `${table.tableName}_temp_${Date.now()}`;
|
||||
// Get existing data
|
||||
const existingColumns = this.getTableColumns(table.tableName);
|
||||
const columnsToKeep = table.fields
|
||||
.filter((f) => f.fieldName &&
|
||||
existingColumns.find((c) => c.name == f.fieldName &&
|
||||
c.type == this.mapDataType(f)))
|
||||
.map((f) => f.fieldName);
|
||||
// Create temp table with new schema
|
||||
const tempTable = { ...table, tableName: tempTableName };
|
||||
await this.createTable(tempTable);
|
||||
// Copy data if there are common columns
|
||||
if (columnsToKeep.length > 0) {
|
||||
const columnList = columnsToKeep.map((c) => `"${c}"`).join(", ");
|
||||
this.db.run(`INSERT INTO "${tempTableName}" (${columnList}) SELECT ${columnList} FROM "${table.tableName}"`);
|
||||
}
|
||||
// Drop old table
|
||||
this.db.run(`DROP TABLE "${table.tableName}"`);
|
||||
// Rename temp table
|
||||
this.db.run(`ALTER TABLE "${tempTableName}" RENAME TO "${table.tableName}"`);
|
||||
}
|
||||
/**
|
||||
* Build column definition SQL
|
||||
*/
|
||||
buildColumnDefinition(field, is_vector) {
|
||||
if (!field.fieldName) {
|
||||
throw new Error("Field name is required");
|
||||
}
|
||||
const fieldName = field.sideCar
|
||||
? `+${field.fieldName}`
|
||||
: `${field.fieldName}`;
|
||||
const parts = [fieldName];
|
||||
// Data type mapping
|
||||
const dataType = this.mapDataType(field);
|
||||
parts.push(dataType);
|
||||
// Primary key
|
||||
if (field.primaryKey) {
|
||||
parts.push("PRIMARY KEY");
|
||||
if (field.autoIncrement) {
|
||||
parts.push("AUTOINCREMENT");
|
||||
}
|
||||
}
|
||||
// Not null
|
||||
if (field.notNullValue || field.primaryKey) {
|
||||
if (!field.primaryKey) {
|
||||
parts.push("NOT NULL");
|
||||
}
|
||||
}
|
||||
// Unique
|
||||
if (field.unique && !field.primaryKey) {
|
||||
parts.push("UNIQUE");
|
||||
}
|
||||
if (field.vectorDistanceMetric) {
|
||||
parts.push(`distance_metric=${field.vectorDistanceMetric}`);
|
||||
}
|
||||
// Default value
|
||||
if (field.defaultValue !== undefined) {
|
||||
if (typeof field.defaultValue === "string") {
|
||||
parts.push(
|
||||
// Escape single quotes by doubling them to prevent SQL injection and wrap in single quotes
|
||||
`DEFAULT '${field.defaultValue.replace(/'/g, "''")}'`);
|
||||
}
|
||||
else {
|
||||
parts.push(`DEFAULT ${field.defaultValue}`);
|
||||
}
|
||||
}
|
||||
else if (field.defaultValueLiteral) {
|
||||
parts.push(`DEFAULT ${field.defaultValueLiteral}`);
|
||||
}
|
||||
return parts.join(" ");
|
||||
}
|
||||
/**
|
||||
* Map DSQL data types to SQLite types
|
||||
*/
|
||||
mapDataType(field) {
|
||||
const dataType = field.dataType?.toLowerCase() || "text";
|
||||
const vectorSize = field.vectorSize || 1536;
|
||||
// Vector Embeddings
|
||||
if (field.isVector) {
|
||||
return `FLOAT[${vectorSize}]`;
|
||||
}
|
||||
// Integer types
|
||||
if (dataType.includes("int") ||
|
||||
dataType === "bigint" ||
|
||||
dataType === "smallint" ||
|
||||
dataType === "tinyint") {
|
||||
return "INTEGER";
|
||||
}
|
||||
// Real/Float types
|
||||
if (dataType.includes("real") ||
|
||||
dataType.includes("float") ||
|
||||
dataType.includes("double") ||
|
||||
dataType === "decimal" ||
|
||||
dataType === "numeric") {
|
||||
return "REAL";
|
||||
}
|
||||
// Blob types
|
||||
if (dataType.includes("blob") || dataType.includes("binary")) {
|
||||
return "BLOB";
|
||||
}
|
||||
// Boolean
|
||||
if (dataType === "boolean" || dataType === "bool") {
|
||||
return "INTEGER"; // SQLite uses INTEGER for boolean (0/1)
|
||||
}
|
||||
// Date/Time types
|
||||
if (dataType.includes("date") || dataType.includes("time")) {
|
||||
return "TEXT"; // SQLite stores dates as TEXT or INTEGER
|
||||
}
|
||||
// Default to TEXT for all text-based types
|
||||
return "TEXT";
|
||||
}
|
||||
/**
|
||||
* Build foreign key constraint
|
||||
*/
|
||||
buildForeignKeyConstraint(field) {
|
||||
const fk = field.foreignKey;
|
||||
let constraint = `FOREIGN KEY ("${field.fieldName}") REFERENCES "${fk.destinationTableName}"("${fk.destinationTableColumnName}")`;
|
||||
if (fk.cascadeDelete) {
|
||||
constraint += " ON DELETE CASCADE";
|
||||
}
|
||||
if (fk.cascadeUpdate) {
|
||||
constraint += " ON UPDATE CASCADE";
|
||||
}
|
||||
return constraint;
|
||||
}
|
||||
/**
|
||||
* Sync indexes for a table
|
||||
*/
|
||||
async syncIndexes(table) {
|
||||
if (!table.indexes || table.indexes.length === 0) {
|
||||
return;
|
||||
}
|
||||
// Get existing indexes
|
||||
const query = this.db.query(`SELECT name FROM sqlite_master WHERE type='index' AND tbl_name='${table.tableName}' AND name NOT LIKE 'sqlite_%'`);
|
||||
const existingIndexes = query.all().map((r) => r.name);
|
||||
// Drop indexes not in schema
|
||||
for (const indexName of existingIndexes) {
|
||||
const stillExists = table.indexes.some((idx) => idx.indexName === indexName);
|
||||
if (!stillExists) {
|
||||
console.log(`Dropping index: ${indexName}`);
|
||||
this.db.run(`DROP INDEX IF EXISTS "${indexName}"`);
|
||||
}
|
||||
}
|
||||
// Create new indexes
|
||||
for (const index of table.indexes) {
|
||||
if (!index.indexName ||
|
||||
!index.indexTableFields ||
|
||||
index.indexTableFields.length === 0) {
|
||||
continue;
|
||||
}
|
||||
if (!existingIndexes.includes(index.indexName)) {
|
||||
console.log(`Creating index: ${index.indexName}`);
|
||||
const fields = index.indexTableFields
|
||||
.map((f) => `"${f}"`)
|
||||
.join(", ");
|
||||
this.db.run(`CREATE INDEX "${index.indexName}" ON "${table.tableName}" (${fields})`);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Close database connection
|
||||
*/
|
||||
close() {
|
||||
this.db.close();
|
||||
}
|
||||
}
|
||||
export { SQLiteSchemaManager };
|
||||
+1
-1
@@ -18,7 +18,7 @@ export default function dbSchemaToType({ config, dbSchema, }) {
|
||||
if (final_table.parentTableName) {
|
||||
const parent_table = dbTablesSchemas.find((t) => t.tableName === final_table.parentTableName);
|
||||
if (parent_table) {
|
||||
final_table = _.merge(parent_table, {
|
||||
final_table = _.merge(_.cloneDeep(parent_table), {
|
||||
tableName: final_table.tableName,
|
||||
tableDescription: final_table.tableDescription,
|
||||
});
|
||||
|
||||
Vendored
+3
-2
@@ -1,4 +1,4 @@
|
||||
import type { APIResponseObject, ServerQueryParam } from "../../types";
|
||||
import type { APIResponseObject, BunSQLiteConfig, ServerQueryParam } from "../../types";
|
||||
type Params<Schema extends {
|
||||
[k: string]: any;
|
||||
} = {
|
||||
@@ -8,10 +8,11 @@ type Params<Schema extends {
|
||||
table: Table;
|
||||
count?: boolean;
|
||||
targetId?: number | string;
|
||||
config?: BunSQLiteConfig;
|
||||
};
|
||||
export default function DbSelect<Schema extends {
|
||||
[k: string]: any;
|
||||
} = {
|
||||
[k: string]: any;
|
||||
}, Table extends string = string>({ table, query, count, targetId, }: Params<Schema, Table>): Promise<APIResponseObject<Schema>>;
|
||||
}, Table extends string = string>({ table, query, count, targetId, config, }: Params<Schema, Table>): Promise<APIResponseObject<Schema>>;
|
||||
export {};
|
||||
|
||||
Vendored
+32
-23
@@ -1,27 +1,24 @@
|
||||
import mysql from "mysql";
|
||||
import DbClient from ".";
|
||||
import _ from "lodash";
|
||||
import dbHandler from "../db-handler";
|
||||
import sqlGenerator from "../../utils/sql-generator";
|
||||
export default async function DbSelect({ table, query, count, targetId, }) {
|
||||
export default async function DbSelect({ table, query, count, targetId, config, }) {
|
||||
let sqlObj = null;
|
||||
try {
|
||||
let finalQuery = query || {};
|
||||
if (targetId) {
|
||||
finalQuery = _.merge(finalQuery, {
|
||||
query: {
|
||||
id: {
|
||||
value: String(targetId),
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
const finalQuery = query || {};
|
||||
sqlObj = sqlGenerator({
|
||||
tableName: table,
|
||||
genObject: finalQuery,
|
||||
targetId,
|
||||
});
|
||||
let sql = mysql.format(sqlObj.string, sqlObj.values);
|
||||
const res = DbClient.query(sql);
|
||||
const batchRes = res.all();
|
||||
const sql = sqlObj.string;
|
||||
const res = dbHandler({
|
||||
query: sql,
|
||||
values: sqlObj.values,
|
||||
config,
|
||||
});
|
||||
if (!res.success) {
|
||||
return { ...res, debug: { sqlObj, sql } };
|
||||
}
|
||||
const batchRes = res.payload || [];
|
||||
let resp = {
|
||||
success: Boolean(batchRes[0]),
|
||||
payload: batchRes,
|
||||
@@ -32,16 +29,28 @@ export default async function DbSelect({ table, query, count, targetId, }) {
|
||||
},
|
||||
};
|
||||
if (count) {
|
||||
let count_sql_object = sqlGenerator({
|
||||
const count_sql_object = sqlGenerator({
|
||||
tableName: table,
|
||||
genObject: finalQuery,
|
||||
targetId,
|
||||
count,
|
||||
});
|
||||
let count_sql = mysql.format(count_sql_object.string, count_sql_object.values);
|
||||
count_sql = `SELECT COUNT(*) FROM (${count_sql}) as c`;
|
||||
const count_res = DbClient.query(count_sql).all();
|
||||
const count_val = count_res[0]?.["COUNT(*)"];
|
||||
resp["count"] = Number(count_val);
|
||||
const count_sql = `SELECT COUNT(*) AS count FROM (${count_sql_object.string}) AS c`;
|
||||
const count_res = dbHandler({
|
||||
query: count_sql,
|
||||
values: count_sql_object.values,
|
||||
config,
|
||||
});
|
||||
if (!count_res.success) {
|
||||
return {
|
||||
...resp,
|
||||
success: false,
|
||||
error: count_res.error,
|
||||
msg: "Database count failed",
|
||||
debug: { ...resp.debug, count_sql },
|
||||
};
|
||||
}
|
||||
resp["count"] = Number(count_res.singleRes?.count);
|
||||
resp["debug"]["count_sql"] = count_sql;
|
||||
}
|
||||
return resp;
|
||||
|
||||
Vendored
+3
-2
@@ -1,11 +1,12 @@
|
||||
import type { APIResponseObject, SQLInsertGenValueType } from "../../types";
|
||||
import type { APIResponseObject, BunSQLiteConfig, SQLInsertGenValueType } from "../../types";
|
||||
type Params = {
|
||||
sql: string;
|
||||
values?: SQLInsertGenValueType[];
|
||||
config?: BunSQLiteConfig;
|
||||
};
|
||||
export default function DbSQL<T extends {
|
||||
[k: string]: any;
|
||||
} = {
|
||||
[k: string]: any;
|
||||
}>({ sql, values }: Params): Promise<APIResponseObject<T>>;
|
||||
}>({ sql, values, config }: Params): Promise<APIResponseObject<T>>;
|
||||
export {};
|
||||
|
||||
Vendored
+16
-32
@@ -1,34 +1,18 @@
|
||||
import DbClient from ".";
|
||||
import _ from "lodash";
|
||||
export default async function DbSQL({ sql, values }) {
|
||||
try {
|
||||
const trimmed_sql = sql.trim();
|
||||
const res = trimmed_sql.match(/^select/i)
|
||||
? DbClient.query(trimmed_sql).all(...(values || []))
|
||||
: DbClient.run(trimmed_sql, values || []);
|
||||
return {
|
||||
success: true,
|
||||
payload: Array.isArray(res) ? res : undefined,
|
||||
singleRes: Array.isArray(res) ? res?.[0] : undefined,
|
||||
postInsertReturn: Array.isArray(res)
|
||||
? undefined
|
||||
: {
|
||||
affectedRows: res.changes,
|
||||
insertId: Number(res.lastInsertRowid),
|
||||
},
|
||||
debug: {
|
||||
sqlObj: {
|
||||
sql: trimmed_sql,
|
||||
values,
|
||||
},
|
||||
sql,
|
||||
import dbHandler from "../db-handler";
|
||||
export default async function DbSQL({ sql, values, config }) {
|
||||
const trimmed_sql = sql.trim();
|
||||
const res = dbHandler({ query: trimmed_sql, values, config });
|
||||
if (!res.success) {
|
||||
return res;
|
||||
}
|
||||
return {
|
||||
...res,
|
||||
debug: {
|
||||
sqlObj: {
|
||||
sql: trimmed_sql,
|
||||
values,
|
||||
},
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
sql,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Vendored
+3
-2
@@ -1,4 +1,4 @@
|
||||
import type { APIResponseObject, ServerQueryParam } from "../../types";
|
||||
import type { APIResponseObject, BunSQLiteConfig, ServerQueryParam } from "../../types";
|
||||
type Params<Schema extends {
|
||||
[k: string]: any;
|
||||
} = {
|
||||
@@ -8,10 +8,11 @@ type Params<Schema extends {
|
||||
data: Schema;
|
||||
query?: ServerQueryParam<Schema>;
|
||||
targetId?: number | string;
|
||||
config?: BunSQLiteConfig;
|
||||
};
|
||||
export default function DbUpdate<Schema extends {
|
||||
[k: string]: any;
|
||||
} = {
|
||||
[k: string]: any;
|
||||
}, Table extends string = string>({ table, data, query, targetId, }: Params<Schema, Table>): Promise<APIResponseObject>;
|
||||
}, Table extends string = string>({ table, data, query, targetId, config, }: Params<Schema, Table>): Promise<APIResponseObject>;
|
||||
export {};
|
||||
|
||||
Vendored
+22
-28
@@ -1,27 +1,24 @@
|
||||
import DbClient from ".";
|
||||
import dbHandler from "../db-handler";
|
||||
import _ from "lodash";
|
||||
import sqlGenerator from "../../utils/sql-generator";
|
||||
export default async function DbUpdate({ table, data, query, targetId, }) {
|
||||
let sqlObj = { string: "", values: [] };
|
||||
import { sqlValue } from "../../utils/sql-insert-generator";
|
||||
import { sqlIdent } from "../../utils/sql-safe";
|
||||
export default async function DbUpdate({ table, data, query, targetId, config, }) {
|
||||
let sqlObj = {
|
||||
string: "",
|
||||
values: [],
|
||||
};
|
||||
try {
|
||||
let finalQuery = query || {};
|
||||
if (targetId) {
|
||||
finalQuery = _.merge(finalQuery, {
|
||||
query: {
|
||||
id: {
|
||||
value: String(targetId),
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
const finalQuery = query || {};
|
||||
const sqlQueryObj = sqlGenerator({
|
||||
tableName: table,
|
||||
genObject: finalQuery,
|
||||
genObject: _.pick(finalQuery, ["query", "searchOperator"]),
|
||||
targetId,
|
||||
});
|
||||
let values = [];
|
||||
const whereClause = sqlQueryObj.string.match(/WHERE .*/)?.[0];
|
||||
const whereClause = sqlQueryObj.where.string;
|
||||
if (whereClause) {
|
||||
let sql = `UPDATE ${table} SET`;
|
||||
let sql = `UPDATE ${sqlIdent(table, "table name")} SET`;
|
||||
const finalData = {
|
||||
updated_at: Date.now(),
|
||||
...data,
|
||||
@@ -32,26 +29,23 @@ export default async function DbUpdate({ table, data, query, targetId, }) {
|
||||
if (!key)
|
||||
continue;
|
||||
const isLast = i == keys.length - 1;
|
||||
sql += ` ${key}=?`;
|
||||
const value = finalData[key];
|
||||
values.push(typeof value == "number" || typeof value == "string"
|
||||
? value
|
||||
: value || null);
|
||||
sql += ` ${sqlIdent(key, "column name")}=?`;
|
||||
values.push(sqlValue(finalData[key]));
|
||||
if (!isLast) {
|
||||
sql += `,`;
|
||||
}
|
||||
}
|
||||
sql += ` ${whereClause}`;
|
||||
values = [...values, ...sqlQueryObj.values];
|
||||
values = [...values, ...sqlQueryObj.where.values];
|
||||
sqlObj.string = sql;
|
||||
sqlObj.values = values;
|
||||
const res = DbClient.run(sql, values);
|
||||
const res = dbHandler({ query: sql, values, config });
|
||||
if (!res.success) {
|
||||
return { ...res, debug: { sqlObj } };
|
||||
}
|
||||
return {
|
||||
success: Boolean(res.changes),
|
||||
postInsertReturn: {
|
||||
affectedRows: res.changes,
|
||||
insertId: Number(res.lastInsertRowid),
|
||||
},
|
||||
success: Boolean(res.postInsertReturn?.affectedRows),
|
||||
postInsertReturn: res.postInsertReturn,
|
||||
debug: {
|
||||
sqlObj,
|
||||
},
|
||||
|
||||
Vendored
-3
@@ -1,3 +0,0 @@
|
||||
import { Database } from "bun:sqlite";
|
||||
declare const DbClient: Database;
|
||||
export default DbClient;
|
||||
Vendored
-20
@@ -1,20 +0,0 @@
|
||||
import { Database } from "bun:sqlite";
|
||||
import * as sqliteVec from "sqlite-vec";
|
||||
import grabDirNames from "../../data/grab-dir-names";
|
||||
import init from "../../functions/init";
|
||||
import grabDBDir from "../../utils/grab-db-dir";
|
||||
const { ROOT_DIR } = grabDirNames();
|
||||
const { config } = await init();
|
||||
let db_dir = ROOT_DIR;
|
||||
if (config.db_dir) {
|
||||
db_dir = config.db_dir;
|
||||
}
|
||||
const { db_file_path } = grabDBDir({ config });
|
||||
const DbClient = new Database(db_file_path, {
|
||||
create: true,
|
||||
});
|
||||
sqliteVec.load(DbClient);
|
||||
if (config.wal_mode) {
|
||||
DbClient.run("PRAGMA journal_mode = WAL;");
|
||||
}
|
||||
export default DbClient;
|
||||
Vendored
-2
@@ -1,2 +0,0 @@
|
||||
import type { BUN_SQLITE_DatabaseSchemaType } from "../../types";
|
||||
export declare const DbSchema: BUN_SQLITE_DatabaseSchemaType;
|
||||
Vendored
-5
@@ -1,5 +0,0 @@
|
||||
import _ from "lodash";
|
||||
export const DbSchema = {
|
||||
dbName: "travis-ai",
|
||||
tables: [],
|
||||
};
|
||||
Reference in New Issue
Block a user