diff --git a/dist/package-shared/api-paths/utils/checks.js b/dist/package-shared/api-paths/utils/checks.js index a87e383..64fbd37 100644 --- a/dist/package-shared/api-paths/utils/checks.js +++ b/dist/package-shared/api-paths/utils/checks.js @@ -51,7 +51,10 @@ function checks(_a) { } } if (method == "GET" && getMiddleware) { - newQuery = yield getMiddleware({ query: newQuery || {} }); + newQuery = yield getMiddleware({ + query: newQuery || {}, + table, + }); } if (method !== "GET" && crudMiddleware) { const middRes = yield crudMiddleware({ diff --git a/dist/package-shared/shell/createDbFromSchema/grab-all-user-db-schemas.d.ts b/dist/package-shared/shell/createDbFromSchema/grab-all-user-db-schemas.d.ts new file mode 100644 index 0000000..3fd613d --- /dev/null +++ b/dist/package-shared/shell/createDbFromSchema/grab-all-user-db-schemas.d.ts @@ -0,0 +1,13 @@ +import { DSQL_DatabaseSchemaType } from "../../types"; +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; +export {}; diff --git a/dist/package-shared/shell/createDbFromSchema/grab-all-user-db-schemas.js b/dist/package-shared/shell/createDbFromSchema/grab-all-user-db-schemas.js new file mode 100644 index 0000000..7e140ed --- /dev/null +++ b/dist/package-shared/shell/createDbFromSchema/grab-all-user-db-schemas.js @@ -0,0 +1,54 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.default = grabAllDbSchemas; +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")); +/** + * # Grab All Database Schemas from Directory + * @param params + * @returns + */ +function grabAllDbSchemas({ userId, parentDbId, parentTableId, }) { + try { + let dbSchemas = []; + 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); + for (let i = 0; i < dbSchemasFiles.length; i++) { + try { + const dbSchemaFile = dbSchemasFiles[i]; + const fullPath = path_1.default.join(targetUserPrivateDir, dbSchemaFile); + const json = fs_1.default.readFileSync(fullPath, "utf-8"); + const dbSchema = ejson_1.default.parse(json); + if (!dbSchema) { + continue; + } + if (parentDbId) { + const isDbChild = (0, numberfy_1.default)(dbSchema.childDatabaseDbId) == + (0, numberfy_1.default)(parentDbId); + if (isDbChild) { + dbSchemas.push(dbSchema); + } + continue; + } + dbSchemas.push(dbSchema); + } + catch (error) { } + } + return dbSchemas; + } + catch (error) { + return undefined; + } +} diff --git a/dist/package-shared/shell/createDbFromSchema/grab-required-database-schemas.d.ts b/dist/package-shared/shell/createDbFromSchema/grab-required-database-schemas.d.ts index 4b2f018..b7f75c7 100644 --- a/dist/package-shared/shell/createDbFromSchema/grab-required-database-schemas.d.ts +++ b/dist/package-shared/shell/createDbFromSchema/grab-required-database-schemas.d.ts @@ -4,6 +4,11 @@ type Params = { dbId?: string | number; dbSlug?: string; }; +/** + * # Grab Database Schemas related to a database + * @param params + * @returns + */ export default function grabRequiredDatabaseSchemas(params: Params): DSQL_DatabaseSchemaType[] | undefined; export declare function grabPrimaryRequiredDbSchema({ userId, dbId, dbSlug }: Params): DSQL_DatabaseSchemaType | undefined; export declare function findDbNameInSchemaDir({ userId, dbName, }: { diff --git a/dist/package-shared/shell/createDbFromSchema/grab-required-database-schemas.js b/dist/package-shared/shell/createDbFromSchema/grab-required-database-schemas.js index dc5414c..d6ede13 100644 --- a/dist/package-shared/shell/createDbFromSchema/grab-required-database-schemas.js +++ b/dist/package-shared/shell/createDbFromSchema/grab-required-database-schemas.js @@ -16,6 +16,11 @@ const grab_dir_names_1 = __importDefault(require("../../utils/backend/names/grab 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")); +/** + * # Grab Database Schemas related to a database + * @param params + * @returns + */ function grabRequiredDatabaseSchemas(params) { const primaryDbSchema = grabPrimaryRequiredDbSchema(params); if (!primaryDbSchema) diff --git a/dist/package-shared/shell/createDbFromSchema/index.js b/dist/package-shared/shell/createDbFromSchema/index.js index 541f1af..5874c25 100644 --- a/dist/package-shared/shell/createDbFromSchema/index.js +++ b/dist/package-shared/shell/createDbFromSchema/index.js @@ -1,37 +1,4 @@ "use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - var desc = Object.getOwnPropertyDescriptor(m, k); - if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { - desc = { enumerable: true, get: function() { return m[k]; } }; - } - Object.defineProperty(o, k2, desc); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { - Object.defineProperty(o, "default", { enumerable: true, value: v }); -}) : function(o, v) { - o["default"] = v; -}); -var __importStar = (this && this.__importStar) || (function () { - var ownKeys = function(o) { - ownKeys = Object.getOwnPropertyNames || function (o) { - var ar = []; - for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; - return ar; - }; - return ownKeys(o); - }; - return function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); - __setModuleDefault(result, mod); - return result; - }; -})(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { @@ -51,8 +18,9 @@ const updateTable_1 = __importDefault(require("../utils/updateTable")); const grab_dir_names_1 = __importDefault(require("../../utils/backend/names/grab-dir-names")); const check_db_record_1 = __importDefault(require("./check-db-record")); const handle_indexes_1 = __importDefault(require("./handle-indexes")); -const grab_required_database_schemas_1 = __importStar(require("./grab-required-database-schemas")); +const grab_required_database_schemas_1 = require("./grab-required-database-schemas"); const dbHandler_1 = __importDefault(require("../../functions/backend/dbHandler")); +const grab_all_user_db_schemas_1 = __importDefault(require("./grab-all-user-db-schemas")); /** * # Create database from Schema Function * @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database @@ -67,13 +35,15 @@ function createDbFromSchema(_a) { let dbSchema = dbSchemaData ? dbSchemaData : dbId - ? (0, grab_required_database_schemas_1.default)({ - dbId, - userId, - }) + ? [ + (0, grab_required_database_schemas_1.grabPrimaryRequiredDbSchema)({ + dbId, + userId, + }), + ] : undefined; if (!dbSchema) { - console.log("Schema Not Found!"); + console.log(`Schema Not Found for DB ID ${dbId}!`); return false; } // fs.writeFileSync( @@ -83,7 +53,9 @@ function createDbFromSchema(_a) { const isMain = !userSchemaMainJSONFilePath; for (let i = 0; i < dbSchema.length; i++) { const database = dbSchema[i]; - const { dbFullName, tables, dbSlug, childrenDatabases } = database; + if (!database) + continue; + const { dbFullName, tables, dbSlug } = database; if (!dbFullName) continue; if (targetDatabase && dbFullName != targetDatabase) { @@ -225,15 +197,23 @@ function createDbFromSchema(_a) { /** * @description Check all children databases */ - if (childrenDatabases === null || childrenDatabases === void 0 ? void 0 : childrenDatabases[0]) { + const childrenDatabases = userId + ? (0, grab_all_user_db_schemas_1.default)({ userId, parentDbId: database.id }) || [] + : []; + if ((childrenDatabases === null || childrenDatabases === void 0 ? void 0 : childrenDatabases[0]) && userId) { for (let ch = 0; ch < childrenDatabases.length; ch++) { const childDb = childrenDatabases[ch]; - const { dbId } = childDb; - const targetDatabase = dbSchema.find((dbSch) => dbSch.childDatabaseDbId == dbId); - if (targetDatabase === null || targetDatabase === void 0 ? void 0 : targetDatabase.id) { + childDb.tables = [...database.tables]; + childDb.collation = database.collation; + (0, grab_required_database_schemas_1.writeUpdatedDbSchema)({ + dbSchema: childDb, + userId, + }); + if (dbId) { yield createDbFromSchema({ userId, - dbId: targetDatabase === null || targetDatabase === void 0 ? void 0 : targetDatabase.id, + dbId: childDb.id, + targetTable, }); } } diff --git a/dist/package-shared/types/index.d.ts b/dist/package-shared/types/index.d.ts index f79dd6c..a569f54 100644 --- a/dist/package-shared/types/index.d.ts +++ b/dist/package-shared/types/index.d.ts @@ -2138,7 +2138,7 @@ export interface GiteaTreeResTree { sha: string; url: string; } -export declare const OpsActions: readonly ["exit", "test", "restart-web-app", "restart-web-app-container", "restart-db", "restart-all", "clear"]; +export declare const OpsActions: readonly ["exit", "test", "restart-web-app", "restart-web-app-container", "restart-db", "restart-all", "clear", "clear-container-logs"]; export type OpsObject = { action: (typeof OpsActions)[number]; }; @@ -2210,9 +2210,7 @@ export type APIPathsParams Promise; - getMiddleware?: (params: { - query: APIPathsQuery; - }) => Promise>; + getMiddleware?: APIPathsParamsGetMiddleware; postMiddleware?: APIPathsParamsCrudMiddleware; putMiddleware?: APIPathsParamsCrudMiddleware; deleteMiddleware?: APIPathsParamsCrudMiddleware; @@ -2250,7 +2248,8 @@ export type APIPathsParamsGetMiddleware = (params: { query: APIPathsQuery; -}) => Promise>; + table: string; +}) => Promise>; export type APIPathsParamsCrudMiddleware { diff --git a/package-shared/shell/createDbFromSchema/index.ts b/package-shared/shell/createDbFromSchema/index.ts index f13b3cc..70c5ca1 100644 --- a/package-shared/shell/createDbFromSchema/index.ts +++ b/package-shared/shell/createDbFromSchema/index.ts @@ -4,10 +4,13 @@ import { DSQL_DatabaseSchemaType } from "../../types"; import grabDirNames from "../../utils/backend/names/grab-dir-names"; import checkDbRecordCreateDbSchema from "./check-db-record"; import handleIndexescreateDbFromSchema from "./handle-indexes"; -import grabRequiredDatabaseSchemas, { +import { grabPrimaryRequiredDbSchema, + writeUpdatedDbSchema, } from "./grab-required-database-schemas"; import dbHandler from "../../functions/backend/dbHandler"; +import grabAllDbSchemas from "./grab-all-user-db-schemas"; +import _ from "lodash"; type Param = { userId?: number | string | null; @@ -36,14 +39,16 @@ export default async function createDbFromSchema({ let dbSchema = dbSchemaData ? dbSchemaData : dbId - ? grabRequiredDatabaseSchemas({ - dbId, - userId, - }) + ? [ + grabPrimaryRequiredDbSchema({ + dbId, + userId, + }), + ] : undefined; if (!dbSchema) { - console.log("Schema Not Found!"); + console.log(`Schema Not Found for DB ID ${dbId}!`); return false; } @@ -55,9 +60,11 @@ export default async function createDbFromSchema({ const isMain = !userSchemaMainJSONFilePath; for (let i = 0; i < dbSchema.length; i++) { - const database: DSQL_DatabaseSchemaType = dbSchema[i]; + const database: DSQL_DatabaseSchemaType | undefined = dbSchema[i]; - const { dbFullName, tables, dbSlug, childrenDatabases } = database; + if (!database) continue; + + const { dbFullName, tables, dbSlug } = database; if (!dbFullName) continue; @@ -233,19 +240,27 @@ export default async function createDbFromSchema({ /** * @description Check all children databases */ - if (childrenDatabases?.[0]) { + const childrenDatabases: DSQL_DatabaseSchemaType[] = userId + ? grabAllDbSchemas({ userId, parentDbId: database.id }) || [] + : []; + + if (childrenDatabases?.[0] && userId) { for (let ch = 0; ch < childrenDatabases.length; ch++) { const childDb = childrenDatabases[ch]; - const { dbId } = childDb; - const targetDatabase = dbSchema.find( - (dbSch) => dbSch.childDatabaseDbId == dbId - ); + childDb.tables = [...database.tables]; + childDb.collation = database.collation; - if (targetDatabase?.id) { + writeUpdatedDbSchema({ + dbSchema: childDb, + userId, + }); + + if (dbId) { await createDbFromSchema({ userId, - dbId: targetDatabase?.id, + dbId: childDb.id, + targetTable, }); } } diff --git a/package-shared/types/index.ts b/package-shared/types/index.ts index a8d08d2..3af26c1 100644 --- a/package-shared/types/index.ts +++ b/package-shared/types/index.ts @@ -2741,6 +2741,7 @@ export const OpsActions = [ "restart-db", "restart-all", "clear", + "clear-container-logs", ] as const; export type OpsObject = { @@ -2869,9 +2870,7 @@ export type APIPathsParams< */ basePath?: string; auth?: () => Promise; - getMiddleware?: (params: { - query: APIPathsQuery; - }) => Promise>; + getMiddleware?: APIPathsParamsGetMiddleware; postMiddleware?: APIPathsParamsCrudMiddleware; putMiddleware?: APIPathsParamsCrudMiddleware; deleteMiddleware?: APIPathsParamsCrudMiddleware; @@ -2912,7 +2911,10 @@ export type APIPathsQuery< export type APIPathsParamsGetMiddleware< T extends { [k: string]: any } = { [k: string]: any } -> = (params: { query: APIPathsQuery }) => Promise>; +> = (params: { + query: APIPathsQuery; + table: string; +}) => Promise>; export type APIPathsParamsCrudMiddleware< T extends { [k: string]: any } = { [k: string]: any }, diff --git a/package.json b/package.json index 7495c93..9af8b23 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/datasquirel", - "version": "5.6.4", + "version": "5.6.5", "description": "Cloud-based SQL data management tool", "main": "dist/index.js", "bin": {