133 lines
4.1 KiB
TypeScript
133 lines
4.1 KiB
TypeScript
import {
|
|
grabPrimaryRequiredDbSchema,
|
|
writeUpdatedDbSchema,
|
|
} from "../../../shell/createDbFromSchema/grab-required-database-schemas";
|
|
import {
|
|
DSQL_ChildrenDatabaseObject,
|
|
DSQL_DatabaseSchemaType,
|
|
} from "../../../types";
|
|
import _ from "lodash";
|
|
import uniqueByKey from "../../unique-by-key";
|
|
|
|
type Params = {
|
|
currentDbSchema: DSQL_DatabaseSchemaType;
|
|
userId: string | number;
|
|
};
|
|
|
|
export default function ({ currentDbSchema, userId }: Params) {
|
|
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?.id && targetChildDatabase.childDatabase) {
|
|
targetChildDatabase.tables = [...newCurrentDbSchema.tables];
|
|
writeUpdatedDbSchema({
|
|
dbSchema: targetChildDatabase,
|
|
userId,
|
|
});
|
|
} else {
|
|
newCurrentDbSchema.childrenDatabases?.splice(ch, 1, {});
|
|
}
|
|
}
|
|
|
|
newCurrentDbSchema.childrenDatabases =
|
|
uniqueByKey<DSQL_ChildrenDatabaseObject>(
|
|
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?.id) {
|
|
delete newCurrentDbSchema.childDatabase;
|
|
delete newCurrentDbSchema.childDatabaseDbId;
|
|
return newCurrentDbSchema;
|
|
}
|
|
|
|
/**
|
|
* New Child Database Object to be appended
|
|
*/
|
|
const newChildDatabaseObject: DSQL_ChildrenDatabaseObject = {
|
|
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?.id &&
|
|
!targetParentDatabase.childrenDatabases?.[0]
|
|
) {
|
|
targetParentDatabase.childrenDatabases = [newChildDatabaseObject];
|
|
} else if (
|
|
targetParentDatabase?.id &&
|
|
targetParentDatabase.childrenDatabases?.[0]
|
|
) {
|
|
const existingChildDb = targetParentDatabase.childrenDatabases.find(
|
|
(db) => db.dbId == currentDbSchema.id
|
|
);
|
|
|
|
if (!existingChildDb?.dbId) {
|
|
targetParentDatabase.childrenDatabases.push(
|
|
newChildDatabaseObject
|
|
);
|
|
}
|
|
|
|
targetParentDatabase.childrenDatabases = uniqueByKey(
|
|
targetParentDatabase.childrenDatabases,
|
|
"dbId"
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Update tables for child database, which is the current database
|
|
*/
|
|
if (targetParentDatabase?.id) {
|
|
newCurrentDbSchema.tables = targetParentDatabase.tables;
|
|
writeUpdatedDbSchema({ dbSchema: targetParentDatabase, userId });
|
|
}
|
|
}
|
|
|
|
return newCurrentDbSchema;
|
|
}
|