datasquirel/dist/package-shared/utils/db/schema/resolve-schema-children-handle-children-databases.js
Benjamin Toby 7e8bb37c09 Updates
2025-07-05 14:59:30 +01:00

89 lines
4.1 KiB
JavaScript

import { grabPrimaryRequiredDbSchema, writeUpdatedDbSchema, } from "../../../shell/createDbFromSchema/grab-required-database-schemas";
import _ from "lodash";
import uniqueByKey from "../../unique-by-key";
export default function ({ currentDbSchema, userId }) {
var _a, _b, _c;
const newCurrentDbSchema = _.cloneDeep(currentDbSchema);
if (newCurrentDbSchema.childrenDatabases) {
for (let ch = 0; ch < newCurrentDbSchema.childrenDatabases.length; ch++) {
const dbChildDb = newCurrentDbSchema.childrenDatabases[ch];
if (!dbChildDb.dbId) {
newCurrentDbSchema.childrenDatabases.splice(ch, 1, {});
continue;
}
const targetChildDatabase = grabPrimaryRequiredDbSchema({
dbId: dbChildDb.dbId,
userId,
});
/**
* Delete child database from array if said database
* doesn't exist
*/
if ((targetChildDatabase === null || targetChildDatabase === void 0 ? void 0 : targetChildDatabase.id) && targetChildDatabase.childDatabase) {
targetChildDatabase.tables = [...newCurrentDbSchema.tables];
writeUpdatedDbSchema({
dbSchema: targetChildDatabase,
userId,
});
}
else {
(_a = newCurrentDbSchema.childrenDatabases) === null || _a === void 0 ? void 0 : _a.splice(ch, 1, {});
}
}
newCurrentDbSchema.childrenDatabases =
uniqueByKey(newCurrentDbSchema.childrenDatabases.filter((db) => Boolean(db.dbId)), "dbId");
}
/**
* Handle scenario where this database is a child of another
*/
if (currentDbSchema.childDatabase && currentDbSchema.childDatabaseDbId) {
const targetParentDatabase = grabPrimaryRequiredDbSchema({
dbId: currentDbSchema.childDatabaseDbId,
userId,
});
if (!targetParentDatabase) {
return newCurrentDbSchema;
}
/**
* Delete child Database key/values from current database if
* the parent database doesn't esit
*/
if (!(targetParentDatabase === null || targetParentDatabase === void 0 ? void 0 : targetParentDatabase.id)) {
delete newCurrentDbSchema.childDatabase;
delete newCurrentDbSchema.childDatabaseDbId;
return newCurrentDbSchema;
}
/**
* New Child Database Object to be appended
*/
const newChildDatabaseObject = {
dbId: currentDbSchema.id,
};
/**
* Add a new Children array in the target Database if this is the
* first child to be added to said database. Else append to array
* if it exists
*/
if ((targetParentDatabase === null || targetParentDatabase === void 0 ? void 0 : targetParentDatabase.id) &&
!((_b = targetParentDatabase.childrenDatabases) === null || _b === void 0 ? void 0 : _b[0])) {
targetParentDatabase.childrenDatabases = [newChildDatabaseObject];
}
else if ((targetParentDatabase === null || targetParentDatabase === void 0 ? void 0 : targetParentDatabase.id) &&
((_c = targetParentDatabase.childrenDatabases) === null || _c === void 0 ? void 0 : _c[0])) {
const existingChildDb = targetParentDatabase.childrenDatabases.find((db) => db.dbId == currentDbSchema.id);
if (!(existingChildDb === null || existingChildDb === void 0 ? void 0 : existingChildDb.dbId)) {
targetParentDatabase.childrenDatabases.push(newChildDatabaseObject);
}
targetParentDatabase.childrenDatabases = uniqueByKey(targetParentDatabase.childrenDatabases, "dbId");
}
/**
* Update tables for child database, which is the current database
*/
if (targetParentDatabase === null || targetParentDatabase === void 0 ? void 0 : targetParentDatabase.id) {
newCurrentDbSchema.tables = targetParentDatabase.tables;
writeUpdatedDbSchema({ dbSchema: targetParentDatabase, userId });
}
}
return newCurrentDbSchema;
}