This commit is contained in:
Benjamin Toby
2025-04-17 10:49:34 +01:00
parent bcaaaae347
commit 6e32bbb4e0
14 changed files with 468 additions and 2 deletions
@@ -0,0 +1,76 @@
import fs from "fs";
import grabDirNames from "../../utils/backend/names/grab-dir-names";
import {
DSQL_DatabaseSchemaType,
DSQL_FieldSchemaType,
DSQL_TableSchemaType,
} from "../../types";
import _ from "lodash";
import EJSON from "../../utils/ejson";
import generateTypeDefinition from "./generate-type-definitions";
import path from "path";
type Params = {
dbSchema?: DSQL_DatabaseSchemaType;
};
export default function dbSchemaToType(params?: Params): string[] | undefined {
let datasquirelSchema;
const defaultTableFieldsJSONFilePath = path.resolve(
__dirname,
"../../data/defaultFields.json"
);
if (params?.dbSchema) {
datasquirelSchema = params.dbSchema;
} else {
const { mainShemaJSONFilePath } = grabDirNames();
const mainSchema = EJSON.parse(
fs.readFileSync(mainShemaJSONFilePath, "utf-8")
) as DSQL_DatabaseSchemaType[];
datasquirelSchema = mainSchema.find(
(sch) => sch.dbFullName == "datasquirel"
);
}
if (!datasquirelSchema) return;
let tableNames = `export const DsqlTables = [\n${datasquirelSchema.tables
.map((tbl) => ` "${tbl.tableName}",`)
.join("\n")}\n] as const`;
const defaultFields = EJSON.parse(
fs.readFileSync(defaultTableFieldsJSONFilePath, "utf-8")
) as DSQL_FieldSchemaType[];
const dbTablesSchemas = datasquirelSchema.tables.map((tblSchm) => {
let newDefaultFields = _.cloneDeep(defaultFields);
return {
...tblSchm,
fields: params?.dbSchema
? tblSchm.fields
: [
newDefaultFields.shift(),
newDefaultFields.shift(),
...tblSchm.fields,
...newDefaultFields,
],
} as DSQL_TableSchemaType;
});
const schemas = dbTablesSchemas
.map((table) =>
generateTypeDefinition({
paradigm: "TypeScript",
table,
typeDefName: `DSQL_DATASQUIREL_${table.tableName.toUpperCase()}`,
allValuesOptional: true,
addExport: true,
})
)
.filter((schm) => typeof schm == "string");
return [tableNames, ...schemas];
}
@@ -0,0 +1,16 @@
// @ts-check
/**
* Check for user in local storage
*
* @description Preventdefault, declare variables
*/
const defaultFieldsRegexp =
/^id$|^uuid$|^date_created$|^date_created_code$|^date_created_timestamp$|^date_updated$|^date_updated_code$|^date_updated_timestamp$/;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
export default defaultFieldsRegexp;
@@ -0,0 +1,87 @@
import { DSQL_TableSchemaType } from "../../types";
import defaultFieldsRegexp from "./default-fields-regexp";
type Param = {
paradigm: "JavaScript" | "TypeScript" | undefined;
table: DSQL_TableSchemaType;
query?: any;
typeDefName?: string;
allValuesOptional?: boolean;
addExport?: boolean;
};
export default function generateTypeDefinition({
paradigm,
table,
query,
typeDefName,
allValuesOptional,
addExport,
}: Param): string | null {
let typeDefinition: string | null = ``;
try {
const tdName =
typeDefName ||
`DSQL_${query.single}_${query.single_table}`.toUpperCase();
const fields = table.fields;
function typeMap(type: string) {
if (type?.match(/int/i)) {
return "number";
}
if (type?.match(/text|varchar|timestamp/i)) {
return "string";
}
return "string";
}
const typesArrayTypeScript = [];
const typesArrayJavascript = [];
typesArrayTypeScript.push(
`${addExport ? "export " : ""}type ${tdName} = {`
);
typesArrayJavascript.push(`/**\n * @typedef {object} ${tdName}`);
fields.forEach((field) => {
const nullValue = allValuesOptional
? "?"
: field.nullValue
? "?"
: field.fieldName?.match(defaultFieldsRegexp)
? "?"
: "";
typesArrayTypeScript.push(
` ${field.fieldName}${nullValue}: ${typeMap(
field.dataType || ""
)};`
);
typesArrayJavascript.push(
` * @property {${typeMap(field.dataType || "")}${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;
}