95 lines
4.6 KiB
JavaScript
95 lines
4.6 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, userId }) {
|
|
var _a, _b, _c;
|
|
const newCurrentDbSchema = lodash_1.default.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 = (0, grab_required_database_schemas_1.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];
|
|
(0, grab_required_database_schemas_1.writeUpdatedDbSchema)({
|
|
dbSchema: targetChildDatabase,
|
|
userId,
|
|
});
|
|
}
|
|
else {
|
|
(_a = newCurrentDbSchema.childrenDatabases) === null || _a === void 0 ? void 0 : _a.splice(ch, 1, {});
|
|
}
|
|
}
|
|
newCurrentDbSchema.childrenDatabases =
|
|
(0, unique_by_key_1.default)(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 = (0, grab_required_database_schemas_1.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 = (0, unique_by_key_1.default)(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;
|
|
(0, grab_required_database_schemas_1.writeUpdatedDbSchema)({ dbSchema: targetParentDatabase, userId });
|
|
}
|
|
}
|
|
return newCurrentDbSchema;
|
|
}
|