Major version update. Major bugfixes. Security vulnerabilites fixed

This commit is contained in:
2026-10-14 03:02:07 +01:00
parent 4e47bd8ce0
commit 7c095f6f16
112 changed files with 2479 additions and 1219 deletions
+3 -2
View File
@@ -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 {};
+19 -21
View File
@@ -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,
},
+3 -2
View File
@@ -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 {};
+12 -8
View File
@@ -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
View File
@@ -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 };
-425
View File
@@ -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
View File
@@ -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,
});
+3 -2
View File
@@ -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 {};
+32 -23
View File
@@ -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;
+3 -2
View File
@@ -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 {};
+16 -32
View File
@@ -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,
},
};
}
+3 -2
View File
@@ -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 {};
+22 -28
View File
@@ -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,
},
-3
View File
@@ -1,3 +0,0 @@
import { Database } from "bun:sqlite";
declare const DbClient: Database;
export default DbClient;
-20
View File
@@ -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;
-2
View File
@@ -1,2 +0,0 @@
import type { BUN_SQLITE_DatabaseSchemaType } from "../../types";
export declare const DbSchema: BUN_SQLITE_DatabaseSchemaType;
-5
View File
@@ -1,5 +0,0 @@
import _ from "lodash";
export const DbSchema = {
dbName: "travis-ai",
tables: [],
};