Updates
This commit is contained in:
+3
-3
@@ -22,7 +22,7 @@ const grab_query_and_values_1 = __importDefault(require("../../../utils/grab-que
|
||||
*/
|
||||
function apiGet(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ query, dbFullName, queryValues, tableName, dbSchema, debug, dbContext, forceLocal, }) {
|
||||
var _b;
|
||||
var _b, _c;
|
||||
const queryAndValues = (0, grab_query_and_values_1.default)({
|
||||
query,
|
||||
values: queryValues,
|
||||
@@ -49,7 +49,7 @@ function apiGet(_a) {
|
||||
}
|
||||
let tableSchema;
|
||||
if (dbSchema) {
|
||||
const targetTable = dbSchema.tables.find((table) => table.tableName === tableName);
|
||||
const targetTable = (_b = dbSchema.tables) === null || _b === void 0 ? void 0 : _b.find((table) => table.tableName === tableName);
|
||||
if (targetTable) {
|
||||
const clonedTargetTable = lodash_1.default.cloneDeep(targetTable);
|
||||
delete clonedTargetTable.childTable;
|
||||
@@ -79,7 +79,7 @@ function apiGet(_a) {
|
||||
component: "/api/query/get/lines-85-94",
|
||||
message: error.message,
|
||||
});
|
||||
(_b = global.ERROR_CALLBACK) === null || _b === void 0 ? void 0 : _b.call(global, `API Get Error`, error);
|
||||
(_c = global.ERROR_CALLBACK) === null || _c === void 0 ? void 0 : _c.call(global, `API Get Error`, error);
|
||||
if (debug && global.DSQL_USE_LOCAL) {
|
||||
console.log("apiGet:error", error.message);
|
||||
console.log("queryAndValues", queryAndValues);
|
||||
|
||||
@@ -20,6 +20,6 @@ export default function apiCreateUser({ encryptionKey, payload, database, userId
|
||||
} | {
|
||||
success: boolean;
|
||||
msg: string;
|
||||
sqlResult: any;
|
||||
sqlResult: import("../../../types").PostInsertReturn | null;
|
||||
payload: null;
|
||||
}>;
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import { DSQL_DATASQUIREL_USER_DATABASES } from "@/package-shared/types/dsql";
|
||||
type Params = {
|
||||
userId: number | string;
|
||||
database: DSQL_DATASQUIREL_USER_DATABASES;
|
||||
};
|
||||
export default function createDbSchemaFromDb({ userId, database, }: Params): Promise<boolean | undefined>;
|
||||
export {};
|
||||
@@ -0,0 +1,132 @@
|
||||
"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 = createDbSchemaFromDb;
|
||||
const varDatabaseDbHandler_1 = __importDefault(require("../../functions/backend/varDatabaseDbHandler"));
|
||||
const grabUserSchemaData_1 = __importDefault(require("../../functions/backend/grabUserSchemaData"));
|
||||
const setUserSchemaData_1 = __importDefault(require("../../functions/backend/setUserSchemaData"));
|
||||
const addDbEntry_1 = __importDefault(require("../../functions/backend/db/addDbEntry"));
|
||||
const slugToCamelTitle_1 = __importDefault(require("../../shell/utils/slugToCamelTitle"));
|
||||
function createDbSchemaFromDb(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ userId, database, }) {
|
||||
var _b, _c, _d, _e, _f, _g;
|
||||
try {
|
||||
if (!userId) {
|
||||
console.log("No user Id provided");
|
||||
return;
|
||||
}
|
||||
const userSchemaData = (0, grabUserSchemaData_1.default)({ userId });
|
||||
if (!userSchemaData)
|
||||
throw new Error("User schema data not found!");
|
||||
const targetDb = userSchemaData.filter((dbObject) => dbObject.dbFullName === database.db_full_name)[0];
|
||||
const existingTables = yield (0, varDatabaseDbHandler_1.default)({
|
||||
database: database.db_full_name,
|
||||
queryString: `SHOW TABLES FROM ${database.db_full_name}`,
|
||||
});
|
||||
if (!existingTables)
|
||||
throw new Error("No Existing Tables");
|
||||
for (let i = 0; i < existingTables.length; i++) {
|
||||
const table = existingTables[i];
|
||||
const tableName = Object.values(table)[0];
|
||||
const tableInsert = yield (0, addDbEntry_1.default)({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "user_database_tables",
|
||||
data: {
|
||||
user_id: userId,
|
||||
db_id: database.id,
|
||||
db_slug: database.db_slug,
|
||||
table_name: (0, slugToCamelTitle_1.default)(tableName),
|
||||
table_slug: tableName,
|
||||
},
|
||||
});
|
||||
const tableObject = {
|
||||
tableName: tableName,
|
||||
tableFullName: (0, slugToCamelTitle_1.default)(tableName) || "",
|
||||
fields: [],
|
||||
indexes: [],
|
||||
};
|
||||
const tableColumns = yield (0, varDatabaseDbHandler_1.default)({
|
||||
database: database.db_full_name,
|
||||
queryString: `SHOW COLUMNS FROM ${database.db_full_name}.${tableName}`,
|
||||
});
|
||||
if (tableColumns) {
|
||||
for (let k = 0; k < tableColumns.length; k++) {
|
||||
const tableColumn = tableColumns[k];
|
||||
const { Field, Type, Null, Key, Default, Extra } = tableColumn;
|
||||
const fieldObject = {
|
||||
fieldName: Field,
|
||||
dataType: Type.toUpperCase(),
|
||||
};
|
||||
if (Null === null || Null === void 0 ? void 0 : Null.match(/^no$/i))
|
||||
fieldObject.notNullValue = true;
|
||||
if (Key === null || Key === void 0 ? void 0 : Key.match(/^pri$/i))
|
||||
fieldObject.primaryKey = true;
|
||||
if ((_b = Default === null || Default === void 0 ? void 0 : Default.toString()) === null || _b === void 0 ? void 0 : _b.match(/./))
|
||||
fieldObject.defaultValue = Default;
|
||||
if ((_c = Default === null || Default === void 0 ? void 0 : Default.toString()) === null || _c === void 0 ? void 0 : _c.match(/timestamp/i)) {
|
||||
delete fieldObject.defaultValue;
|
||||
fieldObject.defaultValueLiteral = Default;
|
||||
}
|
||||
if ((_d = Extra === null || Extra === void 0 ? void 0 : Extra.toString()) === null || _d === void 0 ? void 0 : _d.match(/auto_increment/i))
|
||||
fieldObject.autoIncrement = true;
|
||||
tableObject.fields.push(fieldObject);
|
||||
}
|
||||
}
|
||||
const tableIndexes = yield (0, varDatabaseDbHandler_1.default)({
|
||||
database: database.db_full_name,
|
||||
queryString: `SHOW INDEXES FROM ${database.db_full_name}.${tableName}`,
|
||||
});
|
||||
if (tableIndexes) {
|
||||
for (let m = 0; m < tableIndexes.length; m++) {
|
||||
const indexObject = tableIndexes[m];
|
||||
const { Table, Key_name, Column_name, Null, Index_type, Index_comment, } = indexObject;
|
||||
if (!(Index_comment === null || Index_comment === void 0 ? void 0 : Index_comment.match(/^schema_index$/)))
|
||||
continue;
|
||||
const indexNewObject = {
|
||||
indexType: (Index_type === null || Index_type === void 0 ? void 0 : Index_type.match(/fulltext/i))
|
||||
? "fullText"
|
||||
: "regular",
|
||||
indexName: Key_name,
|
||||
indexTableFields: [],
|
||||
};
|
||||
const targetTableFieldObject = tableColumns === null || tableColumns === void 0 ? void 0 : tableColumns.filter((col) => col.Field === Column_name)[0];
|
||||
const existingIndexField = (_e = tableObject.indexes) === null || _e === void 0 ? void 0 : _e.filter((indx) => indx.indexName == Key_name);
|
||||
if (existingIndexField && existingIndexField[0]) {
|
||||
(_f = existingIndexField[0].indexTableFields) === null || _f === void 0 ? void 0 : _f.push({
|
||||
value: Column_name,
|
||||
dataType: targetTableFieldObject.Type.toUpperCase(),
|
||||
});
|
||||
}
|
||||
else {
|
||||
indexNewObject.indexTableFields = [
|
||||
{
|
||||
value: Column_name,
|
||||
dataType: targetTableFieldObject.Type.toUpperCase(),
|
||||
},
|
||||
];
|
||||
(_g = tableObject.indexes) === null || _g === void 0 ? void 0 : _g.push(indexNewObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
targetDb.tables.push(tableObject);
|
||||
}
|
||||
(0, setUserSchemaData_1.default)({ schemaData: userSchemaData, userId });
|
||||
return true;
|
||||
}
|
||||
catch (error) {
|
||||
console.log(error);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
import { DbContextsArray } from "./runQuery";
|
||||
type Param = {
|
||||
import { PostInsertReturn } from "../../../types";
|
||||
type Param<T extends {
|
||||
[k: string]: any;
|
||||
} = any> = {
|
||||
dbContext?: (typeof DbContextsArray)[number];
|
||||
paradigm?: "Read Only" | "Full Access";
|
||||
dbFullName?: string;
|
||||
tableName: string;
|
||||
data: any;
|
||||
data: T;
|
||||
tableSchema?: import("../../../types").DSQL_TableSchemaType;
|
||||
duplicateColumnName?: string;
|
||||
duplicateColumnValue?: string;
|
||||
@@ -17,5 +20,7 @@ type Param = {
|
||||
/**
|
||||
* Add a db Entry Function
|
||||
*/
|
||||
export default function addDbEntry({ dbContext, paradigm, dbFullName, tableName, data, tableSchema, duplicateColumnName, duplicateColumnValue, update, encryptionKey, encryptionSalt, forceLocal, debug, }: Param): Promise<any>;
|
||||
export default function addDbEntry<T extends {
|
||||
[k: string]: any;
|
||||
} = any>({ dbContext, paradigm, dbFullName, tableName, data, tableSchema, duplicateColumnName, duplicateColumnValue, update, encryptionKey, encryptionSalt, forceLocal, debug, }: Param<T>): Promise<PostInsertReturn | null>;
|
||||
export {};
|
||||
|
||||
@@ -26,9 +26,6 @@ const debug_log_1 = __importDefault(require("../../../utils/logging/debug-log"))
|
||||
function addDbEntry(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ dbContext, paradigm, dbFullName, tableName, data, tableSchema, duplicateColumnName, duplicateColumnValue, update, encryptionKey, encryptionSalt, forceLocal, debug, }) {
|
||||
var _b, _c, _d;
|
||||
/**
|
||||
* Initialize variables
|
||||
*/
|
||||
const isMaster = forceLocal
|
||||
? true
|
||||
: (0, check_if_is_master_1.default)({ dbContext, dbFullName });
|
||||
@@ -45,9 +42,6 @@ function addDbEntry(_a) {
|
||||
const DB_RO_CONN = isMaster
|
||||
? global.DSQL_DB_CONN
|
||||
: global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN;
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
if (data === null || data === void 0 ? void 0 : data["date_created_timestamp"])
|
||||
delete data["date_created_timestamp"];
|
||||
if (data === null || data === void 0 ? void 0 : data["date_updated_timestamp"])
|
||||
@@ -60,9 +54,6 @@ function addDbEntry(_a) {
|
||||
delete data["date_created"];
|
||||
if (data === null || data === void 0 ? void 0 : data["date_created_code"])
|
||||
delete data["date_created_code"];
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
if (duplicateColumnName && typeof duplicateColumnName === "string") {
|
||||
const checkDuplicateQuery = `SELECT * FROM ${isMaster ? "" : `\`${dbFullName}\`.`}\`${tableName}\` WHERE \`${duplicateColumnName}\`=?`;
|
||||
const duplicateValue = yield (0, conn_db_handler_1.default)(DB_RO_CONN, checkDuplicateQuery, [duplicateColumnValue]);
|
||||
@@ -83,18 +74,12 @@ function addDbEntry(_a) {
|
||||
});
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Declare variables
|
||||
*
|
||||
* @description Declare "results" variable
|
||||
*/
|
||||
const dataKeys = Object.keys(data);
|
||||
let insertKeysArray = [];
|
||||
let insertValuesArray = [];
|
||||
for (let i = 0; i < dataKeys.length; i++) {
|
||||
try {
|
||||
const dataKey = dataKeys[i];
|
||||
// @ts-ignore
|
||||
let value = data === null || data === void 0 ? void 0 : data[dataKey];
|
||||
const targetFieldSchemaArray = tableSchema
|
||||
? (_b = tableSchema === null || tableSchema === void 0 ? void 0 : tableSchema.fields) === null || _b === void 0 ? void 0 : _b.filter((field) => field.fieldName == dataKey)
|
||||
@@ -144,7 +129,6 @@ function addDbEntry(_a) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
////////////////////////////////////////
|
||||
if (!(data === null || data === void 0 ? void 0 : data["date_created"])) {
|
||||
insertKeysArray.push("`date_created`");
|
||||
insertValuesArray.push(Date());
|
||||
@@ -153,7 +137,6 @@ function addDbEntry(_a) {
|
||||
insertKeysArray.push("`date_created_code`");
|
||||
insertValuesArray.push(Date.now());
|
||||
}
|
||||
////////////////////////////////////////
|
||||
if (!(data === null || data === void 0 ? void 0 : data["date_updated"])) {
|
||||
insertKeysArray.push("`date_updated`");
|
||||
insertValuesArray.push(Date());
|
||||
@@ -162,7 +145,6 @@ function addDbEntry(_a) {
|
||||
insertKeysArray.push("`date_updated_code`");
|
||||
insertValuesArray.push(Date.now());
|
||||
}
|
||||
////////////////////////////////////////
|
||||
const query = `INSERT INTO ${isMaster ? "" : `\`${dbFullName}\`.`}\`${tableName}\` (${insertKeysArray.join(",")}) VALUES (${insertValuesArray
|
||||
.map(() => "?")
|
||||
.join(",")})`;
|
||||
@@ -192,9 +174,6 @@ function addDbEntry(_a) {
|
||||
label: "newInsert",
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Return statement
|
||||
*/
|
||||
return newInsert;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import { DbContextsArray } from "./runQuery";
|
||||
type Param = {
|
||||
import { PostInsertReturn } from "../../../types";
|
||||
type Param<T extends {
|
||||
[k: string]: any;
|
||||
} = any> = {
|
||||
dbContext?: (typeof DbContextsArray)[number];
|
||||
dbFullName?: string;
|
||||
tableName: string;
|
||||
@@ -7,7 +10,7 @@ type Param = {
|
||||
encryptionSalt?: string;
|
||||
data: any;
|
||||
tableSchema?: import("../../../types").DSQL_TableSchemaType;
|
||||
identifierColumnName: string;
|
||||
identifierColumnName: keyof T;
|
||||
identifierValue: string | number;
|
||||
forceLocal?: boolean;
|
||||
};
|
||||
@@ -15,5 +18,7 @@ type Param = {
|
||||
* # Update DB Function
|
||||
* @description
|
||||
*/
|
||||
export default function updateDbEntry({ dbContext, dbFullName, tableName, data, tableSchema, identifierColumnName, identifierValue, encryptionKey, encryptionSalt, forceLocal, }: Param): Promise<object | null>;
|
||||
export default function updateDbEntry<T extends {
|
||||
[k: string]: any;
|
||||
} = any>({ dbContext, dbFullName, tableName, data, tableSchema, identifierColumnName, identifierValue, encryptionKey, encryptionSalt, forceLocal, }: Param<T>): Promise<PostInsertReturn | null>;
|
||||
export {};
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { DSQL_DatabaseSchemaType } from "@/package-shared/types";
|
||||
type Params = {
|
||||
userId?: string | number;
|
||||
};
|
||||
/**
|
||||
* # Grab User Schema Data
|
||||
*/
|
||||
export default function grabUserSchemaData({ userId, }: {
|
||||
userId: string | number;
|
||||
}): import("../../types").DSQL_DatabaseSchemaType[] | null;
|
||||
export default function grabUserSchemaData({ userId, }: Params): DSQL_DatabaseSchemaType[] | null;
|
||||
export {};
|
||||
|
||||
@@ -6,16 +6,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = grabUserSchemaData;
|
||||
const serverError_1 = __importDefault(require("./serverError"));
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const grab_dir_names_1 = __importDefault(require("@/package-shared/utils/backend/names/grab-dir-names"));
|
||||
const client_exports_1 = require("@/client-exports");
|
||||
/**
|
||||
* # Grab User Schema Data
|
||||
*/
|
||||
function grabUserSchemaData({ userId, }) {
|
||||
var _a;
|
||||
try {
|
||||
const userSchemaFilePath = path_1.default.resolve(process.cwd(), `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${userId}/main.json`);
|
||||
const userSchemaData = JSON.parse(fs_1.default.readFileSync(userSchemaFilePath, "utf-8"));
|
||||
return userSchemaData;
|
||||
const { userSchemaMainJSONFilePath } = (0, grab_dir_names_1.default)({ userId });
|
||||
const schemaJSON = fs_1.default.readFileSync(userSchemaMainJSONFilePath || "", "utf-8");
|
||||
const schemaObj = client_exports_1.EJSON.parse(schemaJSON);
|
||||
return schemaObj;
|
||||
}
|
||||
catch (error) {
|
||||
(0, serverError_1.default)({
|
||||
|
||||
@@ -6,15 +6,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = setUserSchemaData;
|
||||
const serverError_1 = __importDefault(require("./serverError"));
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const grab_dir_names_1 = __importDefault(require("@/package-shared/utils/backend/names/grab-dir-names"));
|
||||
/**
|
||||
* # Set User Schema Data
|
||||
*/
|
||||
function setUserSchemaData({ userId, schemaData, }) {
|
||||
var _a;
|
||||
try {
|
||||
const userSchemaFilePath = path_1.default.resolve(process.cwd(), `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${userId}/main.json`);
|
||||
fs_1.default.writeFileSync(userSchemaFilePath, JSON.stringify(schemaData), "utf8");
|
||||
const { userSchemaMainJSONFilePath } = (0, grab_dir_names_1.default)({ userId });
|
||||
if (!userSchemaMainJSONFilePath) {
|
||||
throw new Error(`No User Schema JSON found!`);
|
||||
}
|
||||
fs_1.default.writeFileSync(userSchemaMainJSONFilePath, JSON.stringify(schemaData), "utf8");
|
||||
return true;
|
||||
}
|
||||
catch (error) {
|
||||
|
||||
+3
-5
@@ -11,7 +11,7 @@ const ejson_1 = __importDefault(require("../../utils/ejson"));
|
||||
const generate_type_definitions_1 = __importDefault(require("./generate-type-definitions"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
function dbSchemaToType(params) {
|
||||
var _a;
|
||||
var _a, _b;
|
||||
let datasquirelSchema;
|
||||
const defaultTableFieldsJSONFilePath = path_1.default.resolve(__dirname, "../../data/defaultFields.json");
|
||||
if (params === null || params === void 0 ? void 0 : params.dbSchema) {
|
||||
@@ -39,10 +39,8 @@ function dbSchemaToType(params) {
|
||||
...newDefaultFields,
|
||||
] });
|
||||
});
|
||||
const defDbName = (datasquirelSchema.dbName ||
|
||||
((_a = datasquirelSchema.dbFullName) === null || _a === void 0 ? void 0 : _a.replace(/datasquirel_user_\d+_/, "")))
|
||||
.toUpperCase()
|
||||
.replace(/ /g, "_");
|
||||
const defDbName = (_b = (datasquirelSchema.dbName ||
|
||||
((_a = datasquirelSchema.dbFullName) === null || _a === void 0 ? void 0 : _a.replace(/datasquirel_user_\d+_/, "")))) === null || _b === void 0 ? void 0 : _b.toUpperCase().replace(/ /g, "_");
|
||||
const schemas = dbTablesSchemas
|
||||
.map((table) => (0, generate_type_definitions_1.default)({
|
||||
paradigm: "TypeScript",
|
||||
|
||||
+4
-1
@@ -24,7 +24,10 @@ function sqlGenerator({ tableName, genObject, dbFullName, count }) {
|
||||
return field;
|
||||
})();
|
||||
let str = `${finalFieldName}=?`;
|
||||
if (typeof queryObj.value == "string" ||
|
||||
if (queryObj.nullValue) {
|
||||
str = `${finalFieldName} IS NULL`;
|
||||
}
|
||||
else if (typeof queryObj.value == "string" ||
|
||||
typeof queryObj.value == "number") {
|
||||
const valueParsed = String(queryObj.value);
|
||||
if (queryObj.equality == "LIKE") {
|
||||
|
||||
Reference in New Issue
Block a user