Updates
This commit is contained in:
@@ -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 {};
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
+5
@@ -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, }: {
|
||||
|
||||
+5
@@ -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)
|
||||
|
||||
+25
-45
@@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user