Add Engine V1
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env bun
|
||||
|
||||
import { program } from "commander";
|
||||
import schema from "./schema";
|
||||
import typedef from "./typedef";
|
||||
|
||||
/**
|
||||
* # 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());
|
||||
|
||||
/**
|
||||
* # 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(Bun.argv);
|
||||
@@ -0,0 +1,52 @@
|
||||
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 { DefaultFields } from "../types";
|
||||
import appendDefaultFieldsToDbSchema from "../utils/append-default-fields-to-db-schema";
|
||||
|
||||
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,
|
||||
});
|
||||
}
|
||||
|
||||
process.exit();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
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";
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
process.exit();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
export const AppData = {
|
||||
ConfigFileName: "bun-sqlite.config.ts",
|
||||
} 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,71 @@
|
||||
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);
|
||||
}
|
||||
|
||||
if (!Config.db_backup_dir) {
|
||||
console.error(`\`db_backup_dir\` 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 BackupDir = path.resolve(db_dir, Config.db_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 BunSQLite = {
|
||||
select: DbSelect,
|
||||
insert: DbInsert,
|
||||
update: DbUpdate,
|
||||
delete: DbDelete,
|
||||
sql: DbSQL,
|
||||
} as const;
|
||||
|
||||
export default BunSQLite;
|
||||
@@ -1,20 +1,16 @@
|
||||
import type { DSQL_TRAVIS_AI_ALL_TYPEDEFS, DsqlTables } from "@/types/db";
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
ServerQueryParam,
|
||||
} from "@moduletrace/datasquirel/dist/package-shared/types";
|
||||
import DbClient from ".";
|
||||
import _ from "lodash";
|
||||
import type { APIResponseObject, ServerQueryParam } from "../../types";
|
||||
import sqlGenerator from "../../utils/sql-generator";
|
||||
|
||||
type Params<T extends { [k: string]: any } = DSQL_TRAVIS_AI_ALL_TYPEDEFS> = {
|
||||
table: (typeof DsqlTables)[number];
|
||||
type Params<T extends { [k: string]: any } = { [k: string]: any }> = {
|
||||
table: string;
|
||||
query?: ServerQueryParam<T>;
|
||||
targetId?: number | string;
|
||||
};
|
||||
|
||||
export default async function DbDelete<
|
||||
T extends { [k: string]: any } = DSQL_TRAVIS_AI_ALL_TYPEDEFS,
|
||||
T extends { [k: string]: any } = { [k: string]: any },
|
||||
>({ table, query, targetId }: Params<T>): Promise<APIResponseObject> {
|
||||
try {
|
||||
let finalQuery = query || {};
|
||||
@@ -32,7 +28,7 @@ export default async function DbDelete<
|
||||
);
|
||||
}
|
||||
|
||||
const sqlQueryObj = datasquirel.sql.sqlGenerator({
|
||||
const sqlQueryObj = sqlGenerator({
|
||||
tableName: table,
|
||||
genObject: finalQuery,
|
||||
});
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import type {
|
||||
DSQL_FieldSchemaType,
|
||||
DSQL_TableSchemaType,
|
||||
} from "@moduletrace/datasquirel/dist/package-shared/types";
|
||||
BUN_SQLITE_FieldSchemaType,
|
||||
BUN_SQLITE_TableSchemaType,
|
||||
} from "../../types";
|
||||
|
||||
type Param = {
|
||||
paradigm: "JavaScript" | "TypeScript" | undefined;
|
||||
table: DSQL_TableSchemaType;
|
||||
table: BUN_SQLITE_TableSchemaType;
|
||||
query?: any;
|
||||
typeDefName?: string;
|
||||
allValuesOptional?: boolean;
|
||||
@@ -29,12 +29,12 @@ export default function generateTypeDefinition({
|
||||
tdName = typeDefName
|
||||
? typeDefName
|
||||
: dbName
|
||||
? `DSQL_${dbName}_${table.tableName}`.toUpperCase()
|
||||
: `DSQL_${query.single}_${query.single_table}`.toUpperCase();
|
||||
? `BUN_SQLITE_${dbName}_${table.tableName}`.toUpperCase()
|
||||
: `BUN_SQLITE_${query.single}_${query.single_table}`.toUpperCase();
|
||||
|
||||
const fields = table.fields;
|
||||
|
||||
function typeMap(schemaType: DSQL_FieldSchemaType) {
|
||||
function typeMap(schemaType: BUN_SQLITE_FieldSchemaType) {
|
||||
if (schemaType.options && schemaType.options.length > 0) {
|
||||
return schemaType.options
|
||||
.map((opt) =>
|
||||
|
||||
@@ -1,25 +1,23 @@
|
||||
import type { DSQL_TRAVIS_AI_ALL_TYPEDEFS, DsqlTables } from "@/types/db";
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
import type { APIResponseObject } from "@moduletrace/datasquirel/dist/package-shared/types";
|
||||
import DbClient from ".";
|
||||
import type { DBChanges } from "@/types/general";
|
||||
import type { APIResponseObject } from "../../types";
|
||||
import sqlInsertGenerator from "../../utils/sql-insert-generator";
|
||||
|
||||
type Params<T extends { [k: string]: any } = DSQL_TRAVIS_AI_ALL_TYPEDEFS> = {
|
||||
table: (typeof DsqlTables)[number];
|
||||
type Params<T extends { [k: string]: any } = { [k: string]: any }> = {
|
||||
table: string;
|
||||
data: T[];
|
||||
};
|
||||
|
||||
export default async function DbInsert<
|
||||
T extends { [k: string]: any } = DSQL_TRAVIS_AI_ALL_TYPEDEFS,
|
||||
>({ table, data }: Params<T>): Promise<APIResponseObject<DBChanges>> {
|
||||
T extends { [k: string]: any } = { [k: string]: any },
|
||||
>({ table, data }: Params<T>): Promise<APIResponseObject> {
|
||||
try {
|
||||
const finalData: DSQL_TRAVIS_AI_ALL_TYPEDEFS[] = data.map((d) => ({
|
||||
const finalData: { [k: string]: any }[] = data.map((d) => ({
|
||||
...d,
|
||||
created_at: Date.now(),
|
||||
updated_at: Date.now(),
|
||||
}));
|
||||
|
||||
const sqlObj = datasquirel.sql.sqlInsertGenerator({
|
||||
const sqlObj = sqlInsertGenerator({
|
||||
tableName: table,
|
||||
data: finalData as any[],
|
||||
});
|
||||
|
||||
@@ -1,26 +1,26 @@
|
||||
#!/usr/bin/env bun
|
||||
|
||||
import type {
|
||||
DSQL_DatabaseSchemaType,
|
||||
DSQL_FieldSchemaType,
|
||||
DSQL_TableSchemaType,
|
||||
} from "@moduletrace/datasquirel/dist/package-shared/types";
|
||||
import { Database } from "bun:sqlite";
|
||||
import _ from "lodash";
|
||||
import DbClient from ".";
|
||||
import type {
|
||||
BUN_SQLITE_DatabaseSchemaType,
|
||||
BUN_SQLITE_FieldSchemaType,
|
||||
BUN_SQLITE_TableSchemaType,
|
||||
} from "../../types";
|
||||
|
||||
// Schema Manager Class
|
||||
class SQLiteSchemaManager {
|
||||
private db: Database;
|
||||
private db_manager_table_name: string;
|
||||
private recreate_vector_table: boolean;
|
||||
private db_schema: DSQL_DatabaseSchemaType;
|
||||
private db_schema: BUN_SQLITE_DatabaseSchemaType;
|
||||
|
||||
constructor({
|
||||
schema,
|
||||
recreate_vector_table = false,
|
||||
}: {
|
||||
schema: DSQL_DatabaseSchemaType;
|
||||
schema: BUN_SQLITE_DatabaseSchemaType;
|
||||
recreate_vector_table?: boolean;
|
||||
}) {
|
||||
this.db = DbClient;
|
||||
@@ -113,7 +113,7 @@ class SQLiteSchemaManager {
|
||||
* Sync a single table (create or update)
|
||||
*/
|
||||
private async syncTable(
|
||||
table: DSQL_TableSchemaType,
|
||||
table: BUN_SQLITE_TableSchemaType,
|
||||
existingTables: string[],
|
||||
): Promise<void> {
|
||||
let tableExists = existingTables.includes(table.tableName);
|
||||
@@ -149,7 +149,9 @@ class SQLiteSchemaManager {
|
||||
/**
|
||||
* Create a new table
|
||||
*/
|
||||
private async createTable(table: DSQL_TableSchemaType): Promise<void> {
|
||||
private async createTable(
|
||||
table: BUN_SQLITE_TableSchemaType,
|
||||
): Promise<void> {
|
||||
console.log(`Creating table: ${table.tableName}`);
|
||||
|
||||
let new_table = _.cloneDeep(table);
|
||||
@@ -215,7 +217,9 @@ class SQLiteSchemaManager {
|
||||
/**
|
||||
* Update an existing table
|
||||
*/
|
||||
private async updateTable(table: DSQL_TableSchemaType): Promise<void> {
|
||||
private async updateTable(
|
||||
table: BUN_SQLITE_TableSchemaType,
|
||||
): Promise<void> {
|
||||
console.log(`Updating table: ${table.tableName}`);
|
||||
|
||||
const existingColumns = this.getTableColumns(table.tableName);
|
||||
@@ -272,7 +276,7 @@ class SQLiteSchemaManager {
|
||||
*/
|
||||
private async addColumn(
|
||||
tableName: string,
|
||||
field: DSQL_FieldSchemaType,
|
||||
field: BUN_SQLITE_FieldSchemaType,
|
||||
): Promise<void> {
|
||||
console.log(`Adding column: ${tableName}.${field.fieldName}`);
|
||||
|
||||
@@ -292,7 +296,9 @@ class SQLiteSchemaManager {
|
||||
/**
|
||||
* Recreate table (for complex schema changes)
|
||||
*/
|
||||
private async recreateTable(table: DSQL_TableSchemaType): Promise<void> {
|
||||
private async recreateTable(
|
||||
table: BUN_SQLITE_TableSchemaType,
|
||||
): Promise<void> {
|
||||
if (table.isVector) {
|
||||
if (!this.recreate_vector_table) {
|
||||
return;
|
||||
@@ -365,7 +371,7 @@ class SQLiteSchemaManager {
|
||||
/**
|
||||
* Build column definition SQL
|
||||
*/
|
||||
private buildColumnDefinition(field: DSQL_FieldSchemaType): string {
|
||||
private buildColumnDefinition(field: BUN_SQLITE_FieldSchemaType): string {
|
||||
if (!field.fieldName) {
|
||||
throw new Error("Field name is required");
|
||||
}
|
||||
@@ -420,7 +426,7 @@ class SQLiteSchemaManager {
|
||||
/**
|
||||
* Map DSQL data types to SQLite types
|
||||
*/
|
||||
private mapDataType(field: DSQL_FieldSchemaType): string {
|
||||
private mapDataType(field: BUN_SQLITE_FieldSchemaType): string {
|
||||
const dataType = field.dataType?.toLowerCase() || "text";
|
||||
const vectorSize = field.vectorSize || 1536;
|
||||
|
||||
@@ -472,7 +478,9 @@ class SQLiteSchemaManager {
|
||||
/**
|
||||
* Build foreign key constraint
|
||||
*/
|
||||
private buildForeignKeyConstraint(field: DSQL_FieldSchemaType): string {
|
||||
private buildForeignKeyConstraint(
|
||||
field: BUN_SQLITE_FieldSchemaType,
|
||||
): string {
|
||||
const fk = field.foreignKey!;
|
||||
let constraint = `FOREIGN KEY ("${field.fieldName}") REFERENCES "${fk.destinationTableName}"("${fk.destinationTableColumnName}")`;
|
||||
|
||||
@@ -490,7 +498,9 @@ class SQLiteSchemaManager {
|
||||
/**
|
||||
* Sync indexes for a table
|
||||
*/
|
||||
private async syncIndexes(table: DSQL_TableSchemaType): Promise<void> {
|
||||
private async syncIndexes(
|
||||
table: BUN_SQLITE_TableSchemaType,
|
||||
): Promise<void> {
|
||||
if (!table.indexes || table.indexes.length === 0) {
|
||||
return;
|
||||
}
|
||||
@@ -547,7 +557,7 @@ class SQLiteSchemaManager {
|
||||
|
||||
// Example usage
|
||||
async function main() {
|
||||
const schema: DSQL_DatabaseSchemaType = {
|
||||
const schema: BUN_SQLITE_DatabaseSchemaType = {
|
||||
dbName: "example_db",
|
||||
tables: [
|
||||
{
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
import type {
|
||||
DSQL_DatabaseSchemaType,
|
||||
DSQL_TableSchemaType,
|
||||
} from "@moduletrace/datasquirel/dist/package-shared/types";
|
||||
import _ from "lodash";
|
||||
import generateTypeDefinition from "./generate-type-definitions";
|
||||
import type { BUN_SQLITE_DatabaseSchemaType } from "../../types";
|
||||
import generateTypeDefinition from "./db-generate-type-defs";
|
||||
|
||||
type Params = {
|
||||
dbSchema?: DSQL_DatabaseSchemaType;
|
||||
dbSchema?: BUN_SQLITE_DatabaseSchemaType;
|
||||
};
|
||||
|
||||
export default function dbSchemaToType(params?: Params): string[] | undefined {
|
||||
@@ -14,7 +11,7 @@ export default function dbSchemaToType(params?: Params): string[] | undefined {
|
||||
|
||||
if (!datasquirelSchema) return;
|
||||
|
||||
let tableNames = `export const DsqlTables = [\n${datasquirelSchema.tables
|
||||
let tableNames = `export const BunSQLiteTables = [\n${datasquirelSchema.tables
|
||||
.map((tbl) => ` "${tbl.tableName}",`)
|
||||
.join("\n")}\n] as const`;
|
||||
|
||||
@@ -46,7 +43,7 @@ export default function dbSchemaToType(params?: Params): string[] | undefined {
|
||||
const defObj = generateTypeDefinition({
|
||||
paradigm: "TypeScript",
|
||||
table: final_table,
|
||||
typeDefName: `DSQL_${defDbName}_${final_table.tableName.toUpperCase()}`,
|
||||
typeDefName: `BUN_SQLITE_${defDbName}_${final_table.tableName.toUpperCase()}`,
|
||||
allValuesOptional: true,
|
||||
addExport: true,
|
||||
});
|
||||
@@ -60,7 +57,7 @@ export default function dbSchemaToType(params?: Params): string[] | undefined {
|
||||
.filter((schm) => typeof schm == "string");
|
||||
|
||||
const allTd = defNames?.[0]
|
||||
? `export type DSQL_${defDbName}_ALL_TYPEDEFS = ${defNames.join(` & `)}`
|
||||
? `export type BUN_SQLITE_${defDbName}_ALL_TYPEDEFS = ${defNames.join(` & `)}`
|
||||
: ``;
|
||||
|
||||
return [tableNames, ...schemas, allTd];
|
||||
|
||||
+18
-14
@@ -1,24 +1,18 @@
|
||||
import mysql from "mysql";
|
||||
import type { DSQL_TRAVIS_AI_ALL_TYPEDEFS, DsqlTables } from "@/types/db";
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
ServerQueryParam,
|
||||
} from "@moduletrace/datasquirel/dist/package-shared/types";
|
||||
import DbClient from ".";
|
||||
import _ from "lodash";
|
||||
import type { APIResponseObject, ServerQueryParam } from "../../types";
|
||||
import sqlGenerator from "../../utils/sql-generator";
|
||||
|
||||
type Params<
|
||||
T extends DSQL_TRAVIS_AI_ALL_TYPEDEFS = DSQL_TRAVIS_AI_ALL_TYPEDEFS,
|
||||
> = {
|
||||
type Params<T extends { [k: string]: any } = { [k: string]: any }> = {
|
||||
query?: ServerQueryParam<T>;
|
||||
table: (typeof DsqlTables)[number];
|
||||
table: string;
|
||||
count?: boolean;
|
||||
targetId?: number | string;
|
||||
};
|
||||
|
||||
export default async function DbSelect<
|
||||
T extends DSQL_TRAVIS_AI_ALL_TYPEDEFS = DSQL_TRAVIS_AI_ALL_TYPEDEFS,
|
||||
T extends { [k: string]: any } = { [k: string]: any },
|
||||
>({ table, query, count, targetId }: Params<T>): Promise<APIResponseObject<T>> {
|
||||
try {
|
||||
let finalQuery = query || {};
|
||||
@@ -36,7 +30,7 @@ export default async function DbSelect<
|
||||
);
|
||||
}
|
||||
|
||||
const sqlObj = datasquirel.sql.sqlGenerator({
|
||||
const sqlObj = sqlGenerator({
|
||||
tableName: table,
|
||||
genObject: finalQuery,
|
||||
count,
|
||||
@@ -47,8 +41,8 @@ export default async function DbSelect<
|
||||
const res = DbClient.query<T, T[]>(sql);
|
||||
const batchRes = res.all();
|
||||
|
||||
return {
|
||||
success: true,
|
||||
let resp: APIResponseObject<T> = {
|
||||
success: Boolean(batchRes[0]),
|
||||
payload: batchRes,
|
||||
singleRes: batchRes[0],
|
||||
debug: {
|
||||
@@ -56,6 +50,16 @@ export default async function DbSelect<
|
||||
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,
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,17 @@
|
||||
import mysql from "mysql";
|
||||
import type { DSQL_TRAVIS_AI_ALL_TYPEDEFS, DsqlTables } from "@/types/db";
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
ServerQueryParam,
|
||||
} from "@moduletrace/datasquirel/dist/package-shared/types";
|
||||
import DbClient from ".";
|
||||
import _ from "lodash";
|
||||
import type { APIResponseObject, ServerQueryParam } from "../../types";
|
||||
import sqlGenerator from "../../utils/sql-generator";
|
||||
|
||||
type Params<T extends { [k: string]: any } = DSQL_TRAVIS_AI_ALL_TYPEDEFS> = {
|
||||
table: (typeof DsqlTables)[number];
|
||||
type Params<T extends { [k: string]: any } = { [k: string]: any }> = {
|
||||
table: string;
|
||||
data: T;
|
||||
query?: ServerQueryParam<T>;
|
||||
targetId?: number | string;
|
||||
};
|
||||
|
||||
export default async function DbUpdate<
|
||||
T extends { [k: string]: any } = DSQL_TRAVIS_AI_ALL_TYPEDEFS,
|
||||
T extends { [k: string]: any } = { [k: string]: any },
|
||||
>({ table, data, query, targetId }: Params<T>): Promise<APIResponseObject> {
|
||||
try {
|
||||
let finalQuery = query || {};
|
||||
@@ -34,7 +29,7 @@ export default async function DbUpdate<
|
||||
);
|
||||
}
|
||||
|
||||
const sqlQueryObj = datasquirel.sql.sqlGenerator({
|
||||
const sqlQueryObj = sqlGenerator({
|
||||
tableName: table,
|
||||
genObject: finalQuery,
|
||||
});
|
||||
@@ -46,7 +41,7 @@ export default async function DbUpdate<
|
||||
if (whereClause) {
|
||||
let sql = `UPDATE ${table} SET`;
|
||||
|
||||
const finalData: DSQL_TRAVIS_AI_ALL_TYPEDEFS = {
|
||||
const finalData: { [k: string]: any } = {
|
||||
...data,
|
||||
updated_at: Date.now(),
|
||||
};
|
||||
@@ -61,7 +56,7 @@ export default async function DbUpdate<
|
||||
|
||||
sql += ` ${key}=?`;
|
||||
values.push(
|
||||
String(finalData[key as keyof DSQL_TRAVIS_AI_ALL_TYPEDEFS]),
|
||||
String(finalData[key as keyof { [k: string]: any }]),
|
||||
);
|
||||
|
||||
if (!isLast) {
|
||||
|
||||
+10
-12
@@ -1,26 +1,24 @@
|
||||
import AppData from "@/data/app-data";
|
||||
import grabDirNames from "@/utils/grab-dir-names";
|
||||
import { Database } from "bun:sqlite";
|
||||
import path from "node:path";
|
||||
import * as sqliteVec from "sqlite-vec";
|
||||
import grabDirNames from "../../data/grab-dir-names";
|
||||
import init from "../../functions/init";
|
||||
|
||||
const { ROOT_DIR } = grabDirNames();
|
||||
const { config } = await init();
|
||||
|
||||
const DBFilePath = path.join(ROOT_DIR, AppData["DbName"]);
|
||||
const DBVecPluginFilePath = path.join(ROOT_DIR, AppData["DbVecPluginName"]);
|
||||
let db_dir = ROOT_DIR;
|
||||
|
||||
if (config.db_dir) {
|
||||
db_dir = config.db_dir;
|
||||
}
|
||||
|
||||
const DBFilePath = path.join(db_dir, config.db_name);
|
||||
|
||||
const DbClient = new Database(DBFilePath, {
|
||||
create: true,
|
||||
});
|
||||
|
||||
// DbClient.loadExtension(DBVecPluginFilePath);
|
||||
|
||||
sqliteVec.load(DbClient);
|
||||
|
||||
// Test if it's working
|
||||
// const { vec_version } = DbClient.prepare(
|
||||
// "select vec_version() as vec_version",
|
||||
// ).get();
|
||||
// console.log(`sqlite-vec version: ${vec_version}`);
|
||||
|
||||
export default DbClient;
|
||||
|
||||
@@ -1,27 +1,26 @@
|
||||
import type { DSQL_DatabaseSchemaType } from "@moduletrace/datasquirel/dist/package-shared/types";
|
||||
import dbSchemaToType from "./db-schema-to-type";
|
||||
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: DSQL_DatabaseSchemaType;
|
||||
dbSchema: BUN_SQLITE_DatabaseSchemaType;
|
||||
dst_file: string;
|
||||
};
|
||||
|
||||
export default function dbSchemaToTypeDef({ dbSchema }: Params) {
|
||||
export default function dbSchemaToTypeDef({ dbSchema, dst_file }: Params) {
|
||||
try {
|
||||
if (!dbSchema) throw new Error("No schema found");
|
||||
|
||||
const definitions = dbSchemaToType({ dbSchema });
|
||||
|
||||
const finalOutfile = path.resolve(__dirname, "../types/db/index.ts");
|
||||
|
||||
const ourfileDir = path.dirname(finalOutfile);
|
||||
const ourfileDir = path.dirname(dst_file);
|
||||
|
||||
if (!existsSync(ourfileDir)) {
|
||||
mkdirSync(ourfileDir, { recursive: true });
|
||||
}
|
||||
|
||||
writeFileSync(finalOutfile, definitions?.join("\n\n") || "", "utf-8");
|
||||
writeFileSync(dst_file, definitions?.join("\n\n") || "", "utf-8");
|
||||
} catch (error: any) {
|
||||
console.log(`Schema to Typedef Error =>`, error.message);
|
||||
}
|
||||
|
||||
@@ -1,35 +1,7 @@
|
||||
import type {
|
||||
DSQL_DatabaseSchemaType,
|
||||
DSQL_FieldSchemaType,
|
||||
} from "@moduletrace/datasquirel/dist/package-shared/types";
|
||||
import _ from "lodash";
|
||||
import type { BUN_SQLITE_DatabaseSchemaType } from "../../types";
|
||||
|
||||
const DefaultFields: DSQL_FieldSchemaType[] = [
|
||||
{
|
||||
fieldName: "id",
|
||||
dataType: "INTEGER",
|
||||
primaryKey: true,
|
||||
autoIncrement: true,
|
||||
notNullValue: true,
|
||||
fieldDescription: "The unique identifier of the record.",
|
||||
},
|
||||
{
|
||||
fieldName: "created_at",
|
||||
dataType: "INTEGER",
|
||||
notNullValue: true,
|
||||
fieldDescription:
|
||||
"The time when the record was created. (Unix Timestamp)",
|
||||
},
|
||||
{
|
||||
fieldName: "updated_at",
|
||||
dataType: "INTEGER",
|
||||
notNullValue: true,
|
||||
fieldDescription:
|
||||
"The time when the record was updated. (Unix Timestamp)",
|
||||
},
|
||||
];
|
||||
|
||||
export const DbSchema: DSQL_DatabaseSchemaType = {
|
||||
export const DbSchema: BUN_SQLITE_DatabaseSchemaType = {
|
||||
dbName: "travis-ai",
|
||||
tables: [],
|
||||
};
|
||||
|
||||
+1192
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,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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user