This commit is contained in:
2025-12-27 16:06:23 +01:00
parent 27887ded41
commit 0dc8f3c787
14 changed files with 230 additions and 72 deletions
+4 -1
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) {
@@ -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;
}
}
@@ -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) => {
@@ -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,
});
}
}
+6 -4
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 },