First Commit. Based off bun-sqlite
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import type { Connection, ConnectionConfig } from "mariadb";
|
||||
import type { BUN_MARIADB_TableSchemaType, DBResponseObject } from "../types";
|
||||
import grabDBConnection from "./grab-db-connection";
|
||||
|
||||
type Param = {
|
||||
query: string;
|
||||
values?: string[] | object;
|
||||
noErrorLogs?: boolean;
|
||||
database?: string;
|
||||
tableSchema?: BUN_MARIADB_TableSchemaType;
|
||||
config?: ConnectionConfig;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
*/
|
||||
export default async function dbHandler<
|
||||
T extends { [k: string]: any } = { [k: string]: any },
|
||||
>({
|
||||
query,
|
||||
values,
|
||||
noErrorLogs,
|
||||
database,
|
||||
config,
|
||||
}: Param): Promise<DBResponseObject> {
|
||||
let CONNECTION: Connection | undefined;
|
||||
let results: T | null = null;
|
||||
|
||||
try {
|
||||
CONNECTION = await grabDBConnection({ database, config });
|
||||
|
||||
if (query && values) {
|
||||
const queryResults = await CONNECTION.query(query, values);
|
||||
results = queryResults[0];
|
||||
} else {
|
||||
const queryResults = await CONNECTION.query(query);
|
||||
results = queryResults[0];
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (
|
||||
error.message &&
|
||||
typeof error.message == "string" &&
|
||||
error.message.match(/Access denied for user.*password/i)
|
||||
) {
|
||||
throw new Error("Authentication Failed!");
|
||||
}
|
||||
|
||||
if (!noErrorLogs) {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
results = null;
|
||||
} finally {
|
||||
await CONNECTION?.end();
|
||||
}
|
||||
|
||||
if (results) {
|
||||
return {
|
||||
success: true,
|
||||
payload: Array.isArray(results) ? results : undefined,
|
||||
single_res: Array.isArray(results) ? undefined : results,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
success: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import * as mariadb from "mariadb";
|
||||
import type { Connection } from "mariadb";
|
||||
import grabDSQLConnectionConfig from "./grab-dsql-connection-config";
|
||||
import type { DsqlConnectionParam } from "../types";
|
||||
|
||||
/**
|
||||
* # Grab General CONNECTION for DSQL
|
||||
*/
|
||||
export default async function grabDBConnection(
|
||||
param?: DsqlConnectionParam,
|
||||
): Promise<Connection> {
|
||||
const config = grabDSQLConnectionConfig(param);
|
||||
|
||||
try {
|
||||
return await mariadb.createConnection(config);
|
||||
} catch (error) {
|
||||
console.log(`Error Grabbing DSQL Connection =>`, config);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import fs from "fs";
|
||||
import type { ConnectionConfig } from "mariadb";
|
||||
import path from "path";
|
||||
|
||||
type Return = ConnectionConfig["ssl"] | undefined;
|
||||
|
||||
/**
|
||||
* # Grab SSL
|
||||
*/
|
||||
export default function grabDbSSL(): Return {
|
||||
const caProivdedPath = process.env.DSQL_SSL_CA_CERT;
|
||||
|
||||
if (!caProivdedPath?.match(/./)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const caFilePath = path.resolve(process.cwd(), caProivdedPath);
|
||||
|
||||
if (!fs.existsSync(caFilePath)) {
|
||||
console.log(`${caFilePath} does not exist`);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
ca: fs.readFileSync(caFilePath),
|
||||
rejectUnauthorized: false,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { type ConnectionConfig } from "mariadb";
|
||||
import type { DsqlConnectionParam } from "../types";
|
||||
import grabDbSSL from "./grab-db-ssl";
|
||||
|
||||
/**
|
||||
* # Grab General CONNECTION for DSQL
|
||||
*/
|
||||
export default function grabDSQLConnectionConfig(
|
||||
param?: DsqlConnectionParam,
|
||||
): ConnectionConfig {
|
||||
const CONN_TIMEOUT = 10000;
|
||||
|
||||
const config: ConnectionConfig = {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
// database: param?.dbFullName,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
bigIntAsNumber: true,
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
metaAsArray: true,
|
||||
socketTimeout: CONN_TIMEOUT,
|
||||
connectTimeout: CONN_TIMEOUT,
|
||||
compress: true,
|
||||
...param?.config,
|
||||
};
|
||||
|
||||
return config;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
type Params = {
|
||||
sql: string;
|
||||
table: string;
|
||||
data: any | any[];
|
||||
};
|
||||
|
||||
export default async function ({ sql: passed_sql, table, data }: Params) {
|
||||
let sql = passed_sql;
|
||||
const config = global.CONFIG;
|
||||
const dbSchema = global.DB_SCHEMA;
|
||||
|
||||
const table_schema = dbSchema.tables.find((t) => t.tableName == table);
|
||||
const now = Date.now();
|
||||
|
||||
if (table_schema?.tableName) {
|
||||
const set_sql_arr = Object.keys(
|
||||
Array.isArray(data) ? data[0] : data,
|
||||
).map((field) => `${field} = excluded.${field}`);
|
||||
|
||||
set_sql_arr.push(`updated_at = ${now}`);
|
||||
|
||||
const set_sql = set_sql_arr.join(", ");
|
||||
|
||||
const unique_fields = table_schema.fields.filter((f) => f.unique);
|
||||
|
||||
for (let i = 0; i < unique_fields.length; i++) {
|
||||
const field = unique_fields[i];
|
||||
sql += ` ON CONFLICT(${field?.fieldName}) DO UPDATE SET ${set_sql}`;
|
||||
}
|
||||
|
||||
if (table_schema.uniqueConstraints?.[0]) {
|
||||
for (let i = 0; i < table_schema.uniqueConstraints.length; i++) {
|
||||
const constraint = table_schema.uniqueConstraints[i];
|
||||
sql += ` ON CONFLICT(${constraint?.constraintTableFields?.map((c) => c.value)?.join(", ")}) DO UPDATE SET ${set_sql}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return sql;
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
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> {
|
||||
let sqlObj: ReturnType<typeof sqlGenerator> | null = null;
|
||||
|
||||
try {
|
||||
let finalQuery = query || {};
|
||||
|
||||
if (targetId) {
|
||||
finalQuery = _.merge<ServerQueryParam<any>, ServerQueryParam<any>>(
|
||||
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: any) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
debug: {
|
||||
sqlObj,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
import type {
|
||||
BUN_MARIADB_FieldSchemaType,
|
||||
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) {
|
||||
let typeDefinition: string | null = ``;
|
||||
let tdName: string | null = ``;
|
||||
|
||||
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: BUN_MARIADB_FieldSchemaType) {
|
||||
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: any) {
|
||||
console.log(error.message);
|
||||
typeDefinition = null;
|
||||
}
|
||||
|
||||
return { typeDefinition, tdName };
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import DbClient from ".";
|
||||
import type { APIResponseObject, SQLInsertGenReturn } from "../../types";
|
||||
import sqlInsertGenerator from "../../utils/sql-insert-generator";
|
||||
import grabDuplicateSafeInsertSql from "../grab-duplicate-safe-insert-sql";
|
||||
|
||||
type Params<
|
||||
Schema extends { [k: string]: any } = { [k: string]: any },
|
||||
Table extends string = string,
|
||||
> = {
|
||||
table: Table;
|
||||
data: Schema[];
|
||||
update_on_duplicate?: boolean;
|
||||
};
|
||||
|
||||
export default async function DbInsert<
|
||||
Schema extends { [k: string]: any } = { [k: string]: any },
|
||||
Table extends string = string,
|
||||
>({
|
||||
table,
|
||||
data,
|
||||
update_on_duplicate,
|
||||
}: Params<Schema, Table>): Promise<APIResponseObject> {
|
||||
let sqlObj: SQLInsertGenReturn | null = null;
|
||||
|
||||
try {
|
||||
const finalData: { [k: string]: any }[] = data.map((d) => ({
|
||||
created_at: Date.now(),
|
||||
updated_at: Date.now(),
|
||||
...d,
|
||||
}));
|
||||
|
||||
sqlObj =
|
||||
sqlInsertGenerator({
|
||||
tableName: table,
|
||||
data: finalData as any[],
|
||||
}) || null;
|
||||
|
||||
let sql = sqlObj?.query || "";
|
||||
|
||||
if (update_on_duplicate && data[0]) {
|
||||
sql = await grabDuplicateSafeInsertSql({ data, table, sql });
|
||||
}
|
||||
|
||||
(sqlObj || ({} as any)).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: any) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
debug: {
|
||||
sqlObj,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,71 @@
|
||||
import _ from "lodash";
|
||||
import type {
|
||||
BUN_MARIADB_DatabaseSchemaType,
|
||||
BunMariaDBConfig,
|
||||
} from "../../types";
|
||||
import generateTypeDefinition from "./db-generate-type-defs";
|
||||
|
||||
type Params = {
|
||||
dbSchema: BUN_MARIADB_DatabaseSchemaType;
|
||||
config: BunMariaDBConfig;
|
||||
};
|
||||
|
||||
export default function dbSchemaToType({
|
||||
config,
|
||||
dbSchema,
|
||||
}: Params): string[] | undefined {
|
||||
let datasquirelSchema = dbSchema;
|
||||
|
||||
if (!datasquirelSchema) return;
|
||||
|
||||
let tableNames = `export const BunMariaDBTables = [\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: 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_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];
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
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>> {
|
||||
let sqlObj: ReturnType<typeof sqlGenerator> | null = null;
|
||||
|
||||
try {
|
||||
let finalQuery = query || {};
|
||||
|
||||
if (targetId) {
|
||||
finalQuery = _.merge<ServerQueryParam<any>, ServerQueryParam<any>>(
|
||||
finalQuery,
|
||||
{
|
||||
query: {
|
||||
id: {
|
||||
value: String(targetId),
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
sqlObj = sqlGenerator({
|
||||
tableName: table,
|
||||
genObject: finalQuery,
|
||||
});
|
||||
|
||||
let 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) {
|
||||
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<Schema, Schema[]>(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: any) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
debug: {
|
||||
sqlObj,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import DbClient from ".";
|
||||
import _ from "lodash";
|
||||
import type { APIResponseObject, SQLInsertGenValueType } from "../../types";
|
||||
|
||||
type Params = {
|
||||
sql: string;
|
||||
values?: SQLInsertGenValueType[];
|
||||
};
|
||||
|
||||
export default async function DbSQL<
|
||||
T extends { [k: string]: any } = { [k: string]: any },
|
||||
>({ sql, values }: Params): Promise<APIResponseObject<T>> {
|
||||
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 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: trimmed_sql,
|
||||
values,
|
||||
},
|
||||
sql,
|
||||
},
|
||||
};
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
import DbClient from ".";
|
||||
import _ from "lodash";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
SQLInsertGenValueType,
|
||||
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> {
|
||||
let sqlObj: ReturnType<typeof sqlGenerator> = { string: "", values: [] };
|
||||
|
||||
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: SQLInsertGenValueType[] = [];
|
||||
|
||||
const whereClause = sqlQueryObj.string.match(/WHERE .*/)?.[0];
|
||||
|
||||
if (whereClause) {
|
||||
let sql = `UPDATE ${table} SET`;
|
||||
|
||||
const finalData: { [k: string]: SQLInsertGenValueType } = {
|
||||
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 as any[];
|
||||
|
||||
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: any) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
debug: {
|
||||
sqlObj,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import path from "node:path";
|
||||
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
||||
import type {
|
||||
BUN_MARIADB_DatabaseSchemaType,
|
||||
BunMariaDBConfig,
|
||||
} from "../../types";
|
||||
import dbSchemaToType from "./db-schema-to-typedef";
|
||||
|
||||
type Params = {
|
||||
dbSchema: BUN_MARIADB_DatabaseSchemaType;
|
||||
dst_file: string;
|
||||
config: BunMariaDBConfig;
|
||||
};
|
||||
|
||||
export default function dbSchemaToTypeDef({
|
||||
dbSchema,
|
||||
dst_file,
|
||||
config,
|
||||
}: Params) {
|
||||
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: any) {
|
||||
console.log(`Schema to Typedef Error =>`, error.message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import _ from "lodash";
|
||||
import type { BUN_MARIADB_DatabaseSchemaType } from "../../types";
|
||||
|
||||
export const DbSchema: BUN_MARIADB_DatabaseSchemaType = {
|
||||
dbName: "travis-ai",
|
||||
tables: [],
|
||||
};
|
||||
Reference in New Issue
Block a user