import fs from "fs"; import path from "path"; import grabDirNames from "../../utils/backend/names/grab-dir-names"; import EJSON from "../../utils/ejson"; import { DSQL_DatabaseSchemaType } from "../../types"; import numberfy from "../../utils/numberfy"; import _ from "lodash"; import uniqueByKey from "../../utils/unique-by-key"; type Params = { userId: string | number | null; parentDbId?: number | string | null; parentTableId?: number | string | null; }; /** * # Grab All Database Schemas from Directory * @param params * @returns */ export default function grabAllDbSchemas({ userId, parentDbId, parentTableId, }: Params): DSQL_DatabaseSchemaType[] | undefined { try { let dbSchemas: DSQL_DatabaseSchemaType[] = []; const { targetUserPrivateDir } = grabDirNames({ userId, }); if (!targetUserPrivateDir) { console.log(`targetUserPrivateDir not found!`); return undefined; } const dbSchemasFiles = fs.readdirSync(targetUserPrivateDir); for (let i = 0; i < dbSchemasFiles.length; i++) { try { const dbSchemaFile = dbSchemasFiles[i]; const fullPath = path.join(targetUserPrivateDir, dbSchemaFile); const json = fs.readFileSync(fullPath, "utf-8"); const dbSchema = EJSON.parse(json) as | DSQL_DatabaseSchemaType | undefined; if (!dbSchema) { continue; } if (parentDbId) { const isDbChild = numberfy(dbSchema.childDatabaseDbId) == numberfy(parentDbId); if (isDbChild) { dbSchemas.push(dbSchema); } continue; } dbSchemas.push(dbSchema); } catch (error) {} } return dbSchemas; } catch (error) { return undefined; } }