datasquirel/dist/package-shared/utils/db/schema/resolve-schema-children-handle-children-tables.js
2025-07-05 16:14:11 +01:00

140 lines
8.0 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
const grab_required_database_schemas_1 = require("../../../shell/createDbFromSchema/grab-required-database-schemas");
const lodash_1 = __importDefault(require("lodash"));
const unique_by_key_1 = __importDefault(require("../../unique-by-key"));
function default_1({ currentDbSchema, currentTableSchema, currentTableSchemaIndex, userId, }) {
var _a, _b, _c, _d, _e, _f, _g;
if (!currentDbSchema.dbFullName) {
throw new Error(`Resolve Children tables ERROR => currentDbSchema.dbFullName not found!`);
}
const newCurrentDbSchema = lodash_1.default.cloneDeep(currentDbSchema);
if (newCurrentDbSchema.tables[currentTableSchemaIndex].childrenTables) {
for (let ch = 0; ch <
newCurrentDbSchema.tables[currentTableSchemaIndex].childrenTables
.length; ch++) {
const childTable = newCurrentDbSchema.tables[currentTableSchemaIndex]
.childrenTables[ch];
if (!childTable.dbId || !childTable.tableId) {
(_a = newCurrentDbSchema.tables[currentTableSchemaIndex].childrenTables) === null || _a === void 0 ? void 0 : _a.splice(ch, 1, {});
continue;
}
const targetChildTableParentDatabase = (0, grab_required_database_schemas_1.grabPrimaryRequiredDbSchema)({
dbId: childTable.dbId,
userId,
});
/**
* Delete child table from array if the parent database
* of said child table has been deleted or doesn't exist
*/
if (!(targetChildTableParentDatabase === null || targetChildTableParentDatabase === void 0 ? void 0 : targetChildTableParentDatabase.dbFullName)) {
(_b = newCurrentDbSchema.tables[currentTableSchemaIndex].childrenTables) === null || _b === void 0 ? void 0 : _b.splice(ch, 1, {});
}
else {
/**
* Delete child table from array if the parent database
* exists but the target tabled has been deleted or doesn't
* exist
*/
const targetChildTableParentDatabaseTableIndex = targetChildTableParentDatabase.tables.findIndex((tbl) => tbl.id == childTable.tableId);
const targetChildTableParentDatabaseTable = targetChildTableParentDatabase.tables[targetChildTableParentDatabaseTableIndex];
if (targetChildTableParentDatabaseTable === null || targetChildTableParentDatabaseTable === void 0 ? void 0 : targetChildTableParentDatabaseTable.childTable) {
targetChildTableParentDatabase.tables[targetChildTableParentDatabaseTableIndex].fields = [...currentTableSchema.fields];
targetChildTableParentDatabase.tables[targetChildTableParentDatabaseTableIndex].indexes = [...(currentTableSchema.indexes || [])];
(0, grab_required_database_schemas_1.writeUpdatedDbSchema)({
dbSchema: targetChildTableParentDatabase,
userId,
});
}
else {
(_c = newCurrentDbSchema.tables[currentTableSchemaIndex].childrenTables) === null || _c === void 0 ? void 0 : _c.splice(ch, 1, {});
}
}
}
if ((_d = newCurrentDbSchema.tables[currentTableSchemaIndex]
.childrenTables) === null || _d === void 0 ? void 0 : _d[0]) {
newCurrentDbSchema.tables[currentTableSchemaIndex].childrenTables =
(0, unique_by_key_1.default)(newCurrentDbSchema.tables[currentTableSchemaIndex].childrenTables.filter((tbl) => Boolean(tbl.dbId) && Boolean(tbl.tableId)), "dbId");
}
else {
delete newCurrentDbSchema.tables[currentTableSchemaIndex]
.childrenTables;
}
}
/**
* Handle scenario where this table is a child of another
*/
if (currentTableSchema.childTable &&
currentTableSchema.childTableDbId &&
currentTableSchema.childTableDbId) {
const targetParentDatabase = (0, grab_required_database_schemas_1.grabPrimaryRequiredDbSchema)({
dbId: currentTableSchema.childTableDbId,
userId,
});
const targetParentDatabaseTableIndex = targetParentDatabase === null || targetParentDatabase === void 0 ? void 0 : targetParentDatabase.tables.findIndex((tbl) => tbl.id == currentTableSchema.childTableId);
const targetParentDatabaseTable = typeof targetParentDatabaseTableIndex == "number"
? targetParentDatabaseTableIndex < 0
? undefined
: targetParentDatabase === null || targetParentDatabase === void 0 ? void 0 : targetParentDatabase.tables[targetParentDatabaseTableIndex]
: undefined;
/**
* Delete child Table key/values from current database if
* the parent database doesn't esit
*/
if (!(targetParentDatabase === null || targetParentDatabase === void 0 ? void 0 : targetParentDatabase.dbFullName) ||
!(targetParentDatabaseTable === null || targetParentDatabaseTable === void 0 ? void 0 : targetParentDatabaseTable.tableName)) {
delete newCurrentDbSchema.tables[currentTableSchemaIndex]
.childTable;
delete newCurrentDbSchema.tables[currentTableSchemaIndex]
.childTableDbId;
delete newCurrentDbSchema.tables[currentTableSchemaIndex]
.childTableId;
delete newCurrentDbSchema.tables[currentTableSchemaIndex]
.childTableDbId;
return newCurrentDbSchema;
}
/**
* New Child Database Table Object to be appended
*/
const newChildDatabaseTableObject = {
tableId: currentTableSchema.id,
dbId: newCurrentDbSchema.id,
};
/**
* Add a new Children array in the target table schema if this is the
* first child to be added to said table schema. Else append to array
* if it exists
*/
if (typeof targetParentDatabaseTableIndex == "number" &&
!((_e = targetParentDatabaseTable.childrenTables) === null || _e === void 0 ? void 0 : _e[0])) {
targetParentDatabase.tables[targetParentDatabaseTableIndex].childrenTables = [newChildDatabaseTableObject];
}
else if (typeof targetParentDatabaseTableIndex == "number" &&
((_f = targetParentDatabaseTable.childrenTables) === null || _f === void 0 ? void 0 : _f[0])) {
const existingChildDbTable = targetParentDatabaseTable.childrenTables.find((tbl) => tbl.dbId == newCurrentDbSchema.id &&
tbl.tableId == currentTableSchema.id);
if (!(existingChildDbTable === null || existingChildDbTable === void 0 ? void 0 : existingChildDbTable.tableId)) {
(_g = targetParentDatabase.tables[targetParentDatabaseTableIndex].childrenTables) === null || _g === void 0 ? void 0 : _g.push(newChildDatabaseTableObject);
}
targetParentDatabase.tables[targetParentDatabaseTableIndex].childrenTables = (0, unique_by_key_1.default)(targetParentDatabase.tables[targetParentDatabaseTableIndex]
.childrenTables || [], ["dbId", "tableId"]);
}
/**
* Update fields and indexes for child table, which is the
* current table
*/
if (targetParentDatabaseTable === null || targetParentDatabaseTable === void 0 ? void 0 : targetParentDatabaseTable.tableName) {
newCurrentDbSchema.tables[currentTableSchemaIndex].fields =
targetParentDatabaseTable.fields;
newCurrentDbSchema.tables[currentTableSchemaIndex].indexes =
targetParentDatabaseTable.indexes;
(0, grab_required_database_schemas_1.writeUpdatedDbSchema)({ dbSchema: targetParentDatabase, userId });
}
}
return newCurrentDbSchema;
}