"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = grabRequiredDatabaseSchemas; exports.grabPrimaryRequiredDbSchema = grabPrimaryRequiredDbSchema; exports.findDbNameInSchemaDir = findDbNameInSchemaDir; exports.writeUpdatedDbSchema = writeUpdatedDbSchema; exports.deleteDbSchema = deleteDbSchema; exports.findTargetDbSchemaFromMainSchema = findTargetDbSchemaFromMainSchema; exports.grabLatestDbSchemaID = grabLatestDbSchemaID; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const grab_dir_names_1 = __importDefault(require("../../utils/backend/names/grab-dir-names")); const ejson_1 = __importDefault(require("../../utils/ejson")); const numberfy_1 = __importDefault(require("../../utils/numberfy")); const unique_by_key_1 = __importDefault(require("../../utils/unique-by-key")); function grabRequiredDatabaseSchemas(params) { const primaryDbSchema = grabPrimaryRequiredDbSchema(params); if (!primaryDbSchema) return undefined; let relatedDatabases = []; const childrenDatabases = primaryDbSchema.childrenDatabases || []; const childrenTables = primaryDbSchema.tables .map((tbl) => { return tbl.childrenTables || []; }) .flat() || []; for (let i = 0; i < childrenDatabases.length; i++) { const childDb = childrenDatabases[i]; const childDbSchema = grabPrimaryRequiredDbSchema({ userId: params.userId, dbId: childDb.dbId, }); if (!(childDbSchema === null || childDbSchema === void 0 ? void 0 : childDbSchema.dbSlug)) continue; relatedDatabases.push(childDbSchema); } for (let i = 0; i < childrenTables.length; i++) { const childTbl = childrenTables[i]; const childTableDbSchema = grabPrimaryRequiredDbSchema({ userId: params.userId, dbId: childTbl.dbId, }); if (!(childTableDbSchema === null || childTableDbSchema === void 0 ? void 0 : childTableDbSchema.dbSlug)) continue; relatedDatabases.push(childTableDbSchema); } return (0, unique_by_key_1.default)([primaryDbSchema, ...relatedDatabases], "dbFullName"); } function grabPrimaryRequiredDbSchema({ userId, dbId, dbSlug }) { let finalDbId = dbId; if (!finalDbId && userId && dbSlug) { const searchedDb = findDbNameInSchemaDir({ dbName: dbSlug, userId }); if (searchedDb === null || searchedDb === void 0 ? void 0 : searchedDb.id) { finalDbId = searchedDb.id; } } if (!finalDbId) { return undefined; } const { targetUserPrivateDir, oldSchemasDir } = (0, grab_dir_names_1.default)({ userId, }); const finalSchemaDir = targetUserPrivateDir || oldSchemasDir; if (!finalSchemaDir) { console.log(`finalSchemaDir not found!`); return undefined; } if (finalDbId) { const dbIdSchema = path_1.default.resolve(finalSchemaDir, `${finalDbId}.json`); if (fs_1.default.existsSync(dbIdSchema)) { const dbIdSchemaObject = ejson_1.default.parse(fs_1.default.readFileSync(dbIdSchema, "utf-8")); return dbIdSchemaObject; } } const dbSchemasFiles = fs_1.default.readdirSync(finalSchemaDir); let targetDbSchema; try { for (let i = 0; i < dbSchemasFiles.length; i++) { const fileOrPath = dbSchemasFiles[i]; if (!fileOrPath.endsWith(`.json`)) continue; if (!fileOrPath.match(/^\d+.json/)) continue; const targetFileJSONPath = path_1.default.join(finalSchemaDir, fileOrPath); const targetSchema = ejson_1.default.parse(fs_1.default.readFileSync(targetFileJSONPath, "utf-8")); if (targetSchema && finalDbId && (targetSchema === null || targetSchema === void 0 ? void 0 : targetSchema.id) == finalDbId) { targetDbSchema = targetSchema; } } } catch (error) { } if (targetDbSchema) { return targetDbSchema; } // else if ( dbFullName) { // let existingSchemaInMainJSON = findTargetDbSchemaFromMainSchema( // dbFullName // ); // const nextID = grabLatestDbSchemaID(finalSchemaDir); // if (existingSchemaInMainJSON) { // existingSchemaInMainJSON.id = nextID; // fs.writeFileSync( // path.join(finalSchemaDir, `${nextID}.json`), // EJSON.stringify(existingSchemaInMainJSON) || "[]" // ); // return existingSchemaInMainJSON; // } // } console.log(`userSchemaDir not found!`); console.log(`userId`, userId); return undefined; } function findDbNameInSchemaDir({ userId, dbName, }) { if (!userId) { console.log(`userId not provided!`); return undefined; } if (!dbName) { console.log(`dbName not provided!`); return undefined; } const { targetUserPrivateDir } = (0, grab_dir_names_1.default)({ userId, }); if (!targetUserPrivateDir) { console.log(`targetUserPrivateDir not found!`); return undefined; } const dbSchemasFiles = fs_1.default.readdirSync(targetUserPrivateDir); let targetDbSchema; try { for (let i = 0; i < dbSchemasFiles.length; i++) { const fileOrPath = dbSchemasFiles[i]; if (!fileOrPath.endsWith(`.json`)) continue; if (!fileOrPath.match(/^\d+.json/)) continue; const targetFileJSONPath = path_1.default.join(targetUserPrivateDir, fileOrPath); const targetSchema = ejson_1.default.parse(fs_1.default.readFileSync(targetFileJSONPath, "utf-8")); if (!targetSchema) continue; if (targetSchema.dbFullName == dbName || targetSchema.dbSlug == dbName) { targetDbSchema = targetSchema; return targetSchema; } } } catch (error) { } return targetDbSchema; } function writeUpdatedDbSchema({ dbSchema, userId, }) { const { targetUserPrivateDir } = (0, grab_dir_names_1.default)({ userId, }); if (!targetUserPrivateDir) { console.log(`user ${userId} has no targetUserPrivateDir`); return {}; } if (dbSchema.id) { const dbIdSchemaPath = path_1.default.join(targetUserPrivateDir, `${dbSchema.id}.json`); fs_1.default.writeFileSync(dbIdSchemaPath, ejson_1.default.stringify(dbSchema) || "[]"); return { success: true }; } else { const nextID = grabLatestDbSchemaID(targetUserPrivateDir); dbSchema.id = nextID; fs_1.default.writeFileSync(path_1.default.join(targetUserPrivateDir, `${nextID}.json`), ejson_1.default.stringify(dbSchema) || "[]"); return { success: true, dbSchemaId: nextID }; } } function deleteDbSchema({ dbSchema, userId }) { const { targetUserPrivateDir, userSchemaMainJSONFilePath } = (0, grab_dir_names_1.default)({ userId, }); if (!targetUserPrivateDir) return; const targetDbSchema = grabPrimaryRequiredDbSchema({ dbId: dbSchema.id, userId, }); const schemaFile = path_1.default.join(targetUserPrivateDir, `${targetDbSchema === null || targetDbSchema === void 0 ? void 0 : targetDbSchema.id}.json`); try { fs_1.default.unlinkSync(schemaFile); } catch (error) { } if (userSchemaMainJSONFilePath && fs_1.default.existsSync(userSchemaMainJSONFilePath)) { try { let allDbSchemas = ejson_1.default.parse(fs_1.default.readFileSync(userSchemaMainJSONFilePath, "utf-8")); if (allDbSchemas === null || allDbSchemas === void 0 ? void 0 : allDbSchemas[0]) { for (let i = 0; i < allDbSchemas.length; i++) { const dbSch = allDbSchemas[i]; if (dbSch.dbFullName == dbSchema.dbFullName || dbSch.id == dbSchema.id) { allDbSchemas.splice(i, 1); } } fs_1.default.writeFileSync(userSchemaMainJSONFilePath, ejson_1.default.stringify(allDbSchemas) || "[]"); } } catch (error) { } } } function findTargetDbSchemaFromMainSchema(schemas, dbFullName, dbId) { const targetDbSchema = schemas.find((sch) => sch.dbFullName == dbFullName || (dbId && sch.id == dbId)); return targetDbSchema; } function grabLatestDbSchemaID(userSchemaDir) { const dbSchemasFiles = fs_1.default.readdirSync(userSchemaDir); const dbNumbers = dbSchemasFiles .filter((dbSch) => { if (!dbSch.endsWith(`.json`)) return false; if (dbSch.match(/^\d+\.json/)) return true; return false; }) .map((dbSch) => (0, numberfy_1.default)(dbSch.replace(/[^0-9]/g, ""))); if (dbNumbers[0]) return ((dbNumbers .sort((a, b) => { return a - b; }) .pop() || 0) + 1); return 1; }