This commit is contained in:
Benjamin Toby 2025-12-27 16:06:23 +01:00
parent 27887ded41
commit 0dc8f3c787
14 changed files with 230 additions and 72 deletions

View File

@ -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({

View File

@ -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 {};

View File

@ -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;
}
}

View File

@ -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, }: {

View File

@ -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)

View File

@ -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)({
? [
(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,
});
}
}

View File

@ -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<T extends {
*/
basePath?: string;
auth?: () => Promise<boolean>;
getMiddleware?: (params: {
query: APIPathsQuery<T>;
}) => Promise<APIPathsQuery<T>>;
getMiddleware?: APIPathsParamsGetMiddleware<T>;
postMiddleware?: APIPathsParamsCrudMiddleware<T>;
putMiddleware?: APIPathsParamsCrudMiddleware<T>;
deleteMiddleware?: APIPathsParamsCrudMiddleware<T>;
@ -2250,7 +2248,8 @@ export type APIPathsParamsGetMiddleware<T extends {
[k: string]: any;
}> = (params: {
query: APIPathsQuery<T>;
}) => Promise<DsqlCrudQueryObject<T>>;
table: string;
}) => Promise<APIPathsQuery<T>>;
export type APIPathsParamsCrudMiddleware<T extends {
[k: string]: any;
} = {

View File

@ -465,6 +465,7 @@ exports.OpsActions = [
"restart-db",
"restart-all",
"clear",
"clear-container-logs",
];
exports.AIOptions = [
{

View File

@ -70,7 +70,10 @@ export default async function checks<
}
if (method == "GET" && getMiddleware) {
newQuery = await getMiddleware({ query: newQuery || ({} as any) });
newQuery = await getMiddleware({
query: newQuery || ({} as any),
table,
});
}
if (method !== "GET" && crudMiddleware) {

View File

@ -0,0 +1,72 @@
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;
}
}

View File

@ -13,6 +13,11 @@ type Params = {
dbSlug?: string;
};
/**
* # Grab Database Schemas related to a database
* @param params
* @returns
*/
export default function grabRequiredDatabaseSchemas(
params: Params
): DSQL_DatabaseSchemaType[] | undefined {
@ -22,6 +27,7 @@ export default function grabRequiredDatabaseSchemas(
let relatedDatabases: DSQL_DatabaseSchemaType[] = [];
const childrenDatabases = primaryDbSchema.childrenDatabases || [];
const childrenTables =
primaryDbSchema.tables
.map((tbl) => {

View File

@ -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({
? [
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,
});
}
}

View File

@ -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<boolean>;
getMiddleware?: (params: {
query: APIPathsQuery<T>;
}) => Promise<APIPathsQuery<T>>;
getMiddleware?: APIPathsParamsGetMiddleware<T>;
postMiddleware?: APIPathsParamsCrudMiddleware<T>;
putMiddleware?: APIPathsParamsCrudMiddleware<T>;
deleteMiddleware?: APIPathsParamsCrudMiddleware<T>;
@ -2912,7 +2911,10 @@ export type APIPathsQuery<
export type APIPathsParamsGetMiddleware<
T extends { [k: string]: any } = { [k: string]: any }
> = (params: { query: APIPathsQuery<T> }) => Promise<DsqlCrudQueryObject<T>>;
> = (params: {
query: APIPathsQuery<T>;
table: string;
}) => Promise<APIPathsQuery<T>>;
export type APIPathsParamsCrudMiddleware<
T extends { [k: string]: any } = { [k: string]: any },

View File

@ -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": {