This commit is contained in:
Benjamin Toby 2026-01-14 11:01:08 +01:00
parent 0a03405598
commit b50e100732
8 changed files with 91 additions and 20 deletions

View File

@ -29,14 +29,25 @@ function dbSchemaToType(params) {
}); });
const defDbName = (_b = (datasquirelSchema.dbName || const defDbName = (_b = (datasquirelSchema.dbName ||
((_a = datasquirelSchema.dbFullName) === null || _a === void 0 ? void 0 : _a.replace(new RegExp(`${app_names_1.AppNames["DsqlDbPrefix"]}\\d+_`), "")))) === null || _b === void 0 ? void 0 : _b.toUpperCase().replace(/ /g, "_"); ((_a = datasquirelSchema.dbFullName) === null || _a === void 0 ? void 0 : _a.replace(new RegExp(`${app_names_1.AppNames["DsqlDbPrefix"]}\\d+_`), "")))) === null || _b === void 0 ? void 0 : _b.toUpperCase().replace(/ /g, "_");
const defNames = [];
const schemas = dbTablesSchemas const schemas = dbTablesSchemas
.map((table) => (0, generate_type_definitions_1.default)({ .map((table) => {
var _a;
const defObj = (0, generate_type_definitions_1.default)({
paradigm: "TypeScript", paradigm: "TypeScript",
table, table,
typeDefName: `DSQL_${defDbName}_${table.tableName.toUpperCase()}`, typeDefName: `DSQL_${defDbName}_${table.tableName.toUpperCase()}`,
allValuesOptional: true, allValuesOptional: true,
addExport: true, addExport: true,
})) });
if ((_a = defObj.tdName) === null || _a === void 0 ? void 0 : _a.match(/./)) {
defNames.push(defObj.tdName);
}
return defObj.typeDefinition;
})
.filter((schm) => typeof schm == "string"); .filter((schm) => typeof schm == "string");
return [tableNames, ...schemas]; const allTd = (defNames === null || defNames === void 0 ? void 0 : defNames[0])
? `export type DSQL_${defDbName}_ALL_TYPEDEFS = ${defNames.join(` & `)}`
: ``;
return [tableNames, ...schemas, allTd];
} }

View File

@ -8,5 +8,8 @@ type Param = {
addExport?: boolean; addExport?: boolean;
dbName?: string; dbName?: string;
}; };
export default function generateTypeDefinition({ paradigm, table, query, typeDefName, allValuesOptional, addExport, dbName, }: Param): string | null; export default function generateTypeDefinition({ paradigm, table, query, typeDefName, allValuesOptional, addExport, dbName, }: Param): {
typeDefinition: string | null;
tdName: string;
};
export {}; export {};

View File

@ -7,8 +7,9 @@ exports.default = generateTypeDefinition;
const default_fields_regexp_1 = __importDefault(require("./default-fields-regexp")); const default_fields_regexp_1 = __importDefault(require("./default-fields-regexp"));
function generateTypeDefinition({ paradigm, table, query, typeDefName, allValuesOptional, addExport, dbName, }) { function generateTypeDefinition({ paradigm, table, query, typeDefName, allValuesOptional, addExport, dbName, }) {
let typeDefinition = ``; let typeDefinition = ``;
let tdName = ``;
try { try {
const tdName = typeDefName tdName = typeDefName
? typeDefName ? typeDefName
: dbName : dbName
? `DSQL_${dbName}_${table.tableName}`.toUpperCase() ? `DSQL_${dbName}_${table.tableName}`.toUpperCase()
@ -67,5 +68,5 @@ function generateTypeDefinition({ paradigm, table, query, typeDefName, allValues
console.log(error.message); console.log(error.message);
typeDefinition = null; typeDefinition = null;
} }
return typeDefinition; return { typeDefinition, tdName };
} }

View File

@ -19,6 +19,7 @@ const handle_table_foreign_key_1 = __importDefault(require("./handle-table-forei
const handle_dsql_schema_fields_1 = __importDefault(require("./handle-dsql-schema-fields")); const handle_dsql_schema_fields_1 = __importDefault(require("./handle-dsql-schema-fields"));
const handle_mariadb_existing_columns_1 = __importDefault(require("./handle-mariadb-existing-columns")); const handle_mariadb_existing_columns_1 = __importDefault(require("./handle-mariadb-existing-columns"));
const update_table_init_1 = __importDefault(require("./update-table-init")); const update_table_init_1 = __importDefault(require("./update-table-init"));
const debug_log_1 = __importDefault(require("../../utils/logging/debug-log"));
/** /**
* # Update table function * # Update table function
*/ */
@ -41,6 +42,10 @@ function updateTable(_a) {
* @description Try to undate table, catch error if anything goes wrong * @description Try to undate table, catch error if anything goes wrong
*/ */
try { try {
(0, debug_log_1.default)({
log: `Handling Existing Columns ...`,
addTime: true,
});
const { allExistingColumns, upToDateTableFieldsArray } = yield (0, handle_mariadb_existing_columns_1.default)({ const { allExistingColumns, upToDateTableFieldsArray } = yield (0, handle_mariadb_existing_columns_1.default)({
dbFullName, dbFullName,
dbSchema, dbSchema,
@ -54,6 +59,10 @@ function updateTable(_a) {
* @description Iterate through each field object and * @description Iterate through each field object and
* perform operations * perform operations
*/ */
(0, debug_log_1.default)({
log: `Handling Schema Fields ...`,
addTime: true,
});
yield (0, handle_dsql_schema_fields_1.default)({ yield (0, handle_dsql_schema_fields_1.default)({
dbFullName, dbFullName,
tableName, tableName,
@ -66,6 +75,10 @@ function updateTable(_a) {
* @description Iterate through each datasquirel schema * @description Iterate through each datasquirel schema
* table index(if available), and perform operations * table index(if available), and perform operations
*/ */
(0, debug_log_1.default)({
log: `Handling Table Foreign Keys ...`,
addTime: true,
});
yield (0, handle_table_foreign_key_1.default)({ yield (0, handle_table_foreign_key_1.default)({
dbFullName, dbFullName,
fields: upToDateTableFieldsArray, fields: upToDateTableFieldsArray,
@ -78,6 +91,10 @@ function updateTable(_a) {
* @description Iterate through each datasquirel schema * @description Iterate through each datasquirel schema
* table index(if available), and perform operations * table index(if available), and perform operations
*/ */
(0, debug_log_1.default)({
log: `Handling Table Indexes ...`,
addTime: true,
});
if (tableIndexes === null || tableIndexes === void 0 ? void 0 : tableIndexes[0]) { if (tableIndexes === null || tableIndexes === void 0 ? void 0 : tableIndexes[0]) {
(0, handle_indexes_1.default)({ (0, handle_indexes_1.default)({
dbFullName, dbFullName,
@ -91,6 +108,10 @@ function updateTable(_a) {
* @description Iterate through each datasquirel schema * @description Iterate through each datasquirel schema
* table unique constraint(if available), and perform operations * table unique constraint(if available), and perform operations
*/ */
(0, debug_log_1.default)({
log: `Handling Unique Constraints ...`,
addTime: true,
});
if (tableUniqueConstraints === null || tableUniqueConstraints === void 0 ? void 0 : tableUniqueConstraints[0]) { if (tableUniqueConstraints === null || tableUniqueConstraints === void 0 ? void 0 : tableUniqueConstraints[0]) {
(0, handle_unique_constraints_1.default)({ (0, handle_unique_constraints_1.default)({
dbFullName, dbFullName,

View File

@ -42,17 +42,29 @@ export default function dbSchemaToType(params?: Params): string[] | undefined {
?.toUpperCase() ?.toUpperCase()
.replace(/ /g, "_"); .replace(/ /g, "_");
const defNames: string[] = [];
const schemas = dbTablesSchemas const schemas = dbTablesSchemas
.map((table) => .map((table) => {
generateTypeDefinition({ const defObj = generateTypeDefinition({
paradigm: "TypeScript", paradigm: "TypeScript",
table, table,
typeDefName: `DSQL_${defDbName}_${table.tableName.toUpperCase()}`, typeDefName: `DSQL_${defDbName}_${table.tableName.toUpperCase()}`,
allValuesOptional: true, allValuesOptional: true,
addExport: true, addExport: true,
});
if (defObj.tdName?.match(/./)) {
defNames.push(defObj.tdName);
}
return defObj.typeDefinition;
}) })
)
.filter((schm) => typeof schm == "string"); .filter((schm) => typeof schm == "string");
return [tableNames, ...schemas]; const allTd = defNames?.[0]
? `export type DSQL_${defDbName}_ALL_TYPEDEFS = ${defNames.join(` & `)}`
: ``;
return [tableNames, ...schemas, allTd];
} }

View File

@ -19,11 +19,12 @@ export default function generateTypeDefinition({
allValuesOptional, allValuesOptional,
addExport, addExport,
dbName, dbName,
}: Param): string | null { }: Param) {
let typeDefinition: string | null = ``; let typeDefinition: string | null = ``;
let tdName: string | null = ``;
try { try {
const tdName = typeDefName tdName = typeDefName
? typeDefName ? typeDefName
: dbName : dbName
? `DSQL_${dbName}_${table.tableName}`.toUpperCase() ? `DSQL_${dbName}_${table.tableName}`.toUpperCase()
@ -101,5 +102,5 @@ export default function generateTypeDefinition({
typeDefinition = null; typeDefinition = null;
} }
return typeDefinition; return { typeDefinition, tdName };
} }

View File

@ -13,6 +13,7 @@ import handleTableForeignKey from "./handle-table-foreign-key";
import handleDSQLSchemaFields from "./handle-dsql-schema-fields"; import handleDSQLSchemaFields from "./handle-dsql-schema-fields";
import handleMariaDBExistingColumns from "./handle-mariadb-existing-columns"; import handleMariaDBExistingColumns from "./handle-mariadb-existing-columns";
import updateTableInit from "./update-table-init"; import updateTableInit from "./update-table-init";
import debugLog from "../../utils/logging/debug-log";
type Param = { type Param = {
dbFullName: string; dbFullName: string;
@ -62,6 +63,11 @@ export default async function updateTable({
* @description Try to undate table, catch error if anything goes wrong * @description Try to undate table, catch error if anything goes wrong
*/ */
try { try {
debugLog({
log: `Handling Existing Columns ...`,
addTime: true,
});
const { allExistingColumns, upToDateTableFieldsArray } = const { allExistingColumns, upToDateTableFieldsArray } =
await handleMariaDBExistingColumns({ await handleMariaDBExistingColumns({
dbFullName, dbFullName,
@ -77,6 +83,10 @@ export default async function updateTable({
* @description Iterate through each field object and * @description Iterate through each field object and
* perform operations * perform operations
*/ */
debugLog({
log: `Handling Schema Fields ...`,
addTime: true,
});
await handleDSQLSchemaFields({ await handleDSQLSchemaFields({
dbFullName, dbFullName,
tableName, tableName,
@ -90,6 +100,10 @@ export default async function updateTable({
* @description Iterate through each datasquirel schema * @description Iterate through each datasquirel schema
* table index(if available), and perform operations * table index(if available), and perform operations
*/ */
debugLog({
log: `Handling Table Foreign Keys ...`,
addTime: true,
});
await handleTableForeignKey({ await handleTableForeignKey({
dbFullName, dbFullName,
fields: upToDateTableFieldsArray, fields: upToDateTableFieldsArray,
@ -103,6 +117,10 @@ export default async function updateTable({
* @description Iterate through each datasquirel schema * @description Iterate through each datasquirel schema
* table index(if available), and perform operations * table index(if available), and perform operations
*/ */
debugLog({
log: `Handling Table Indexes ...`,
addTime: true,
});
if (tableIndexes?.[0]) { if (tableIndexes?.[0]) {
handleIndexescreateDbFromSchema({ handleIndexescreateDbFromSchema({
dbFullName, dbFullName,
@ -117,6 +135,10 @@ export default async function updateTable({
* @description Iterate through each datasquirel schema * @description Iterate through each datasquirel schema
* table unique constraint(if available), and perform operations * table unique constraint(if available), and perform operations
*/ */
debugLog({
log: `Handling Unique Constraints ...`,
addTime: true,
});
if (tableUniqueConstraints?.[0]) { if (tableUniqueConstraints?.[0]) {
handleUniqueConstraintsCreateDbFromSchema({ handleUniqueConstraintsCreateDbFromSchema({
dbFullName, dbFullName,

View File

@ -1,6 +1,6 @@
{ {
"name": "@moduletrace/datasquirel", "name": "@moduletrace/datasquirel",
"version": "5.7.27", "version": "5.7.28",
"description": "Cloud-based SQL data management tool", "description": "Cloud-based SQL data management tool",
"main": "dist/index.js", "main": "dist/index.js",
"bin": { "bin": {