First Commit
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import { Command } from "commander";
|
||||
import init from "../functions/init";
|
||||
import path from "path";
|
||||
import grabDBDir from "../utils/grab-db-dir";
|
||||
import fs from "fs";
|
||||
import grabDBBackupFileName from "../utils/grab-db-backup-file-name";
|
||||
import chalk from "chalk";
|
||||
import trimBackups from "../utils/trim-backups";
|
||||
|
||||
export default function () {
|
||||
return new Command("backup")
|
||||
.description("Backup Database")
|
||||
.action(async (opts) => {
|
||||
console.log(`Backing up database ...`);
|
||||
|
||||
const { config } = await init();
|
||||
|
||||
const { backup_dir, db_file_path } = grabDBDir({ config });
|
||||
|
||||
const new_db_file_name = grabDBBackupFileName({ config });
|
||||
|
||||
fs.cpSync(db_file_path, path.join(backup_dir, new_db_file_name));
|
||||
|
||||
trimBackups({ config });
|
||||
|
||||
console.log(`${chalk.bold(chalk.green(`DB Backup Success!`))}`);
|
||||
process.exit();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env bun
|
||||
|
||||
import { program } from "commander";
|
||||
import schema from "./schema";
|
||||
import typedef from "./typedef";
|
||||
import backup from "./backup";
|
||||
import restore from "./restore";
|
||||
|
||||
/**
|
||||
* # Declare Global Variables
|
||||
*/
|
||||
declare global {}
|
||||
|
||||
/**
|
||||
* # Describe Program
|
||||
*/
|
||||
program
|
||||
.name(`bun-sqlite`)
|
||||
.description(`SQLite manager for Bun`)
|
||||
.version(`1.0.0`);
|
||||
|
||||
/**
|
||||
* # Declare Commands
|
||||
*/
|
||||
program.addCommand(schema());
|
||||
program.addCommand(typedef());
|
||||
program.addCommand(backup());
|
||||
program.addCommand(restore());
|
||||
|
||||
/**
|
||||
* # Handle Unavailable Commands
|
||||
*/
|
||||
program.on("command:*", () => {
|
||||
console.error(
|
||||
"Invalid command: %s\nSee --help for a list of available commands.",
|
||||
program.args.join(" "),
|
||||
);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
/**
|
||||
* # Parse Arguments
|
||||
*/
|
||||
program.parse(process.argv);
|
||||
@@ -0,0 +1,56 @@
|
||||
import { Command } from "commander";
|
||||
import init from "../functions/init";
|
||||
import grabDBDir from "../utils/grab-db-dir";
|
||||
import fs from "fs";
|
||||
import chalk from "chalk";
|
||||
import grabSortedBackups from "../utils/grab-sorted-backups";
|
||||
import { select } from "@inquirer/prompts";
|
||||
import grabBackupData from "../utils/grab-backup-data";
|
||||
import path from "path";
|
||||
|
||||
export default function () {
|
||||
return new Command("restore")
|
||||
.description("Restore Database")
|
||||
.action(async (opts) => {
|
||||
console.log(`Restoring up database ...`);
|
||||
|
||||
const { config } = await init();
|
||||
|
||||
const { backup_dir, db_file_path } = grabDBDir({ config });
|
||||
|
||||
const backups = grabSortedBackups({ config });
|
||||
|
||||
if (!backups?.[0]) {
|
||||
console.error(
|
||||
`No Backups to restore. Use the \`backup\` command to create a backup`,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
const selected_backup = await select({
|
||||
message: "Select a backup:",
|
||||
choices: backups.map((b, i) => {
|
||||
const { backup_date } = grabBackupData({
|
||||
backup_name: b,
|
||||
});
|
||||
return {
|
||||
name: `Backup #${i + 1}: ${backup_date.toDateString()} ${backup_date.getHours()}:${backup_date.getMinutes()}:${backup_date.getSeconds().toString().padStart(2, "0")}`,
|
||||
value: b,
|
||||
};
|
||||
}),
|
||||
});
|
||||
|
||||
fs.cpSync(path.join(backup_dir, selected_backup), db_file_path);
|
||||
|
||||
console.log(
|
||||
`${chalk.bold(chalk.green(`DB Restore Success!`))}`,
|
||||
);
|
||||
|
||||
process.exit();
|
||||
} catch (error: any) {
|
||||
console.error(`Backup Restore ERROR => ${error.message}`);
|
||||
process.exit();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import { Command } from "commander";
|
||||
import { SQLiteSchemaManager } from "../lib/sqlite/db-schema-manager";
|
||||
import init from "../functions/init";
|
||||
import grabDirNames from "../data/grab-dir-names";
|
||||
import path from "path";
|
||||
import dbSchemaToTypeDef from "../lib/sqlite/schema-to-typedef";
|
||||
import _ from "lodash";
|
||||
import appendDefaultFieldsToDbSchema from "../utils/append-default-fields-to-db-schema";
|
||||
import chalk from "chalk";
|
||||
|
||||
export default function () {
|
||||
return new Command("schema")
|
||||
.description("Build DB From Schema")
|
||||
.option(
|
||||
"-v, --vector",
|
||||
"Recreate Vector Tables. This will drop and rebuild all vector tables",
|
||||
)
|
||||
.option("-t, --typedef", "Generate typescript type definitions")
|
||||
.action(async (opts) => {
|
||||
console.log(`Starting process ...`);
|
||||
|
||||
const { config, dbSchema } = await init();
|
||||
const { ROOT_DIR } = grabDirNames();
|
||||
|
||||
const isVector = Boolean(opts.vector || opts.v);
|
||||
const isTypeDef = Boolean(opts.typedef || opts.t);
|
||||
|
||||
const finaldbSchema = appendDefaultFieldsToDbSchema({ dbSchema });
|
||||
|
||||
const manager = new SQLiteSchemaManager({
|
||||
schema: finaldbSchema,
|
||||
recreate_vector_table: isVector,
|
||||
});
|
||||
|
||||
await manager.syncSchema();
|
||||
manager.close();
|
||||
|
||||
if (isTypeDef && config.typedef_file_path) {
|
||||
const out_file = path.resolve(
|
||||
ROOT_DIR,
|
||||
config.typedef_file_path,
|
||||
);
|
||||
|
||||
dbSchemaToTypeDef({
|
||||
dbSchema: finaldbSchema,
|
||||
dst_file: out_file,
|
||||
});
|
||||
}
|
||||
|
||||
console.log(
|
||||
`${chalk.bold(chalk.green(`DB Schema setup success!`))}`,
|
||||
);
|
||||
process.exit();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import { Command } from "commander";
|
||||
import init from "../functions/init";
|
||||
import dbSchemaToTypeDef from "../lib/sqlite/schema-to-typedef";
|
||||
import path from "path";
|
||||
import grabDirNames from "../data/grab-dir-names";
|
||||
import appendDefaultFieldsToDbSchema from "../utils/append-default-fields-to-db-schema";
|
||||
import chalk from "chalk";
|
||||
|
||||
export default function () {
|
||||
return new Command("typedef")
|
||||
.description("Build DB From Schema")
|
||||
.action(async (opts) => {
|
||||
console.log(`Creating Type Definition From DB Schema ...`);
|
||||
|
||||
const { config, dbSchema } = await init();
|
||||
const { ROOT_DIR } = grabDirNames();
|
||||
|
||||
const finaldbSchema = appendDefaultFieldsToDbSchema({ dbSchema });
|
||||
|
||||
if (config.typedef_file_path) {
|
||||
const out_file = path.resolve(
|
||||
ROOT_DIR,
|
||||
config.typedef_file_path,
|
||||
);
|
||||
dbSchemaToTypeDef({
|
||||
dbSchema: finaldbSchema,
|
||||
dst_file: out_file,
|
||||
});
|
||||
} else {
|
||||
console.error(``);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(`${chalk.bold(chalk.green(`Typedef gen success!`))}`);
|
||||
|
||||
process.exit();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export const AppData = {
|
||||
ConfigFileName: "bun-sqlite.config.ts",
|
||||
MaxBackups: 10,
|
||||
DefaultBackupDirName: ".backups",
|
||||
} as const;
|
||||
@@ -0,0 +1,9 @@
|
||||
import path from "path";
|
||||
|
||||
export default function grabDirNames() {
|
||||
const ROOT_DIR = process.cwd();
|
||||
|
||||
return {
|
||||
ROOT_DIR,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import { AppData } from "../data/app-data";
|
||||
import grabDirNames from "../data/grab-dir-names";
|
||||
import type {
|
||||
BunSQLiteConfig,
|
||||
BunSQLiteConfigReturn,
|
||||
BUN_SQLITE_DatabaseSchemaType,
|
||||
} from "../types";
|
||||
|
||||
export default async function init(): Promise<BunSQLiteConfigReturn> {
|
||||
try {
|
||||
const { ROOT_DIR } = grabDirNames();
|
||||
const { ConfigFileName } = AppData;
|
||||
|
||||
const ConfigFilePath = path.join(ROOT_DIR, ConfigFileName);
|
||||
|
||||
if (!fs.existsSync(ConfigFilePath)) {
|
||||
console.log("ConfigFilePath", ConfigFilePath);
|
||||
|
||||
console.error(
|
||||
`Please create a \`${ConfigFileName}\` file at the root of your project.`,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const ConfigImport = await import(ConfigFilePath);
|
||||
const Config = ConfigImport["default"] as BunSQLiteConfig;
|
||||
|
||||
if (!Config.db_name) {
|
||||
console.error(`\`db_name\` is required in your config`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (!Config.db_schema_file_name) {
|
||||
console.error(`\`db_schema_file_name\` is required in your config`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
let db_dir = ROOT_DIR;
|
||||
|
||||
if (Config.db_dir) {
|
||||
db_dir = path.resolve(ROOT_DIR, Config.db_dir);
|
||||
|
||||
if (!fs.existsSync(Config.db_dir)) {
|
||||
fs.mkdirSync(Config.db_dir, { recursive: true });
|
||||
}
|
||||
}
|
||||
|
||||
const DBSchemaFilePath = path.join(db_dir, Config.db_schema_file_name);
|
||||
const DbSchemaImport = await import(DBSchemaFilePath);
|
||||
const DbSchema = DbSchemaImport[
|
||||
"default"
|
||||
] as BUN_SQLITE_DatabaseSchemaType;
|
||||
|
||||
const backup_dir =
|
||||
Config.db_backup_dir || AppData["DefaultBackupDirName"];
|
||||
|
||||
const BackupDir = path.resolve(db_dir, backup_dir);
|
||||
if (!fs.existsSync(BackupDir)) {
|
||||
fs.mkdirSync(BackupDir, { recursive: true });
|
||||
}
|
||||
|
||||
return { config: Config, dbSchema: DbSchema };
|
||||
} catch (error: any) {
|
||||
console.error(`Initialization ERROR => ` + error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import DbDelete from "./lib/sqlite/db-delete";
|
||||
import DbInsert from "./lib/sqlite/db-insert";
|
||||
import DbSelect from "./lib/sqlite/db-select";
|
||||
import DbSQL from "./lib/sqlite/db-sql";
|
||||
import DbUpdate from "./lib/sqlite/db-update";
|
||||
|
||||
const NodeSQLite = {
|
||||
select: DbSelect,
|
||||
insert: DbInsert,
|
||||
update: DbUpdate,
|
||||
delete: DbDelete,
|
||||
sql: DbSQL,
|
||||
} as const;
|
||||
|
||||
export default NodeSQLite;
|
||||
@@ -0,0 +1,74 @@
|
||||
import DbClient from ".";
|
||||
import _ from "lodash";
|
||||
import type { APIResponseObject, ServerQueryParam } from "../../types";
|
||||
import sqlGenerator from "../../utils/sql-generator";
|
||||
|
||||
type Params<
|
||||
Schema extends { [k: string]: any } = { [k: string]: any },
|
||||
Table extends string = string,
|
||||
> = {
|
||||
table: Table;
|
||||
query?: ServerQueryParam<Schema>;
|
||||
targetId?: number | string;
|
||||
};
|
||||
|
||||
export default async function DbDelete<
|
||||
Schema extends { [k: string]: any } = { [k: string]: any },
|
||||
Table extends string = string,
|
||||
>({
|
||||
table,
|
||||
query,
|
||||
targetId,
|
||||
}: Params<Schema, Table>): Promise<APIResponseObject> {
|
||||
try {
|
||||
let finalQuery = query || {};
|
||||
|
||||
if (targetId) {
|
||||
finalQuery = _.merge<ServerQueryParam<any>, ServerQueryParam<any>>(
|
||||
finalQuery,
|
||||
{
|
||||
query: {
|
||||
id: {
|
||||
value: String(targetId),
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
const sqlQueryObj = sqlGenerator({
|
||||
tableName: table,
|
||||
genObject: finalQuery,
|
||||
});
|
||||
|
||||
const whereClause = sqlQueryObj.string.match(/WHERE .*/)?.[0];
|
||||
|
||||
if (whereClause) {
|
||||
let sql = `DELETE FROM ${table} ${whereClause}`;
|
||||
|
||||
const res = DbClient.run(sql, sqlQueryObj.values);
|
||||
|
||||
return {
|
||||
success: Boolean(res.changes),
|
||||
postInsertReturn: {
|
||||
affectedRows: res.changes,
|
||||
insertId: Number(res.lastInsertRowid),
|
||||
},
|
||||
debug: {
|
||||
sql,
|
||||
values: sqlQueryObj.values,
|
||||
},
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
success: false,
|
||||
msg: `No WHERE clause`,
|
||||
};
|
||||
}
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
import type {
|
||||
BUN_SQLITE_FieldSchemaType,
|
||||
BUN_SQLITE_TableSchemaType,
|
||||
} from "../../types";
|
||||
|
||||
type Param = {
|
||||
paradigm: "JavaScript" | "TypeScript" | undefined;
|
||||
table: BUN_SQLITE_TableSchemaType;
|
||||
query?: any;
|
||||
typeDefName?: string;
|
||||
allValuesOptional?: boolean;
|
||||
addExport?: boolean;
|
||||
dbName?: string;
|
||||
};
|
||||
|
||||
export default function generateTypeDefinition({
|
||||
paradigm,
|
||||
table,
|
||||
query,
|
||||
typeDefName,
|
||||
allValuesOptional,
|
||||
addExport,
|
||||
dbName,
|
||||
}: Param) {
|
||||
let typeDefinition: string | null = ``;
|
||||
let tdName: string | null = ``;
|
||||
|
||||
try {
|
||||
tdName = typeDefName
|
||||
? typeDefName
|
||||
: dbName
|
||||
? `BUN_SQLITE_${dbName}_${table.tableName}`.toUpperCase()
|
||||
: `BUN_SQLITE_${query.single}_${query.single_table}`.toUpperCase();
|
||||
|
||||
const fields = table.fields;
|
||||
|
||||
function typeMap(schemaType: BUN_SQLITE_FieldSchemaType) {
|
||||
if (schemaType.options && schemaType.options.length > 0) {
|
||||
return schemaType.options
|
||||
.map((opt) =>
|
||||
schemaType.dataType?.match(/int/i) ||
|
||||
typeof opt == "number"
|
||||
? `${opt}`
|
||||
: `"${opt}"`,
|
||||
)
|
||||
.join(" | ");
|
||||
}
|
||||
|
||||
if (schemaType.dataType?.match(/int|double|decimal/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: any) {
|
||||
console.log(error.message);
|
||||
typeDefinition = null;
|
||||
}
|
||||
|
||||
return { typeDefinition, tdName };
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import DbClient from ".";
|
||||
import type { APIResponseObject } from "../../types";
|
||||
import sqlInsertGenerator from "../../utils/sql-insert-generator";
|
||||
|
||||
type Params<
|
||||
Schema extends { [k: string]: any } = { [k: string]: any },
|
||||
Table extends string = string,
|
||||
> = {
|
||||
table: Table;
|
||||
data: Schema[];
|
||||
};
|
||||
|
||||
export default async function DbInsert<
|
||||
Schema extends { [k: string]: any } = { [k: string]: any },
|
||||
Table extends string = string,
|
||||
>({ table, data }: Params<Schema, Table>): Promise<APIResponseObject> {
|
||||
try {
|
||||
const finalData: { [k: string]: any }[] = data.map((d) => ({
|
||||
...d,
|
||||
created_at: Date.now(),
|
||||
updated_at: Date.now(),
|
||||
}));
|
||||
|
||||
const sqlObj = sqlInsertGenerator({
|
||||
tableName: table,
|
||||
data: finalData as any[],
|
||||
});
|
||||
|
||||
const res = DbClient.run(sqlObj?.query || "", sqlObj?.values || []);
|
||||
|
||||
return {
|
||||
success: Boolean(Number(res.lastInsertRowid)),
|
||||
postInsertReturn: {
|
||||
affectedRows: res.changes,
|
||||
insertId: Number(res.lastInsertRowid),
|
||||
},
|
||||
debug: {
|
||||
sqlObj,
|
||||
},
|
||||
};
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,64 @@
|
||||
import _ from "lodash";
|
||||
import type { BUN_SQLITE_DatabaseSchemaType } from "../../types";
|
||||
import generateTypeDefinition from "./db-generate-type-defs";
|
||||
|
||||
type Params = {
|
||||
dbSchema?: BUN_SQLITE_DatabaseSchemaType;
|
||||
};
|
||||
|
||||
export default function dbSchemaToType(params?: Params): string[] | undefined {
|
||||
let datasquirelSchema = params?.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 = datasquirelSchema.dbName
|
||||
?.toUpperCase()
|
||||
.replace(/ |\-/g, "_");
|
||||
|
||||
const defNames: string[] = [];
|
||||
|
||||
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_SQLITE_${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_SQLITE_${defDbName}_ALL_TYPEDEFS = ${defNames.join(` & `)}`
|
||||
: ``;
|
||||
|
||||
return [tableNames, ...schemas, allTd];
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
import mysql from "mysql";
|
||||
import DbClient from ".";
|
||||
import _ from "lodash";
|
||||
import type { APIResponseObject, ServerQueryParam } from "../../types";
|
||||
import sqlGenerator from "../../utils/sql-generator";
|
||||
|
||||
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 async function DbSelect<
|
||||
Schema extends { [k: string]: any } = { [k: string]: any },
|
||||
Table extends string = string,
|
||||
>({
|
||||
table,
|
||||
query,
|
||||
count,
|
||||
targetId,
|
||||
}: Params<Schema, Table>): Promise<APIResponseObject<Schema>> {
|
||||
try {
|
||||
let finalQuery = query || {};
|
||||
|
||||
if (targetId) {
|
||||
finalQuery = _.merge<ServerQueryParam<any>, ServerQueryParam<any>>(
|
||||
finalQuery,
|
||||
{
|
||||
query: {
|
||||
id: {
|
||||
value: String(targetId),
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
const sqlObj = sqlGenerator({
|
||||
tableName: table,
|
||||
genObject: finalQuery,
|
||||
count,
|
||||
});
|
||||
|
||||
const sql = mysql.format(sqlObj.string, sqlObj.values);
|
||||
|
||||
const res = DbClient.query<Schema, Schema[]>(sql);
|
||||
const batchRes = res.all();
|
||||
|
||||
let resp: APIResponseObject<Schema> = {
|
||||
success: Boolean(batchRes[0]),
|
||||
payload: batchRes,
|
||||
singleRes: batchRes[0],
|
||||
debug: {
|
||||
sqlObj,
|
||||
sql,
|
||||
},
|
||||
};
|
||||
|
||||
if (count) {
|
||||
const count_val = count ? batchRes[0]?.["COUNT(*)"] : undefined;
|
||||
resp["count"] = Number(count_val);
|
||||
|
||||
delete resp.payload;
|
||||
delete resp.singleRes;
|
||||
}
|
||||
|
||||
return resp;
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import DbClient from ".";
|
||||
import _ from "lodash";
|
||||
import type { APIResponseObject } from "../../types";
|
||||
|
||||
type Params = {
|
||||
sql: string;
|
||||
values?: (string | number)[];
|
||||
};
|
||||
|
||||
export default async function DbSQL<
|
||||
T extends { [k: string]: any } = { [k: string]: any },
|
||||
>({ sql, values }: Params): Promise<APIResponseObject<T>> {
|
||||
try {
|
||||
const res = sql.match(/^select/i)
|
||||
? DbClient.query(sql).all(...(values || []))
|
||||
: DbClient.run(sql, values || []);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
payload: Array.isArray(res) ? (res as T[]) : undefined,
|
||||
singleRes: Array.isArray(res) ? (res as T[])?.[0] : undefined,
|
||||
postInsertReturn: Array.isArray(res)
|
||||
? undefined
|
||||
: {
|
||||
affectedRows: res.changes,
|
||||
insertId: Number(res.lastInsertRowid),
|
||||
},
|
||||
debug: {
|
||||
sqlObj: {
|
||||
sql,
|
||||
values,
|
||||
},
|
||||
sql,
|
||||
},
|
||||
};
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
import DbClient from ".";
|
||||
import _ from "lodash";
|
||||
import type { APIResponseObject, ServerQueryParam } from "../../types";
|
||||
import sqlGenerator from "../../utils/sql-generator";
|
||||
|
||||
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 async function DbUpdate<
|
||||
Schema extends { [k: string]: any } = { [k: string]: any },
|
||||
Table extends string = string,
|
||||
>({
|
||||
table,
|
||||
data,
|
||||
query,
|
||||
targetId,
|
||||
}: Params<Schema, Table>): Promise<APIResponseObject> {
|
||||
try {
|
||||
let finalQuery = query || {};
|
||||
|
||||
if (targetId) {
|
||||
finalQuery = _.merge<ServerQueryParam<any>, ServerQueryParam<any>>(
|
||||
finalQuery,
|
||||
{
|
||||
query: {
|
||||
id: {
|
||||
value: String(targetId),
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
const sqlQueryObj = sqlGenerator({
|
||||
tableName: table,
|
||||
genObject: finalQuery,
|
||||
});
|
||||
|
||||
let values: (string | number)[] = [];
|
||||
|
||||
const whereClause = sqlQueryObj.string.match(/WHERE .*/)?.[0];
|
||||
|
||||
if (whereClause) {
|
||||
let sql = `UPDATE ${table} SET`;
|
||||
|
||||
const finalData: { [k: string]: any } = {
|
||||
...data,
|
||||
updated_at: Date.now(),
|
||||
};
|
||||
|
||||
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}=?`;
|
||||
values.push(
|
||||
String(finalData[key as keyof { [k: string]: any }]),
|
||||
);
|
||||
|
||||
if (!isLast) {
|
||||
sql += `,`;
|
||||
}
|
||||
}
|
||||
|
||||
sql += ` ${whereClause}`;
|
||||
values = [...values, ...sqlQueryObj.values];
|
||||
|
||||
const res = DbClient.run(sql, values);
|
||||
|
||||
return {
|
||||
success: Boolean(res.changes),
|
||||
postInsertReturn: {
|
||||
affectedRows: res.changes,
|
||||
insertId: Number(res.lastInsertRowid),
|
||||
},
|
||||
debug: {
|
||||
sql,
|
||||
values,
|
||||
},
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
success: false,
|
||||
msg: `No WHERE clause`,
|
||||
};
|
||||
}
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import Database from "better-sqlite3";
|
||||
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, { fileMustExist: false });
|
||||
|
||||
sqliteVec.load(DbClient);
|
||||
|
||||
export default DbClient;
|
||||
@@ -0,0 +1,27 @@
|
||||
import path from "node:path";
|
||||
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
||||
import type { BUN_SQLITE_DatabaseSchemaType } from "../../types";
|
||||
import dbSchemaToType from "./db-schema-to-typedef";
|
||||
|
||||
type Params = {
|
||||
dbSchema: BUN_SQLITE_DatabaseSchemaType;
|
||||
dst_file: string;
|
||||
};
|
||||
|
||||
export default function dbSchemaToTypeDef({ dbSchema, dst_file }: Params) {
|
||||
try {
|
||||
if (!dbSchema) throw new Error("No schema found");
|
||||
|
||||
const definitions = dbSchemaToType({ dbSchema });
|
||||
|
||||
const ourfileDir = path.dirname(dst_file);
|
||||
|
||||
if (!existsSync(ourfileDir)) {
|
||||
mkdirSync(ourfileDir, { recursive: true });
|
||||
}
|
||||
|
||||
writeFileSync(dst_file, definitions?.join("\n\n") || "", "utf-8");
|
||||
} catch (error: any) {
|
||||
console.log(`Schema to Typedef Error =>`, error.message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import _ from "lodash";
|
||||
import type { BUN_SQLITE_DatabaseSchemaType } from "../../types";
|
||||
|
||||
export const DbSchema: BUN_SQLITE_DatabaseSchemaType = {
|
||||
dbName: "travis-ai",
|
||||
tables: [],
|
||||
};
|
||||
+1193
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,20 @@
|
||||
import _ from "lodash";
|
||||
import { DefaultFields, type BUN_SQLITE_DatabaseSchemaType } from "../types";
|
||||
|
||||
type Params = {
|
||||
dbSchema: BUN_SQLITE_DatabaseSchemaType;
|
||||
};
|
||||
|
||||
export default function ({ dbSchema }: Params): BUN_SQLITE_DatabaseSchemaType {
|
||||
const finaldbSchema = _.cloneDeep(dbSchema);
|
||||
finaldbSchema.tables = finaldbSchema.tables.map((t) => {
|
||||
const newTable = _.cloneDeep(t);
|
||||
newTable.fields = newTable.fields.filter(
|
||||
(f) => !f.fieldName?.match(/^(id|created_at|updated_at)$/),
|
||||
);
|
||||
newTable.fields.unshift(...DefaultFields);
|
||||
return newTable;
|
||||
});
|
||||
|
||||
return finaldbSchema;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
type Params = {
|
||||
backup_name: string;
|
||||
};
|
||||
|
||||
export default function grabBackupData({ backup_name }: Params) {
|
||||
const backup_parts = backup_name.split("-");
|
||||
const backup_date_timestamp = Number(backup_parts.pop());
|
||||
const origin_backup_name = backup_parts.join("-");
|
||||
|
||||
const backup_date = new Date(backup_date_timestamp);
|
||||
|
||||
return { backup_date, backup_date_timestamp, origin_backup_name };
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import type { BunSQLiteConfig } from "../types";
|
||||
|
||||
type Params = {
|
||||
config: BunSQLiteConfig;
|
||||
};
|
||||
|
||||
export default function grabDBBackupFileName({ config }: Params) {
|
||||
const new_db_file_name = `${config.db_name}-${Date.now()}`;
|
||||
|
||||
return new_db_file_name;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import path from "path";
|
||||
import grabDirNames from "../data/grab-dir-names";
|
||||
import type { BunSQLiteConfig } from "../types";
|
||||
import { AppData } from "../data/app-data";
|
||||
|
||||
type Params = {
|
||||
config: BunSQLiteConfig;
|
||||
};
|
||||
|
||||
export default function grabDBDir({ config }: Params) {
|
||||
const { ROOT_DIR } = grabDirNames();
|
||||
|
||||
let db_dir = ROOT_DIR;
|
||||
|
||||
if (config.db_dir) {
|
||||
db_dir = config.db_dir;
|
||||
}
|
||||
|
||||
const backup_dir_name =
|
||||
config.db_backup_dir || AppData["DefaultBackupDirName"];
|
||||
|
||||
const backup_dir = path.resolve(db_dir, backup_dir_name);
|
||||
const db_file_path = path.resolve(db_dir, config.db_name);
|
||||
|
||||
return { db_dir, backup_dir, db_file_path };
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import grabDBDir from "../utils/grab-db-dir";
|
||||
import fs from "fs";
|
||||
import type { BunSQLiteConfig } from "../types";
|
||||
|
||||
type Params = {
|
||||
config: BunSQLiteConfig;
|
||||
};
|
||||
|
||||
export default function grabSortedBackups({ config }: Params) {
|
||||
const { backup_dir } = grabDBDir({ config });
|
||||
|
||||
const backups = fs.readdirSync(backup_dir);
|
||||
|
||||
/**
|
||||
* Order Backups. Most recent first.
|
||||
*/
|
||||
const ordered_backups = backups.sort((a, b) => {
|
||||
const a_date = Number(a.split("-").pop());
|
||||
const b_date = Number(b.split("-").pop());
|
||||
|
||||
if (a_date > b_date) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
});
|
||||
|
||||
return ordered_backups;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { ServerQueryEqualities } from "../types";
|
||||
|
||||
export default function sqlEqualityParser(
|
||||
eq: (typeof ServerQueryEqualities)[number]
|
||||
): string {
|
||||
switch (eq) {
|
||||
case "EQUAL":
|
||||
return "=";
|
||||
case "LIKE":
|
||||
return "LIKE";
|
||||
case "NOT LIKE":
|
||||
return "NOT LIKE";
|
||||
case "NOT EQUAL":
|
||||
return "<>";
|
||||
case "IN":
|
||||
return "IN";
|
||||
case "NOT IN":
|
||||
return "NOT IN";
|
||||
case "BETWEEN":
|
||||
return "BETWEEN";
|
||||
case "NOT BETWEEN":
|
||||
return "NOT BETWEEN";
|
||||
case "IS NULL":
|
||||
return "IS NULL";
|
||||
case "IS NOT NULL":
|
||||
return "IS NOT NULL";
|
||||
case "EXISTS":
|
||||
return "EXISTS";
|
||||
case "NOT EXISTS":
|
||||
return "NOT EXISTS";
|
||||
case "GREATER THAN":
|
||||
return ">";
|
||||
case "GREATER THAN OR EQUAL":
|
||||
return ">=";
|
||||
case "LESS THAN":
|
||||
return "<";
|
||||
case "LESS THAN OR EQUAL":
|
||||
return "<=";
|
||||
default:
|
||||
return "=";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
import type { ServerQueryEqualities, ServerQueryObject } from "../types";
|
||||
import sqlEqualityParser from "./sql-equality-parser";
|
||||
|
||||
type Params = {
|
||||
fieldName: string;
|
||||
value?: string;
|
||||
equality?: (typeof ServerQueryEqualities)[number];
|
||||
queryObj: ServerQueryObject<
|
||||
{
|
||||
[key: string]: any;
|
||||
},
|
||||
string
|
||||
>;
|
||||
isValueFieldValue?: boolean;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
str?: string;
|
||||
param?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* # SQL Gen Operator Gen
|
||||
* @description Generates an SQL operator for node module `mysql` or `serverless-mysql`
|
||||
*/
|
||||
export default function sqlGenOperatorGen({
|
||||
fieldName,
|
||||
value,
|
||||
equality,
|
||||
queryObj,
|
||||
isValueFieldValue,
|
||||
}: Params): Return {
|
||||
if (queryObj.nullValue) {
|
||||
return { str: `${fieldName} IS NULL` };
|
||||
}
|
||||
|
||||
if (queryObj.notNullValue) {
|
||||
return { str: `${fieldName} IS NOT NULL` };
|
||||
}
|
||||
|
||||
if (value) {
|
||||
const finalValue = isValueFieldValue ? value : "?";
|
||||
const finalParams = isValueFieldValue ? undefined : value;
|
||||
|
||||
if (equality == "MATCH") {
|
||||
return {
|
||||
str: `MATCH(${fieldName}) AGAINST(${finalValue} IN NATURAL LANGUAGE MODE)`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else if (equality == "MATCH_BOOLEAN") {
|
||||
return {
|
||||
str: `MATCH(${fieldName}) AGAINST(${finalValue} IN BOOLEAN MODE)`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else if (equality == "LIKE_LOWER") {
|
||||
return {
|
||||
str: `LOWER(${fieldName}) LIKE LOWER(${finalValue})`,
|
||||
param: `%${finalParams}%`,
|
||||
};
|
||||
} else if (equality == "LIKE_LOWER_RAW") {
|
||||
return {
|
||||
str: `LOWER(${fieldName}) LIKE LOWER(${finalValue})`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else if (equality == "LIKE") {
|
||||
return {
|
||||
str: `${fieldName} LIKE ${finalValue}`,
|
||||
param: `%${finalParams}%`,
|
||||
};
|
||||
} else if (equality == "LIKE_RAW") {
|
||||
return {
|
||||
str: `${fieldName} LIKE ${finalValue}`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else if (equality == "NOT_LIKE_LOWER") {
|
||||
return {
|
||||
str: `LOWER(${fieldName}) NOT LIKE LOWER(${finalValue})`,
|
||||
param: `%${finalParams}%`,
|
||||
};
|
||||
} else if (equality == "NOT_LIKE_LOWER_RAW") {
|
||||
return {
|
||||
str: `LOWER(${fieldName}) NOT LIKE LOWER(${finalValue})`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else if (equality == "NOT LIKE") {
|
||||
return {
|
||||
str: `${fieldName} NOT LIKE ${finalValue}`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else if (equality == "NOT LIKE_RAW") {
|
||||
return {
|
||||
str: `${fieldName} NOT LIKE ${finalValue}`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else if (equality == "REGEXP") {
|
||||
return {
|
||||
str: `LOWER(${fieldName}) REGEXP LOWER(${finalValue})`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else if (equality == "FULLTEXT") {
|
||||
return {
|
||||
str: `MATCH(${fieldName}) AGAINST(${finalValue} IN BOOLEAN MODE)`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else if (equality == "NOT EQUAL") {
|
||||
return {
|
||||
str: `${fieldName} != ${finalValue}`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else if (equality) {
|
||||
return {
|
||||
str: `${fieldName} ${sqlEqualityParser(
|
||||
equality,
|
||||
)} ${finalValue}`,
|
||||
param: finalParams,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
str: `${fieldName} = ${finalValue}`,
|
||||
param: finalParams,
|
||||
};
|
||||
}
|
||||
} else {
|
||||
if (equality == "IS NULL") {
|
||||
return { str: `${fieldName} IS NULL` };
|
||||
} else if (equality == "IS NOT NULL") {
|
||||
return { str: `${fieldName} IS NOT NULL` };
|
||||
} else if (equality) {
|
||||
return {
|
||||
str: `${fieldName} ${sqlEqualityParser(equality)} ?`,
|
||||
param: value,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
str: `${fieldName} = ?`,
|
||||
param: value,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,76 @@
|
||||
import type { SQLInsertGenParams, SQLInsertGenReturn } from "../types";
|
||||
|
||||
/**
|
||||
* # SQL Insert Generator
|
||||
*/
|
||||
export default function sqlInsertGenerator({
|
||||
tableName,
|
||||
data,
|
||||
dbFullName,
|
||||
}: SQLInsertGenParams): SQLInsertGenReturn | undefined {
|
||||
const finalDbName = dbFullName ? `${dbFullName}.` : "";
|
||||
|
||||
try {
|
||||
if (Array.isArray(data) && data?.[0]) {
|
||||
let insertKeys: string[] = [];
|
||||
|
||||
data.forEach((dt) => {
|
||||
const kys = Object.keys(dt);
|
||||
kys.forEach((ky) => {
|
||||
if (!insertKeys.includes(ky)) {
|
||||
insertKeys.push(ky);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
let queryBatches: string[] = [];
|
||||
let queryValues: (string | number)[] = [];
|
||||
|
||||
data.forEach((item) => {
|
||||
queryBatches.push(
|
||||
`(${insertKeys
|
||||
.map((ky) => {
|
||||
const value = item[ky];
|
||||
|
||||
const finalValue =
|
||||
typeof value == "string" ||
|
||||
typeof value == "number"
|
||||
? value
|
||||
: value
|
||||
? String(value().value)
|
||||
: null;
|
||||
|
||||
if (!finalValue) {
|
||||
queryValues.push("");
|
||||
return "?";
|
||||
}
|
||||
|
||||
queryValues.push(finalValue);
|
||||
|
||||
const placeholder =
|
||||
typeof value == "function"
|
||||
? value().placeholder
|
||||
: "?";
|
||||
|
||||
return placeholder;
|
||||
})
|
||||
.filter((k) => Boolean(k))
|
||||
.join(",")})`,
|
||||
);
|
||||
});
|
||||
let query = `INSERT INTO ${finalDbName}${tableName} (${insertKeys.join(
|
||||
",",
|
||||
)}) VALUES ${queryBatches.join(",")}`;
|
||||
|
||||
return {
|
||||
query: query,
|
||||
values: queryValues,
|
||||
};
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
console.log(`SQL insert gen ERROR: ${error.message}`);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import grabDBDir from "../utils/grab-db-dir";
|
||||
import fs from "fs";
|
||||
import type { BunSQLiteConfig } from "../types";
|
||||
import grabSortedBackups from "./grab-sorted-backups";
|
||||
import { AppData } from "../data/app-data";
|
||||
import path from "path";
|
||||
|
||||
type Params = {
|
||||
config: BunSQLiteConfig;
|
||||
};
|
||||
|
||||
export default function trimBackups({ config }: Params) {
|
||||
const { backup_dir } = grabDBDir({ config });
|
||||
|
||||
const backups = grabSortedBackups({ config });
|
||||
|
||||
const max_backups = config.max_backups || AppData["MaxBackups"];
|
||||
|
||||
for (let i = 0; i < backups.length; i++) {
|
||||
const backup_name = backups[i];
|
||||
if (!backup_name) continue;
|
||||
if (i > max_backups - 1) {
|
||||
const backup_file_to_unlink = path.join(backup_dir, backup_name);
|
||||
fs.unlinkSync(backup_file_to_unlink);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user