Updates
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
import { DSQL_DatabaseSchemaType } from "../../types";
|
||||
import { DSQL_DATASQUIREL_USER_DATABASES } from "../../types/dsql";
|
||||
type Param = {
|
||||
userId?: number | string | null;
|
||||
dbSchema: DSQL_DatabaseSchemaType;
|
||||
};
|
||||
/**
|
||||
* # Create database from Schema Function
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
*/
|
||||
export default function checkDbRecordCreateDbSchema({ userId, dbSchema, }: Param): Promise<DSQL_DATASQUIREL_USER_DATABASES | undefined>;
|
||||
export {};
|
||||
@@ -0,0 +1,64 @@
|
||||
"use strict";
|
||||
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) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = checkDbRecordCreateDbSchema;
|
||||
const varDatabaseDbHandler_1 = __importDefault(require("../utils/varDatabaseDbHandler"));
|
||||
const numberfy_1 = __importDefault(require("../../utils/numberfy"));
|
||||
const addDbEntry_1 = __importDefault(require("../../functions/backend/db/addDbEntry"));
|
||||
/**
|
||||
* # Create database from Schema Function
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
*/
|
||||
function checkDbRecordCreateDbSchema(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ userId, dbSchema, }) {
|
||||
try {
|
||||
const { dbFullName, dbName, dbSlug, dbDescription, dbImage, childDatabase, childDatabaseDbFullName, } = dbSchema;
|
||||
let recordedDbEntryArray = userId
|
||||
? yield (0, varDatabaseDbHandler_1.default)({
|
||||
queryString: `SELECT * FROM datasquirel.user_databases WHERE db_full_name = ?`,
|
||||
queryValuesArray: [dbFullName],
|
||||
})
|
||||
: undefined;
|
||||
let recordedDbEntry = recordedDbEntryArray === null || recordedDbEntryArray === void 0 ? void 0 : recordedDbEntryArray[0];
|
||||
if (!(recordedDbEntry === null || recordedDbEntry === void 0 ? void 0 : recordedDbEntry.id) && userId) {
|
||||
const newDbEntryObj = {
|
||||
user_id: (0, numberfy_1.default)(userId),
|
||||
db_name: dbName,
|
||||
db_slug: dbSlug,
|
||||
db_full_name: dbFullName,
|
||||
db_description: dbDescription,
|
||||
db_image: dbImage,
|
||||
active_clone: childDatabase ? 1 : undefined,
|
||||
active_clone_parent_db: childDatabaseDbFullName,
|
||||
};
|
||||
const newDbEntry = (yield (0, addDbEntry_1.default)({
|
||||
data: newDbEntryObj,
|
||||
tableName: "user_databases",
|
||||
forceLocal: true,
|
||||
}));
|
||||
if (newDbEntry.insertId) {
|
||||
recordedDbEntryArray = yield (0, varDatabaseDbHandler_1.default)({
|
||||
queryString: `SELECT * FROM datasquirel.user_databases WHERE db_full_name = ?`,
|
||||
queryValuesArray: [dbFullName],
|
||||
});
|
||||
recordedDbEntry = recordedDbEntryArray === null || recordedDbEntryArray === void 0 ? void 0 : recordedDbEntryArray[0];
|
||||
}
|
||||
}
|
||||
return recordedDbEntry;
|
||||
}
|
||||
catch (error) {
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { DSQL_DatabaseSchemaType, DSQL_TableSchemaType } from "../../types";
|
||||
import { DSQL_DATASQUIREL_USER_DATABASE_TABLES, DSQL_DATASQUIREL_USER_DATABASES } from "../../types/dsql";
|
||||
type Param = {
|
||||
userId?: number | string | null;
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
dbSchema: DSQL_DatabaseSchemaType[];
|
||||
dbRecord?: DSQL_DATASQUIREL_USER_DATABASES;
|
||||
dbFullName: string;
|
||||
};
|
||||
/**
|
||||
* # Create database from Schema Function
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
*/
|
||||
export default function checkTableRecordCreateDbSchema({ userId, tableSchema, dbSchema, dbRecord, dbFullName, }: Param): Promise<DSQL_DATASQUIREL_USER_DATABASE_TABLES | undefined>;
|
||||
export {};
|
||||
@@ -0,0 +1,92 @@
|
||||
"use strict";
|
||||
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) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = checkTableRecordCreateDbSchema;
|
||||
const varDatabaseDbHandler_1 = __importDefault(require("../utils/varDatabaseDbHandler"));
|
||||
const sql_generator_1 = __importDefault(require("../../functions/dsql/sql/sql-generator"));
|
||||
const numberfy_1 = __importDefault(require("../../utils/numberfy"));
|
||||
const addDbEntry_1 = __importDefault(require("../../functions/backend/db/addDbEntry"));
|
||||
/**
|
||||
* # Create database from Schema Function
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
*/
|
||||
function checkTableRecordCreateDbSchema(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ userId, tableSchema, dbSchema, dbRecord, dbFullName, }) {
|
||||
if (!tableSchema)
|
||||
return undefined;
|
||||
try {
|
||||
const queryObj = (0, sql_generator_1.default)({
|
||||
tableName: "user_database_tables",
|
||||
genObject: {
|
||||
query: {
|
||||
db_id: {
|
||||
value: String(dbRecord === null || dbRecord === void 0 ? void 0 : dbRecord.id),
|
||||
},
|
||||
table_slug: {
|
||||
value: tableSchema.tableName,
|
||||
},
|
||||
user_id: {
|
||||
value: String(userId),
|
||||
},
|
||||
},
|
||||
},
|
||||
dbFullName: "datasquirel",
|
||||
});
|
||||
let recordedTableEntryArray = userId
|
||||
? yield (0, varDatabaseDbHandler_1.default)({
|
||||
queryString: (queryObj === null || queryObj === void 0 ? void 0 : queryObj.string) || "",
|
||||
queryValuesArray: queryObj === null || queryObj === void 0 ? void 0 : queryObj.values,
|
||||
})
|
||||
: undefined;
|
||||
let recordedTableEntry = recordedTableEntryArray === null || recordedTableEntryArray === void 0 ? void 0 : recordedTableEntryArray[0];
|
||||
if (!(recordedTableEntry === null || recordedTableEntry === void 0 ? void 0 : recordedTableEntry.id) && userId) {
|
||||
const newTableInsertObject = {
|
||||
user_id: (0, numberfy_1.default)(userId),
|
||||
db_id: dbRecord === null || dbRecord === void 0 ? void 0 : dbRecord.id,
|
||||
db_slug: dbRecord === null || dbRecord === void 0 ? void 0 : dbRecord.db_slug,
|
||||
table_name: tableSchema.tableFullName,
|
||||
table_slug: tableSchema.tableName,
|
||||
};
|
||||
if ((tableSchema === null || tableSchema === void 0 ? void 0 : tableSchema.childTable) && tableSchema.childTableName) {
|
||||
const parentDb = dbSchema.find((db) => db.dbFullName == tableSchema.childTableDbFullName);
|
||||
const parentDbTable = parentDb === null || parentDb === void 0 ? void 0 : parentDb.tables.find((tbl) => tbl.tableName == tableSchema.childTableName);
|
||||
if (parentDb && parentDbTable) {
|
||||
newTableInsertObject["child_table"] = 1;
|
||||
newTableInsertObject["child_table_parent_database"] =
|
||||
parentDb.dbFullName;
|
||||
newTableInsertObject["child_table_parent_table"] =
|
||||
parentDbTable.tableName;
|
||||
}
|
||||
}
|
||||
const newTableRecordEntry = (yield (0, addDbEntry_1.default)({
|
||||
data: newTableInsertObject,
|
||||
tableName: "user_database_tables",
|
||||
dbContext: "Master",
|
||||
forceLocal: true,
|
||||
}));
|
||||
if (newTableRecordEntry.insertId) {
|
||||
recordedTableEntryArray = yield (0, varDatabaseDbHandler_1.default)({
|
||||
queryString: (queryObj === null || queryObj === void 0 ? void 0 : queryObj.string) || "",
|
||||
queryValuesArray: queryObj === null || queryObj === void 0 ? void 0 : queryObj.values,
|
||||
});
|
||||
recordedTableEntry = recordedTableEntryArray === null || recordedTableEntryArray === void 0 ? void 0 : recordedTableEntryArray[0];
|
||||
}
|
||||
}
|
||||
return recordedTableEntry;
|
||||
}
|
||||
catch (error) {
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { DSQL_IndexSchemaType } from "../../types";
|
||||
type Param = {
|
||||
tableName: string;
|
||||
dbFullName: string;
|
||||
indexes: DSQL_IndexSchemaType[];
|
||||
};
|
||||
/**
|
||||
* Handle DATASQUIREL Table Indexes
|
||||
* ===================================================
|
||||
* @description Iterate through each datasquirel schema
|
||||
* table index(if available), and perform operations
|
||||
*/
|
||||
export default function handleIndexescreateDbFromSchema({ dbFullName, tableName, indexes, }: Param): Promise<void>;
|
||||
export {};
|
||||
@@ -0,0 +1,55 @@
|
||||
"use strict";
|
||||
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) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = handleIndexescreateDbFromSchema;
|
||||
const varDatabaseDbHandler_1 = __importDefault(require("../utils/varDatabaseDbHandler"));
|
||||
/**
|
||||
* Handle DATASQUIREL Table Indexes
|
||||
* ===================================================
|
||||
* @description Iterate through each datasquirel schema
|
||||
* table index(if available), and perform operations
|
||||
*/
|
||||
function handleIndexescreateDbFromSchema(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ dbFullName, tableName, indexes, }) {
|
||||
for (let g = 0; g < indexes.length; g++) {
|
||||
const { indexType, indexName, indexTableFields, alias } = indexes[g];
|
||||
if (!(alias === null || alias === void 0 ? void 0 : alias.match(/./)))
|
||||
continue;
|
||||
/**
|
||||
* @description Check for existing Index in MYSQL db
|
||||
*/
|
||||
try {
|
||||
/**
|
||||
* @type {import("../../types").DSQL_MYSQL_SHOW_INDEXES_Type[]}
|
||||
* @description All indexes from MYSQL db
|
||||
*/ // @ts-ignore
|
||||
const allExistingIndexes = yield (0, varDatabaseDbHandler_1.default)({
|
||||
queryString: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\``,
|
||||
});
|
||||
const existingKeyInDb = allExistingIndexes.filter((indexObject) => indexObject.Key_name === alias);
|
||||
if (!existingKeyInDb[0])
|
||||
throw new Error("This Index Does not Exist");
|
||||
}
|
||||
catch (error) {
|
||||
/**
|
||||
* @description Create new index if determined that it
|
||||
* doesn't exist in MYSQL db
|
||||
*/
|
||||
yield (0, varDatabaseDbHandler_1.default)({
|
||||
queryString: `CREATE${(indexType === null || indexType === void 0 ? void 0 : indexType.match(/fullText/i)) ? " FULLTEXT" : ""} INDEX \`${alias}\` ON \`${dbFullName}\`.\`${tableName}\`(${indexTableFields === null || indexTableFields === void 0 ? void 0 : indexTableFields.map((nm) => nm.value).map((nm) => `\`${nm}\``).join(",")}) COMMENT 'schema_index'`,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
type Param = {
|
||||
userId?: number | string | null;
|
||||
targetDatabase?: string;
|
||||
dbSchemaData?: import("../types").DSQL_DatabaseSchemaType[];
|
||||
dbSchemaData?: import("../../types").DSQL_DatabaseSchemaType[];
|
||||
};
|
||||
/**
|
||||
* # Create database from Schema Function
|
||||
+34
-65
@@ -14,13 +14,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = createDbFromSchema;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const noDatabaseDbHandler_1 = __importDefault(require("./utils/noDatabaseDbHandler"));
|
||||
const varDatabaseDbHandler_1 = __importDefault(require("./utils/varDatabaseDbHandler"));
|
||||
const createTable_1 = __importDefault(require("./utils/createTable"));
|
||||
const updateTable_1 = __importDefault(require("./utils/updateTable"));
|
||||
const dbHandler_1 = __importDefault(require("./utils/dbHandler"));
|
||||
const ejson_1 = __importDefault(require("../utils/ejson"));
|
||||
const grab_dir_names_1 = __importDefault(require("../utils/backend/names/grab-dir-names"));
|
||||
const noDatabaseDbHandler_1 = __importDefault(require("../utils/noDatabaseDbHandler"));
|
||||
const varDatabaseDbHandler_1 = __importDefault(require("../utils/varDatabaseDbHandler"));
|
||||
const createTable_1 = __importDefault(require("../utils/createTable"));
|
||||
const updateTable_1 = __importDefault(require("../utils/updateTable"));
|
||||
const dbHandler_1 = __importDefault(require("../utils/dbHandler"));
|
||||
const ejson_1 = __importDefault(require("../../utils/ejson"));
|
||||
const grab_dir_names_1 = __importDefault(require("../../utils/backend/names/grab-dir-names"));
|
||||
const check_db_record_1 = __importDefault(require("./check-db-record"));
|
||||
const check_table_record_1 = __importDefault(require("./check-table-record"));
|
||||
const handle_indexes_1 = __importDefault(require("./handle-indexes"));
|
||||
/**
|
||||
* # Create database from Schema Function
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
@@ -38,25 +41,21 @@ function createDbFromSchema(_a) {
|
||||
console.log("Schema Not Found!");
|
||||
return;
|
||||
}
|
||||
// await createDatabasesFromSchema(dbSchema);
|
||||
for (let i = 0; i < dbSchema.length; i++) {
|
||||
const database = dbSchema[i];
|
||||
const { dbFullName, tables, dbName, dbSlug, childrenDatabases } = database;
|
||||
const { dbFullName, tables, dbSlug, childrenDatabases } = database;
|
||||
if (targetDatabase && dbFullName != targetDatabase) {
|
||||
continue;
|
||||
}
|
||||
/** @type {any} */
|
||||
const dbCheck = yield (0, noDatabaseDbHandler_1.default)(`SELECT SCHEMA_NAME AS dbFullName FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '${dbFullName}'`);
|
||||
if (!((_b = dbCheck === null || dbCheck === void 0 ? void 0 : dbCheck[0]) === null || _b === void 0 ? void 0 : _b.dbFullName)) {
|
||||
const newDatabase = yield (0, noDatabaseDbHandler_1.default)(`CREATE DATABASE IF NOT EXISTS \`${dbFullName}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`);
|
||||
}
|
||||
/**
|
||||
* Select all tables
|
||||
* @type {any}
|
||||
* @description Select All tables in target database
|
||||
*/
|
||||
const allTables = yield (0, noDatabaseDbHandler_1.default)(`SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='${dbFullName}'`);
|
||||
// let tableDropped;
|
||||
let recordedDbEntry = yield (0, check_db_record_1.default)({
|
||||
dbSchema: database,
|
||||
userId,
|
||||
});
|
||||
for (let tb = 0; tb < allTables.length; tb++) {
|
||||
const { TABLE_NAME } = allTables[tb];
|
||||
/**
|
||||
@@ -89,13 +88,6 @@ function createDbFromSchema(_a) {
|
||||
}
|
||||
}
|
||||
}
|
||||
const recordedDbEntryArray = userId
|
||||
? yield (0, varDatabaseDbHandler_1.default)({
|
||||
queryString: `SELECT * FROM datasquirel.user_databases WHERE db_full_name = ?`,
|
||||
queryValuesArray: [dbFullName],
|
||||
})
|
||||
: undefined;
|
||||
const recordedDbEntry = recordedDbEntryArray === null || recordedDbEntryArray === void 0 ? void 0 : recordedDbEntryArray[0];
|
||||
/**
|
||||
* @description Iterate through each table and perform table actions
|
||||
*/
|
||||
@@ -155,10 +147,8 @@ function createDbFromSchema(_a) {
|
||||
});
|
||||
}
|
||||
}
|
||||
////////////////////////////////////////
|
||||
}
|
||||
else {
|
||||
////////////////////////////////////////
|
||||
/**
|
||||
* @description Create new Table if table doesnt exist
|
||||
*/
|
||||
@@ -169,48 +159,27 @@ function createDbFromSchema(_a) {
|
||||
tableSchema: table,
|
||||
recordedDbEntry,
|
||||
});
|
||||
if (indexes && indexes[0]) {
|
||||
/**
|
||||
* Handle DATASQUIREL Table Indexes
|
||||
* ===================================================
|
||||
* @description Iterate through each datasquirel schema
|
||||
* table index(if available), and perform operations
|
||||
*/
|
||||
if (indexes && indexes[0]) {
|
||||
for (let g = 0; g < indexes.length; g++) {
|
||||
const { indexType, indexName, indexTableFields, alias, } = indexes[g];
|
||||
if (!(alias === null || alias === void 0 ? void 0 : alias.match(/./)))
|
||||
continue;
|
||||
/**
|
||||
* @description Check for existing Index in MYSQL db
|
||||
*/
|
||||
try {
|
||||
/**
|
||||
* @type {import("../types").DSQL_MYSQL_SHOW_INDEXES_Type[]}
|
||||
* @description All indexes from MYSQL db
|
||||
*/ // @ts-ignore
|
||||
const allExistingIndexes = yield (0, varDatabaseDbHandler_1.default)({
|
||||
queryString: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\``,
|
||||
});
|
||||
const existingKeyInDb = allExistingIndexes.filter((indexObject) => indexObject.Key_name === alias);
|
||||
if (!existingKeyInDb[0])
|
||||
throw new Error("This Index Does not Exist");
|
||||
}
|
||||
catch (error) {
|
||||
/**
|
||||
* @description Create new index if determined that it
|
||||
* doesn't exist in MYSQL db
|
||||
*/
|
||||
yield (0, varDatabaseDbHandler_1.default)({
|
||||
queryString: `CREATE${(indexType === null || indexType === void 0 ? void 0 : indexType.match(/fullText/i))
|
||||
? " FULLTEXT"
|
||||
: ""} INDEX \`${alias}\` ON \`${dbFullName}\`.\`${tableName}\`(${indexTableFields === null || indexTableFields === void 0 ? void 0 : indexTableFields.map((nm) => nm.value).map((nm) => `\`${nm}\``).join(",")}) COMMENT 'schema_index'`,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Handle DATASQUIREL Table Indexes
|
||||
* ===================================================
|
||||
* @description Iterate through each datasquirel schema
|
||||
* table index(if available), and perform operations
|
||||
*/
|
||||
if (indexes === null || indexes === void 0 ? void 0 : indexes[0]) {
|
||||
(0, handle_indexes_1.default)({
|
||||
dbFullName,
|
||||
indexes,
|
||||
tableName,
|
||||
});
|
||||
}
|
||||
}
|
||||
const tableRecord = yield (0, check_table_record_1.default)({
|
||||
dbFullName,
|
||||
dbSchema,
|
||||
tableSchema: table,
|
||||
dbRecord: recordedDbEntry,
|
||||
userId,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* @description Check all children databases
|
||||
+1
-1
@@ -32,7 +32,7 @@ function dbHandler(_a) {
|
||||
results = yield CONNECTION.query(query);
|
||||
}
|
||||
}
|
||||
catch ( /** @type {any} */error) {
|
||||
catch (error) {
|
||||
if (process.env.FIRST_RUN) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user