Refactor DB Handler. Use Bun native SQL adapter.

This commit is contained in:
2026-06-24 15:52:19 +01:00
parent eb86e1803d
commit 44f6c18a84
107 changed files with 324 additions and 4923 deletions
-16
View File
@@ -1,16 +0,0 @@
import type { APIResponseObject, ServerQueryParam } from "../../types";
type Params<Schema extends {
[k: string]: any;
} = {
[k: string]: any;
}, Table extends string = string> = {
table: Table;
query?: ServerQueryParam<Schema>;
targetId?: number | string;
};
export default function DbDelete<Schema extends {
[k: string]: any;
} = {
[k: string]: any;
}, Table extends string = string>({ table, query, targetId, }: Params<Schema, Table>): Promise<APIResponseObject>;
export {};
-56
View File
@@ -1,56 +0,0 @@
import DbClient from ".";
import _ from "lodash";
import sqlGenerator from "../../utils/sql-generator";
export default async function DbDelete({ table, query, targetId, }) {
let sqlObj = null;
try {
let finalQuery = query || {};
if (targetId) {
finalQuery = _.merge(finalQuery, {
query: {
id: {
value: String(targetId),
},
},
});
}
sqlObj = sqlGenerator({
tableName: table,
genObject: finalQuery,
});
const whereClause = sqlObj.string.match(/WHERE .*/)?.[0];
if (whereClause) {
let sql = `DELETE FROM ${table} ${whereClause}`;
sqlObj.string = sql;
const res = DbClient.run(sql, sqlObj.values);
return {
success: Boolean(res.changes),
postInsertReturn: {
affectedRows: res.changes,
insertId: Number(res.lastInsertRowid),
},
debug: {
sqlObj,
},
};
}
else {
return {
success: false,
msg: `No WHERE clause`,
debug: {
sqlObj,
},
};
}
}
catch (error) {
return {
success: false,
error: error.message,
debug: {
sqlObj,
},
};
}
}
-15
View File
@@ -1,15 +0,0 @@
import type { BUN_MARIADB_TableSchemaType } from "../../types";
type Param = {
paradigm: "JavaScript" | "TypeScript" | undefined;
table: BUN_MARIADB_TableSchemaType;
query?: any;
typeDefName?: string;
allValuesOptional?: boolean;
addExport?: boolean;
dbName?: string;
};
export default function generateTypeDefinition({ paradigm, table, query, typeDefName, allValuesOptional, addExport, dbName, }: Param): {
typeDefinition: string | null;
tdName: string;
};
export {};
-63
View File
@@ -1,63 +0,0 @@
export default function generateTypeDefinition({ paradigm, table, query, typeDefName, allValuesOptional, addExport, dbName, }) {
let typeDefinition = ``;
let tdName = ``;
try {
tdName = typeDefName
? typeDefName
: dbName
? `BUN_MARIADB_${dbName}_${table.tableName}`.toUpperCase()
: `BUN_MARIADB_${query.single}_${query.single_table}`.toUpperCase();
const fields = table.fields;
function typeMap(schemaType) {
if (schemaType.options && schemaType.options.length > 0) {
let opts = schemaType.options.map((opt) => schemaType.dataType?.match(/int/i) || typeof opt == "number"
? `${opt}`
: `"${opt}"`);
opts.push(`""`);
return opts.join(" | ");
}
if (schemaType.dataType?.match(/blob/i)) {
return `Float32Array<ArrayBuffer> | Buffer<ArrayBuffer> | null`;
}
if (schemaType.dataType?.match(/int|double|decimal|real/i)) {
return `number | ""`;
}
if (schemaType.dataType?.match(/text|varchar|timestamp/i)) {
return `string`;
}
if (schemaType.dataType?.match(/boolean/i)) {
return "0 | 1";
}
return "string";
}
const typesArrayTypeScript = [];
const typesArrayJavascript = [];
typesArrayTypeScript.push(`${addExport ? "export " : ""}type ${tdName} = {`);
typesArrayJavascript.push(`/**\n * @typedef {object} ${tdName}`);
fields.forEach((field) => {
if (field.fieldDescription) {
typesArrayTypeScript.push(` /** \n * ${field.fieldDescription}\n */`);
}
const nullValue = allValuesOptional
? "?"
: field.notNullValue
? ""
: "?";
typesArrayTypeScript.push(` ${field.fieldName}${nullValue}: ${typeMap(field)};`);
typesArrayJavascript.push(` * @property {${typeMap(field)}${nullValue}} ${field.fieldName}`);
});
typesArrayTypeScript.push(`}`);
typesArrayJavascript.push(` */`);
if (paradigm?.match(/javascript/i)) {
typeDefinition = typesArrayJavascript.join("\n");
}
if (paradigm?.match(/typescript/i)) {
typeDefinition = typesArrayTypeScript.join("\n");
}
}
catch (error) {
console.log(error.message);
typeDefinition = null;
}
return { typeDefinition, tdName };
}
-16
View File
@@ -1,16 +0,0 @@
import type { APIResponseObject } from "../../types";
type Params<Schema extends {
[k: string]: any;
} = {
[k: string]: any;
}, Table extends string = string> = {
table: Table;
data: Schema[];
update_on_duplicate?: boolean;
};
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>;
export {};
-43
View File
@@ -1,43 +0,0 @@
import DbClient from ".";
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, }) {
let sqlObj = null;
try {
const finalData = data.map((d) => ({
created_at: Date.now(),
updated_at: Date.now(),
...d,
}));
sqlObj =
sqlInsertGenerator({
tableName: table,
data: finalData,
}) || null;
let sql = sqlObj?.query || "";
if (update_on_duplicate && data[0]) {
sql = await grabDuplicateSafeInsertSql({ data, table, sql });
}
(sqlObj || {}).query = sql;
const res = DbClient.run(sql, sqlObj?.values || []);
return {
success: Boolean(Number(res.lastInsertRowid)),
postInsertReturn: {
affectedRows: res.changes,
insertId: Number(res.lastInsertRowid),
},
debug: {
sqlObj,
},
};
}
catch (error) {
return {
success: false,
error: error.message,
debug: {
sqlObj,
},
};
}
}
-39
View File
@@ -1,39 +0,0 @@
#!/usr/bin/env bun
import type { BUN_MARIADB_DatabaseSchemaType } from "../../types";
declare class MariaDBSchemaManager {
private db_manager_table_name;
private recreate_vector_table;
private db_schema;
constructor({ schema, recreate_vector_table, }: {
schema: BUN_MARIADB_DatabaseSchemaType;
recreate_vector_table?: boolean;
});
syncSchema(): Promise<void>;
private getDatabaseName;
private quoteIdentifier;
private tableSchemaWhere;
private run;
private query;
private createDbManagerTable;
private insertDbManagerTable;
private removeDbManagerTable;
private getExistingTables;
private getLiveTableNames;
private dropRemovedTables;
private syncTable;
private resolveTable;
private createTable;
private buildTableOptions;
private updateTable;
private getTableColumns;
private addColumn;
private checkIfTableExists;
private recreateTable;
private insertRows;
private buildColumnDefinition;
private mapDataType;
private buildForeignKeyConstraint;
private syncIndexes;
close(): void;
}
export { MariaDBSchemaManager };
-419
View File
@@ -1,419 +0,0 @@
#!/usr/bin/env bun
import _ from "lodash";
import dbHandler from "../db-handler";
import { AppData } from "../../data/app-data";
import { readLiveSchema } from "../../functions/live-schema";
class MariaDBSchemaManager {
db_manager_table_name;
recreate_vector_table;
db_schema;
constructor({ schema, recreate_vector_table = false, }) {
this.db_manager_table_name = AppData["DbSchemaManagerTableName"];
this.recreate_vector_table = recreate_vector_table;
this.db_schema = schema;
}
async syncSchema() {
console.log("Starting schema synchronization...");
await this.createDbManagerTable();
const existingTables = await this.getExistingTables();
const schemaTables = this.db_schema.tables.map((t) => t.tableName);
for (const table of this.db_schema.tables) {
await this.syncTable(table, existingTables);
}
await this.dropRemovedTables(existingTables, schemaTables);
console.log("Schema synchronization complete!");
}
getDatabaseName() {
return this.db_schema.dbName || this.db_schema.dbSlug;
}
quoteIdentifier(identifier) {
return `\`${identifier.replace(/`/g, "``")}\``;
}
tableSchemaWhere(tableName) {
const databaseName = this.getDatabaseName();
if (databaseName) {
return {
where: "TABLE_SCHEMA = ?",
values: [databaseName, tableName],
};
}
return {
where: "TABLE_SCHEMA = DATABASE()",
values: [tableName],
};
}
async run(query, values) {
const res = await dbHandler({
query,
values: values,
config: this.getDatabaseName()
? { database: this.getDatabaseName() }
: undefined,
});
if (!res.success) {
throw new Error(`Database query failed: ${query}`);
}
}
async query(query, values) {
const res = await dbHandler({
query,
values: values,
config: this.getDatabaseName()
? { database: this.getDatabaseName() }
: undefined,
});
if (!res.success) {
throw new Error(`Database query failed: ${query}`);
}
return (res.payload || []);
}
async createDbManagerTable() {
await this.run(`
CREATE TABLE IF NOT EXISTS ${this.quoteIdentifier(this.db_manager_table_name)} (
table_name VARCHAR(255) NOT NULL PRIMARY KEY,
created_at BIGINT NOT NULL,
updated_at BIGINT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci
`);
}
async insertDbManagerTable(tableName) {
const now = Date.now();
await this.run(`INSERT INTO ${this.quoteIdentifier(this.db_manager_table_name)} (table_name, created_at, updated_at) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE updated_at = VALUES(updated_at)`, [tableName, now, now]);
}
async removeDbManagerTable(tableName) {
await this.run(`DELETE FROM ${this.quoteIdentifier(this.db_manager_table_name)} WHERE table_name = ?`, [tableName]);
}
async getExistingTables() {
const rows = await this.query(`SELECT table_name FROM ${this.quoteIdentifier(this.db_manager_table_name)}`);
return rows.map((row) => row.table_name);
}
async getLiveTableNames() {
const tableSchemaWhere = this.tableSchemaWhere("");
const rows = await this.query(`SELECT TABLE_NAME FROM information_schema.TABLES WHERE ${tableSchemaWhere.where} AND TABLE_TYPE = 'BASE TABLE'`, tableSchemaWhere.values);
return rows.map((row) => row.TABLE_NAME);
}
async dropRemovedTables(existingTables, schemaTables) {
console.log(`Cleaning up tables ...`);
const tablesToDrop = existingTables.filter((tableName) => !schemaTables.includes(tableName) &&
!schemaTables.some((schemaTable) => tableName.startsWith(`${schemaTable}_`)));
const currentSchema = readLiveSchema();
if (currentSchema?.tables?.[0]) {
for (const table of currentSchema.tables) {
if (!table?.tableName)
continue;
const doesTableExist = schemaTables.find((tableName) => tableName === table.tableName);
if (!doesTableExist) {
tablesToDrop.push(table.tableName);
}
}
}
for (const tableName of tablesToDrop) {
console.log(`Dropping table: ${tableName}`);
await this.run(`DROP TABLE IF EXISTS ${this.quoteIdentifier(tableName)}`);
await this.removeDbManagerTable(tableName);
}
}
async syncTable(table, existingTables) {
let tableExists = existingTables.includes(table.tableName);
const liveTables = await this.getLiveTableNames();
if (table.tableNameOld && table.tableNameOld !== table.tableName) {
if (liveTables.includes(table.tableNameOld)) {
console.log(`Renaming table: ${table.tableNameOld} -> ${table.tableName}`);
await this.run(`RENAME TABLE ${this.quoteIdentifier(table.tableNameOld)} TO ${this.quoteIdentifier(table.tableName)}`);
await this.insertDbManagerTable(table.tableName);
await this.removeDbManagerTable(table.tableNameOld);
tableExists = true;
}
}
if (!tableExists) {
await this.createTable(table);
await this.insertDbManagerTable(table.tableName);
}
else {
await this.updateTable(table);
await this.insertDbManagerTable(table.tableName);
}
await this.syncIndexes(table);
}
resolveTable(table) {
if (!table.parentTableName) {
return _.cloneDeep(table);
}
const parentTable = this.db_schema.tables.find((schemaTable) => schemaTable.tableName === table.parentTableName);
if (!parentTable) {
throw new Error(`Parent table \`${table.parentTableName}\` not found for \`${table.tableName}\``);
}
return _.merge({}, parentTable, {
tableName: table.tableName,
tableDescription: table.tableDescription,
collation: table.collation,
fields: [...(parentTable.fields || []), ...(table.fields || [])],
indexes: [...(parentTable.indexes || []), ...(table.indexes || [])],
uniqueConstraints: [
...(parentTable.uniqueConstraints || []),
...(table.uniqueConstraints || []),
],
});
}
async createTable(table) {
if (!table.tableName.match(/_temp_\d+$/)) {
console.log(`Creating table: ${table.tableName}`);
}
const newTable = this.resolveTable(table);
const columnDefinitions = [];
const foreignKeys = [];
for (const field of newTable.fields || []) {
columnDefinitions.push(this.buildColumnDefinition(field));
if (field.foreignKey && !newTable.isVector) {
foreignKeys.push(this.buildForeignKeyConstraint(field));
}
}
if (newTable.uniqueConstraints) {
for (const constraint of newTable.uniqueConstraints) {
if (constraint.constraintTableFields &&
constraint.constraintTableFields.length > 0) {
const fields = constraint.constraintTableFields
.map((field) => this.quoteIdentifier(field.value))
.join(", ");
const constraintName = constraint.constraintName ||
`unique_${fields.replace(/`/g, "")}`;
columnDefinitions.push(`CONSTRAINT ${this.quoteIdentifier(constraintName)} UNIQUE (${fields})`);
}
}
}
const sql = `CREATE TABLE IF NOT EXISTS ${this.quoteIdentifier(newTable.tableName)} (${[...columnDefinitions, ...foreignKeys].join(", ")})${this.buildTableOptions(newTable)}`;
await this.run(sql);
}
buildTableOptions(table) {
const options = ["ENGINE=InnoDB"];
if (table.collation) {
options.push("DEFAULT CHARSET=utf8mb4", `COLLATE ${table.collation}`);
}
return ` ${options.join(" ")}`;
}
async updateTable(table) {
console.log(`Updating table: ${table.tableName}`);
await this.recreateTable(table);
}
async getTableColumns(tableName) {
const tableSchemaWhere = this.tableSchemaWhere(tableName);
const rows = await this.query(`SELECT COLUMN_NAME AS name, COLUMN_TYPE AS type FROM information_schema.COLUMNS WHERE ${tableSchemaWhere.where} AND TABLE_NAME = ? ORDER BY ORDINAL_POSITION`, tableSchemaWhere.values);
return rows.map((row) => ({
name: row.COLUMN_NAME,
type: row.COLUMN_TYPE,
}));
}
async addColumn(tableName, field) {
console.log(`Adding column: ${tableName}.${field.fieldName}`);
const columnDef = this.buildColumnDefinition(field)
.replace(/PRIMARY KEY/gi, "")
.replace(/AUTO_INCREMENT/gi, "")
.replace(/UNIQUE/gi, "")
.trim();
await this.run(`ALTER TABLE ${this.quoteIdentifier(tableName)} ADD COLUMN ${columnDef}`);
}
async checkIfTableExists(table) {
const tableSchemaWhere = this.tableSchemaWhere(table);
const row = await this.query(`SELECT 1 AS exists FROM information_schema.TABLES WHERE ${tableSchemaWhere.where} AND TABLE_NAME = ? LIMIT 1`, tableSchemaWhere.values);
return Boolean(row[0]?.exists);
}
async recreateTable(table) {
if (table.isVector && !this.recreate_vector_table) {
return;
}
const doesTableExist = await this.checkIfTableExists(table.tableName);
if (table.isVector) {
let existingRows = [];
if (doesTableExist) {
existingRows = await this.query(`SELECT * FROM ${this.quoteIdentifier(table.tableName)}`);
await this.run(`DROP TABLE ${this.quoteIdentifier(table.tableName)}`);
}
await this.createTable(table);
if (existingRows.length > 0) {
await this.insertRows(table.tableName, existingRows);
}
return;
}
const tempTableName = `${table.tableName}_temp_${Date.now()}`;
const existingColumns = await this.getTableColumns(table.tableName);
const columnsToKeep = table.fields
.filter((field) => existingColumns.some((column) => column.name === field.fieldName))
.map((field) => field.fieldName)
.filter((fieldName) => Boolean(fieldName));
await this.createTable({ ...table, tableName: tempTableName });
if (columnsToKeep.length > 0) {
const columnList = columnsToKeep
.map((column) => this.quoteIdentifier(column))
.join(", ");
await this.run(`INSERT INTO ${this.quoteIdentifier(tempTableName)} (${columnList}) SELECT ${columnList} FROM ${this.quoteIdentifier(table.tableName)}`);
}
await this.run(`DROP TABLE ${this.quoteIdentifier(table.tableName)}`);
await this.run(`RENAME TABLE ${this.quoteIdentifier(tempTableName)} TO ${this.quoteIdentifier(table.tableName)}`);
}
async insertRows(tableName, rows) {
for (const row of rows) {
const columns = Object.keys(row);
if (columns.length === 0) {
continue;
}
const values = columns.map((column) => row[column]);
const columnList = columns
.map((column) => this.quoteIdentifier(column))
.join(", ");
const placeholders = columns.map(() => "?").join(", ");
await this.run(`INSERT INTO ${this.quoteIdentifier(tableName)} (${columnList}) VALUES (${placeholders})`, values);
}
}
buildColumnDefinition(field) {
if (!field.fieldName) {
throw new Error("Field name is required");
}
const parts = [this.quoteIdentifier(field.fieldName)];
parts.push(this.mapDataType(field));
if (field.primaryKey) {
parts.push("PRIMARY KEY");
if (field.autoIncrement) {
parts.push("AUTO_INCREMENT");
}
}
if (field.notNullValue || field.primaryKey) {
if (!field.primaryKey) {
parts.push("NOT NULL");
}
}
if (field.unique && !field.primaryKey) {
parts.push("UNIQUE");
}
if (field.defaultValue !== undefined) {
if (typeof field.defaultValue === "string") {
parts.push(`DEFAULT '${field.defaultValue.replace(/'/g, "''")}'`);
}
else {
parts.push(`DEFAULT ${field.defaultValue}`);
}
}
else if (field.defaultValueLiteral) {
parts.push(`DEFAULT ${field.defaultValueLiteral}`);
}
if (field.onUpdate) {
parts.push(`ON UPDATE ${field.onUpdate}`);
}
else if (field.onUpdateLiteral) {
parts.push(`ON UPDATE ${field.onUpdateLiteral}`);
}
return parts.join(" ");
}
mapDataType(field) {
const dataType = field.dataType?.toLowerCase() || "text";
const vectorSize = field.vectorSize || 1536;
if (field.isVector) {
return `LONGTEXT COMMENT 'vector_size=${vectorSize}'`;
}
if (dataType.includes("int") ||
dataType === "bigint" ||
dataType === "smallint" ||
dataType === "tinyint") {
if (field.integerLength) {
return `INT(${field.integerLength})`;
}
return "INT";
}
if (dataType === "bigint") {
if (field.integerLength) {
return `BIGINT(${field.integerLength})`;
}
return "BIGINT";
}
if (dataType === "smallint") {
if (field.integerLength) {
return `SMALLINT(${field.integerLength})`;
}
return "SMALLINT";
}
if (dataType === "tinyint") {
if (field.integerLength) {
return `TINYINT(${field.integerLength})`;
}
return "TINYINT";
}
if (dataType.includes("double")) {
return "DOUBLE";
}
if (dataType.includes("float")) {
return "FLOAT";
}
if (dataType.includes("decimal") || dataType.includes("numeric")) {
if (field.integerLength && field.decimals) {
return `DECIMAL(${field.integerLength}, ${field.decimals})`;
}
return "DECIMAL";
}
if (dataType.includes("blob") || dataType.includes("binary")) {
return "BLOB";
}
if (dataType === "boolean" || dataType === "bool") {
return "TINYINT(1)";
}
if (dataType.includes("timestamp")) {
return "TIMESTAMP";
}
if (dataType.includes("datetime")) {
return "DATETIME";
}
if (dataType.includes("date") || dataType.includes("time")) {
return "DATETIME";
}
if (dataType.includes("varchar")) {
if (field.integerLength) {
return `VARCHAR(${field.integerLength})`;
}
return "VARCHAR(255)";
}
return "TEXT";
}
buildForeignKeyConstraint(field) {
const fk = field.foreignKey;
const constraintName = fk.foreignKeyName
? `CONSTRAINT ${this.quoteIdentifier(fk.foreignKeyName)} `
: "";
let constraint = `${constraintName}FOREIGN KEY (${this.quoteIdentifier(field.fieldName)}) REFERENCES ${this.quoteIdentifier(fk.destinationTableName)}(${this.quoteIdentifier(fk.destinationTableColumnName)})`;
if (fk.cascadeDelete) {
constraint += " ON DELETE CASCADE";
}
if (fk.cascadeUpdate) {
constraint += " ON UPDATE CASCADE";
}
return constraint;
}
async syncIndexes(table) {
if (!table.indexes || table.indexes.length === 0) {
return;
}
const tableSchemaWhere = this.tableSchemaWhere(table.tableName);
const rows = await this.query(`SELECT INDEX_NAME AS name FROM information_schema.STATISTICS WHERE ${tableSchemaWhere.where} AND TABLE_NAME = ? AND INDEX_NAME <> 'PRIMARY' GROUP BY INDEX_NAME ORDER BY INDEX_NAME`, tableSchemaWhere.values);
const existingIndexes = rows.map((row) => row.name);
for (const indexName of existingIndexes) {
const stillExists = table.indexes.some((index) => index.indexName === indexName);
if (!stillExists) {
console.log(`Dropping index: ${indexName}`);
await this.run(`DROP INDEX ${this.quoteIdentifier(indexName)} ON ${this.quoteIdentifier(table.tableName)}`);
}
}
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((field) => this.quoteIdentifier(field))
.join(", ");
await this.run(`CREATE INDEX ${this.quoteIdentifier(index.indexName)} ON ${this.quoteIdentifier(table.tableName)} (${fields})`);
}
}
}
close() { }
}
export { MariaDBSchemaManager };
-7
View File
@@ -1,7 +0,0 @@
import type { BUN_MARIADB_DatabaseSchemaType, BunSQLiteConfig } from "../../types";
type Params = {
dbSchema: BUN_MARIADB_DatabaseSchemaType;
config: BunSQLiteConfig;
};
export default function dbSchemaToType({ config, dbSchema, }: Params): string[] | undefined;
export {};
-44
View File
@@ -1,44 +0,0 @@
import _ from "lodash";
import generateTypeDefinition from "./db-generate-type-defs";
export default function dbSchemaToType({ config, dbSchema, }) {
let datasquirelSchema = dbSchema;
if (!datasquirelSchema)
return;
let tableNames = `export const BunSQLiteTables = [\n${datasquirelSchema.tables
.map((tbl) => ` "${tbl.tableName}",`)
.join("\n")}\n] as const`;
const dbTablesSchemas = datasquirelSchema.tables;
const defDbName = config.db_name
?.toUpperCase()
.replace(/[^a-zA-Z0-9]/g, "_");
const defNames = [];
const schemas = dbTablesSchemas
.map((table) => {
let final_table = _.cloneDeep(table);
if (final_table.parentTableName) {
const parent_table = dbTablesSchemas.find((t) => t.tableName === final_table.parentTableName);
if (parent_table) {
final_table = _.merge(parent_table, {
tableName: final_table.tableName,
tableDescription: final_table.tableDescription,
});
}
}
const defObj = generateTypeDefinition({
paradigm: "TypeScript",
table: final_table,
typeDefName: `BUN_MARIADB_${defDbName}_${final_table.tableName.toUpperCase()}`,
allValuesOptional: true,
addExport: true,
});
if (defObj.tdName?.match(/./)) {
defNames.push(defObj.tdName);
}
return defObj.typeDefinition;
})
.filter((schm) => typeof schm == "string");
const allTd = defNames?.[0]
? `export type BUN_MARIADB_${defDbName}_ALL_TYPEDEFS = ${defNames.join(` & `)}`
: ``;
return [tableNames, ...schemas, allTd];
}
-17
View File
@@ -1,17 +0,0 @@
import type { APIResponseObject, ServerQueryParam } from "../../types";
type Params<Schema extends {
[k: string]: any;
} = {
[k: string]: any;
}, Table extends string = string> = {
query?: ServerQueryParam<Schema>;
table: Table;
count?: boolean;
targetId?: number | string;
};
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>>;
export {};
-58
View File
@@ -1,58 +0,0 @@
import mysql from "mysql";
import DbClient from ".";
import _ from "lodash";
import sqlGenerator from "../../utils/sql-generator";
export default async function DbSelect({ table, query, count, targetId, }) {
let sqlObj = null;
try {
let finalQuery = query || {};
if (targetId) {
finalQuery = _.merge(finalQuery, {
query: {
id: {
value: String(targetId),
},
},
});
}
sqlObj = sqlGenerator({
tableName: table,
genObject: finalQuery,
});
let sql = mysql.format(sqlObj.string, sqlObj.values);
const res = DbClient.query(sql);
const batchRes = res.all();
let resp = {
success: Boolean(batchRes[0]),
payload: batchRes,
singleRes: batchRes[0],
debug: {
sqlObj,
sql,
},
};
if (count) {
let count_sql_object = sqlGenerator({
tableName: table,
genObject: finalQuery,
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);
resp["debug"]["count_sql"] = count_sql;
}
return resp;
}
catch (error) {
return {
success: false,
error: error.message,
debug: {
sqlObj,
},
};
}
}
-11
View File
@@ -1,11 +0,0 @@
import type { APIResponseObject, SQLInsertGenValueType } from "../../types";
type Params = {
sql: string;
values?: SQLInsertGenValueType[];
};
export default function DbSQL<T extends {
[k: string]: any;
} = {
[k: string]: any;
}>({ sql, values }: Params): Promise<APIResponseObject<T>>;
export {};
-34
View File
@@ -1,34 +0,0 @@
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,
},
};
}
catch (error) {
return {
success: false,
error: error.message,
};
}
}
-17
View File
@@ -1,17 +0,0 @@
import type { APIResponseObject, ServerQueryParam } from "../../types";
type Params<Schema extends {
[k: string]: any;
} = {
[k: string]: any;
}, Table extends string = string> = {
table: Table;
data: Schema;
query?: ServerQueryParam<Schema>;
targetId?: number | string;
};
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>;
export {};
-74
View File
@@ -1,74 +0,0 @@
import DbClient from ".";
import _ from "lodash";
import sqlGenerator from "../../utils/sql-generator";
export default async function DbUpdate({ table, data, query, targetId, }) {
let sqlObj = { string: "", values: [] };
try {
let finalQuery = query || {};
if (targetId) {
finalQuery = _.merge(finalQuery, {
query: {
id: {
value: String(targetId),
},
},
});
}
const sqlQueryObj = sqlGenerator({
tableName: table,
genObject: finalQuery,
});
let values = [];
const whereClause = sqlQueryObj.string.match(/WHERE .*/)?.[0];
if (whereClause) {
let sql = `UPDATE ${table} SET`;
const finalData = {
updated_at: Date.now(),
...data,
};
const keys = Object.keys(finalData);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (!key)
continue;
const isLast = i == keys.length - 1;
sql += ` ${key}=?`;
const value = finalData[key];
values.push(value || null);
if (!isLast) {
sql += `,`;
}
}
sql += ` ${whereClause}`;
values = [...values, ...sqlQueryObj.values];
sqlObj.string = sql;
sqlObj.values = values;
const res = DbClient.run(sql, values);
return {
success: Boolean(res.changes),
postInsertReturn: {
affectedRows: res.changes,
insertId: Number(res.lastInsertRowid),
},
debug: {
sqlObj,
},
};
}
else {
return {
success: false,
msg: `No WHERE clause`,
};
}
}
catch (error) {
return {
success: false,
error: error.message,
debug: {
sqlObj,
},
};
}
}
-8
View File
@@ -1,8 +0,0 @@
import type { BUN_MARIADB_DatabaseSchemaType, BunSQLiteConfig } from "../../types";
type Params = {
dbSchema: BUN_MARIADB_DatabaseSchemaType;
dst_file: string;
config: BunSQLiteConfig;
};
export default function dbSchemaToTypeDef({ dbSchema, dst_file, config, }: Params): void;
export {};
-18
View File
@@ -1,18 +0,0 @@
import path from "node:path";
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
import dbSchemaToType from "./db-schema-to-typedef";
export default function dbSchemaToTypeDef({ dbSchema, dst_file, config, }) {
try {
if (!dbSchema)
throw new Error("No schema found");
const definitions = dbSchemaToType({ dbSchema, config });
const ourfileDir = path.dirname(dst_file);
if (!existsSync(ourfileDir)) {
mkdirSync(ourfileDir, { recursive: true });
}
writeFileSync(dst_file, definitions?.join("\n\n") || "", "utf-8");
}
catch (error) {
console.log(`Schema to Typedef Error =>`, error.message);
}
}
-2
View File
@@ -1,2 +0,0 @@
import type { BUN_MARIADB_DatabaseSchemaType } from "../../types";
export declare const DbSchema: BUN_MARIADB_DatabaseSchemaType;
-5
View File
@@ -1,5 +0,0 @@
import _ from "lodash";
export const DbSchema = {
dbName: "travis-ai",
tables: [],
};