Updates
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
import { DSQL_DatabaseSchemaType } from "../../types";
|
||||
type Params = {
|
||||
dbSchema?: DSQL_DatabaseSchemaType;
|
||||
};
|
||||
export default function dbSchemaToType(params?: Params): string[] | undefined;
|
||||
export {};
|
||||
@@ -0,0 +1,51 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = dbSchemaToType;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const grab_dir_names_1 = __importDefault(require("../../utils/backend/names/grab-dir-names"));
|
||||
const lodash_1 = __importDefault(require("lodash"));
|
||||
const ejson_1 = __importDefault(require("../../utils/ejson"));
|
||||
const generate_type_definitions_1 = __importDefault(require("./generate-type-definitions"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
function dbSchemaToType(params) {
|
||||
let datasquirelSchema;
|
||||
const defaultTableFieldsJSONFilePath = path_1.default.resolve(__dirname, "../../data/defaultFields.json");
|
||||
if (params === null || params === void 0 ? void 0 : params.dbSchema) {
|
||||
datasquirelSchema = params.dbSchema;
|
||||
}
|
||||
else {
|
||||
const { mainShemaJSONFilePath } = (0, grab_dir_names_1.default)();
|
||||
const mainSchema = ejson_1.default.parse(fs_1.default.readFileSync(mainShemaJSONFilePath, "utf-8"));
|
||||
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_1.default.parse(fs_1.default.readFileSync(defaultTableFieldsJSONFilePath, "utf-8"));
|
||||
const dbTablesSchemas = datasquirelSchema.tables.map((tblSchm) => {
|
||||
let newDefaultFields = lodash_1.default.cloneDeep(defaultFields);
|
||||
return Object.assign(Object.assign({}, tblSchm), { fields: (params === null || params === void 0 ? void 0 : params.dbSchema)
|
||||
? tblSchm.fields
|
||||
: [
|
||||
newDefaultFields.shift(),
|
||||
newDefaultFields.shift(),
|
||||
...tblSchm.fields,
|
||||
...newDefaultFields,
|
||||
] });
|
||||
});
|
||||
const schemas = dbTablesSchemas
|
||||
.map((table) => (0, generate_type_definitions_1.default)({
|
||||
paradigm: "TypeScript",
|
||||
table,
|
||||
typeDefName: `DSQL_DATASQUIREL_${table.tableName.toUpperCase()}`,
|
||||
allValuesOptional: true,
|
||||
addExport: true,
|
||||
}))
|
||||
.filter((schm) => typeof schm == "string");
|
||||
return [tableNames, ...schemas];
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* Check for user in local storage
|
||||
*
|
||||
* @description Preventdefault, declare variables
|
||||
*/
|
||||
declare const defaultFieldsRegexp: RegExp;
|
||||
export default defaultFieldsRegexp;
|
||||
@@ -0,0 +1,13 @@
|
||||
"use strict";
|
||||
// @ts-check
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
/**
|
||||
* 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$/;
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
exports.default = defaultFieldsRegexp;
|
||||
@@ -0,0 +1,11 @@
|
||||
import { DSQL_TableSchemaType } from "../../types";
|
||||
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;
|
||||
export {};
|
||||
@@ -0,0 +1,53 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = generateTypeDefinition;
|
||||
const default_fields_regexp_1 = __importDefault(require("./default-fields-regexp"));
|
||||
function generateTypeDefinition({ paradigm, table, query, typeDefName, allValuesOptional, addExport, }) {
|
||||
let typeDefinition = ``;
|
||||
try {
|
||||
const tdName = typeDefName ||
|
||||
`DSQL_${query.single}_${query.single_table}`.toUpperCase();
|
||||
const fields = table.fields;
|
||||
function typeMap(type) {
|
||||
if (type === null || type === void 0 ? void 0 : type.match(/int/i)) {
|
||||
return "number";
|
||||
}
|
||||
if (type === null || type === void 0 ? void 0 : 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) => {
|
||||
var _a;
|
||||
const nullValue = allValuesOptional
|
||||
? "?"
|
||||
: field.nullValue
|
||||
? "?"
|
||||
: ((_a = field.fieldName) === null || _a === void 0 ? void 0 : _a.match(default_fields_regexp_1.default))
|
||||
? "?"
|
||||
: "";
|
||||
typesArrayTypeScript.push(` ${field.fieldName}${nullValue}: ${typeMap(field.dataType || "")};`);
|
||||
typesArrayJavascript.push(` * @property {${typeMap(field.dataType || "")}${nullValue}} ${field.fieldName}`);
|
||||
});
|
||||
typesArrayTypeScript.push(`}`);
|
||||
typesArrayJavascript.push(` */`);
|
||||
if (paradigm === null || paradigm === void 0 ? void 0 : paradigm.match(/javascript/i)) {
|
||||
typeDefinition = typesArrayJavascript.join("\n");
|
||||
}
|
||||
if (paradigm === null || paradigm === void 0 ? void 0 : paradigm.match(/typescript/i)) {
|
||||
typeDefinition = typesArrayTypeScript.join("\n");
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
console.log(error.message);
|
||||
typeDefinition = null;
|
||||
}
|
||||
return typeDefinition;
|
||||
}
|
||||
Reference in New Issue
Block a user