first Commit

This commit is contained in:
2026-03-02 07:56:05 +01:00
commit 5555a8e917
19 changed files with 1398 additions and 0 deletions
+70
View File
@@ -0,0 +1,70 @@
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";
type Params<T extends { [k: string]: any } = DSQL_TRAVIS_AI_ALL_TYPEDEFS> = {
table: (typeof DsqlTables)[number];
query?: ServerQueryParam<T>;
targetId?: number | string;
};
export default async function DbDelete<
T extends { [k: string]: any } = DSQL_TRAVIS_AI_ALL_TYPEDEFS,
>({ table, query, targetId }: Params<T>): Promise<APIResponseObject> {
try {
let finalQuery = query || {};
if (targetId) {
finalQuery = _.merge<ServerQueryParam<any>, ServerQueryParam<any>>(
finalQuery,
{
query: {
id: {
value: String(targetId),
},
},
},
);
}
const sqlQueryObj = datasquirel.sql.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,
};
}
}
+112
View File
@@ -0,0 +1,112 @@
import type {
DSQL_FieldSchemaType,
DSQL_TableSchemaType,
} from "@moduletrace/datasquirel/dist/package-shared/types";
type Param = {
paradigm: "JavaScript" | "TypeScript" | undefined;
table: DSQL_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
? `DSQL_${dbName}_${table.tableName}`.toUpperCase()
: `DSQL_${query.single}_${query.single_table}`.toUpperCase();
const fields = table.fields;
function typeMap(schemaType: DSQL_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 };
}
+45
View File
@@ -0,0 +1,45 @@
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";
type Params<T extends { [k: string]: any } = DSQL_TRAVIS_AI_ALL_TYPEDEFS> = {
table: (typeof DsqlTables)[number];
data: T[];
};
export default async function DbInsert<
T extends { [k: string]: any } = DSQL_TRAVIS_AI_ALL_TYPEDEFS,
>({ table, data }: Params<T>): Promise<APIResponseObject<DBChanges>> {
try {
const finalData: DSQL_TRAVIS_AI_ALL_TYPEDEFS[] = data.map((d) => ({
...d,
created_at: Date.now(),
updated_at: Date.now(),
}));
const sqlObj = datasquirel.sql.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
+67
View File
@@ -0,0 +1,67 @@
import type {
DSQL_DatabaseSchemaType,
DSQL_TableSchemaType,
} from "@moduletrace/datasquirel/dist/package-shared/types";
import _ from "lodash";
import generateTypeDefinition from "./generate-type-definitions";
type Params = {
dbSchema?: DSQL_DatabaseSchemaType;
};
export default function dbSchemaToType(params?: Params): string[] | undefined {
let datasquirelSchema = params?.dbSchema;
if (!datasquirelSchema) return;
let tableNames = `export const DsqlTables = [\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: `DSQL_${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 DSQL_${defDbName}_ALL_TYPEDEFS = ${defNames.join(` & `)}`
: ``;
return [tableNames, ...schemas, allTd];
}
+65
View File
@@ -0,0 +1,65 @@
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";
type Params<
T extends DSQL_TRAVIS_AI_ALL_TYPEDEFS = DSQL_TRAVIS_AI_ALL_TYPEDEFS,
> = {
query?: ServerQueryParam<T>;
table: (typeof DsqlTables)[number];
count?: boolean;
targetId?: number | string;
};
export default async function DbSelect<
T extends DSQL_TRAVIS_AI_ALL_TYPEDEFS = DSQL_TRAVIS_AI_ALL_TYPEDEFS,
>({ table, query, count, targetId }: Params<T>): Promise<APIResponseObject<T>> {
try {
let finalQuery = query || {};
if (targetId) {
finalQuery = _.merge<ServerQueryParam<any>, ServerQueryParam<any>>(
finalQuery,
{
query: {
id: {
value: String(targetId),
},
},
},
);
}
const sqlObj = datasquirel.sql.sqlGenerator({
tableName: table,
genObject: finalQuery,
count,
});
const sql = mysql.format(sqlObj.string, sqlObj.values);
const res = DbClient.query<T, T[]>(sql);
const batchRes = res.all();
return {
success: true,
payload: batchRes,
singleRes: batchRes[0],
debug: {
sqlObj,
sql,
},
};
} catch (error: any) {
return {
success: false,
error: error.message,
};
}
}
+100
View File
@@ -0,0 +1,100 @@
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";
type Params<T extends { [k: string]: any } = DSQL_TRAVIS_AI_ALL_TYPEDEFS> = {
table: (typeof DsqlTables)[number];
data: T;
query?: ServerQueryParam<T>;
targetId?: number | string;
};
export default async function DbUpdate<
T extends { [k: string]: any } = DSQL_TRAVIS_AI_ALL_TYPEDEFS,
>({ table, data, query, targetId }: Params<T>): Promise<APIResponseObject> {
try {
let finalQuery = query || {};
if (targetId) {
finalQuery = _.merge<ServerQueryParam<any>, ServerQueryParam<any>>(
finalQuery,
{
query: {
id: {
value: String(targetId),
},
},
},
);
}
const sqlQueryObj = datasquirel.sql.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: DSQL_TRAVIS_AI_ALL_TYPEDEFS = {
...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 DSQL_TRAVIS_AI_ALL_TYPEDEFS]),
);
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,
};
}
}
+26
View File
@@ -0,0 +1,26 @@
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";
const { ROOT_DIR } = grabDirNames();
const DBFilePath = path.join(ROOT_DIR, AppData["DbName"]);
const DBVecPluginFilePath = path.join(ROOT_DIR, AppData["DbVecPluginName"]);
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;
+28
View File
@@ -0,0 +1,28 @@
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";
type Params = {
dbSchema: DSQL_DatabaseSchemaType;
};
export default function dbSchemaToTypeDef({ dbSchema }: 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);
if (!existsSync(ourfileDir)) {
mkdirSync(ourfileDir, { recursive: true });
}
writeFileSync(finalOutfile, definitions?.join("\n\n") || "", "utf-8");
} catch (error: any) {
console.log(`Schema to Typedef Error =>`, error.message);
}
}
+35
View File
@@ -0,0 +1,35 @@
import type {
DSQL_DatabaseSchemaType,
DSQL_FieldSchemaType,
} from "@moduletrace/datasquirel/dist/package-shared/types";
import _ from "lodash";
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 = {
dbName: "travis-ai",
tables: [],
};