import fs from "fs"; import grabDirNames from "../../backend/names/grab-dir-names"; import _n from "../../numberfy"; import path from "path"; import _ from "lodash"; import EJSON from "../../ejson"; import { writeUpdatedDbSchema } from "../../../shell/createDbFromSchema/grab-required-database-schemas"; export default function resolveUsersSchemaIDs({ userId, dbId }) { const { targetUserPrivateDir, tempDirName } = grabDirNames({ userId }); if (!targetUserPrivateDir) return false; const schemaDirFilesFolders = fs.readdirSync(targetUserPrivateDir); for (let i = 0; i < schemaDirFilesFolders.length; i++) { const fileOrFolderName = schemaDirFilesFolders[i]; if (!fileOrFolderName.match(/^\d+.json/)) continue; const fileDbId = _n(fileOrFolderName.split(".").shift()); if (!fileDbId) continue; if (dbId && _n(dbId) !== fileDbId) { continue; } const schemaFullPath = path.join(targetUserPrivateDir, fileOrFolderName); if (!fs.existsSync(schemaFullPath)) continue; const dbSchema = EJSON.parse(fs.readFileSync(schemaFullPath, "utf-8")); if (!dbSchema) continue; let newDbSchema = resolveUserDatabaseSchemaIDs({ dbSchema }); writeUpdatedDbSchema({ dbSchema: newDbSchema, userId }); } } export function resolveUserDatabaseSchemaIDs({ dbSchema, }) { let newDbSchema = _.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; }