"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = resolveUsersSchemaIDs; exports.resolveUserDatabaseSchemaIDs = resolveUserDatabaseSchemaIDs; const fs_1 = __importDefault(require("fs")); const grab_dir_names_1 = __importDefault(require("../../backend/names/grab-dir-names")); const numberfy_1 = __importDefault(require("../../numberfy")); const path_1 = __importDefault(require("path")); const lodash_1 = __importDefault(require("lodash")); const ejson_1 = __importDefault(require("../../ejson")); const grab_required_database_schemas_1 = require("../../../shell/createDbFromSchema/grab-required-database-schemas"); function resolveUsersSchemaIDs({ userId, dbId }) { const { targetUserPrivateDir, tempDirName } = (0, grab_dir_names_1.default)({ userId }); if (!targetUserPrivateDir) return false; const schemaDirFilesFolders = fs_1.default.readdirSync(targetUserPrivateDir); for (let i = 0; i < schemaDirFilesFolders.length; i++) { const fileOrFolderName = schemaDirFilesFolders[i]; if (!fileOrFolderName.match(/^\d+.json/)) continue; const fileDbId = (0, numberfy_1.default)(fileOrFolderName.split(".").shift()); if (!fileDbId) continue; if (dbId && (0, numberfy_1.default)(dbId) !== fileDbId) { continue; } const schemaFullPath = path_1.default.join(targetUserPrivateDir, fileOrFolderName); if (!fs_1.default.existsSync(schemaFullPath)) continue; const dbSchema = ejson_1.default.parse(fs_1.default.readFileSync(schemaFullPath, "utf-8")); if (!dbSchema) continue; let newDbSchema = resolveUserDatabaseSchemaIDs({ dbSchema }); (0, grab_required_database_schemas_1.writeUpdatedDbSchema)({ dbSchema: newDbSchema, userId }); } } function resolveUserDatabaseSchemaIDs({ dbSchema, }) { let newDbSchema = lodash_1.default.cloneDeep(dbSchema); if (!newDbSchema.id) newDbSchema.id = dbSchema.id; newDbSchema.tables.forEach((tbl, index) => { var _a; if (!tbl.id) { newDbSchema.tables[index].id = index + 1; } tbl.fields.forEach((fld, flIndx) => { if (!fld.id) { newDbSchema.tables[index].fields[flIndx].id = flIndx + 1; } }); (_a = tbl.indexes) === null || _a === void 0 ? void 0 : _a.forEach((indx, indIndx) => { if (!indx.id && newDbSchema.tables[index].indexes) { newDbSchema.tables[index].indexes[indIndx].id = indIndx + 1; } }); }); return newDbSchema; }