This commit is contained in:
Benjamin Toby
2025-02-12 17:56:44 +01:00
parent a5c3d59d24
commit 1b48c07ee8
431 changed files with 1665 additions and 827 deletions
+5 -10
View File
@@ -9,7 +9,6 @@ import {
DSQL_DatabaseSchemaType,
GetReqQueryObject,
GetReturn,
ServerQueryParam,
} from "../types";
import apiGetGrabQueryAndValues from "../utils/grab-query-and-values";
@@ -21,6 +20,7 @@ type Param<T extends { [k: string]: any } = { [k: string]: any }> = {
tableName?: string;
user_id?: string | number;
debug?: boolean;
forceLocal?: boolean;
};
export type ApiGetParams = Param;
@@ -38,6 +38,7 @@ export default async function get<
tableName,
user_id,
debug,
forceLocal,
}: Param<T>): Promise<GetReturn> {
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
@@ -47,16 +48,9 @@ export default async function get<
*
* @description Look for local db settings in `.env` file and by pass the http request if available
*/
const { DSQL_DB_HOST, DSQL_DB_USERNAME, DSQL_DB_PASSWORD, DSQL_DB_NAME } =
process.env;
const { DSQL_DB_NAME } = process.env;
if (
DSQL_DB_HOST?.match(/./) &&
DSQL_DB_USERNAME?.match(/./) &&
DSQL_DB_PASSWORD?.match(/./) &&
DSQL_DB_NAME?.match(/./) &&
global.DSQL_USE_LOCAL
) {
if (DSQL_DB_NAME?.match(/./) && global.DSQL_USE_LOCAL) {
let dbSchema: DSQL_DatabaseSchemaType | undefined;
try {
@@ -78,6 +72,7 @@ export default async function get<
tableName,
dbSchema,
debug,
forceLocal,
});
}
+3
View File
@@ -13,6 +13,7 @@ type Param = {
queryValues?: any[];
tableName?: string;
user_id?: boolean;
forceLocal?: boolean;
};
/**
@@ -25,6 +26,7 @@ export default async function post({
database,
tableName,
user_id,
forceLocal,
}: Param): Promise<PostReturn> {
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
@@ -61,6 +63,7 @@ export default async function post({
dbSchema,
queryValues,
tableName,
forceLocal,
});
}
@@ -34,8 +34,10 @@ export default async function googleAuth({
apiUserID,
debug,
}: Param): Promise<APILoginFunctionReturn> {
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
const grabedHostNames = grabHostNames({
userId: apiUserID || process.env.DSQL_API_USER_ID,
});
const { host, port, scheme, user_id } = grabedHostNames;
const finalEncryptionKey =
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
@@ -107,12 +109,6 @@ export default async function googleAuth({
debug,
});
} else {
/**
* Make https request
*
* @description make a request to datasquirel.com
* @type {{ success: boolean, user: import("../../../types").DATASQUIREL_LoggedInUser | null, msg?: string, dsqlUserId?: number } | null } - Https response object
*/
httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify({
token,
@@ -179,7 +175,7 @@ export default async function googleAuth({
const cookieNames = getAuthCookieNames({
database,
userId: apiUserID || process.env.DSQL_API_USER_ID,
userId: user_id,
});
if (httpResponse.csrf) {
@@ -105,6 +105,7 @@ export default function userAuth({
success: false,
payload: null,
msg: "Couldn't Decrypt cookie",
cookieNames: keyNames,
};
}
@@ -127,6 +128,7 @@ export default function userAuth({
success: false,
payload: null,
msg: "No CSRF_K in decrypted payload",
cookieNames: keyNames,
};
}
@@ -135,6 +137,7 @@ export default function userAuth({
success: false,
payload: null,
msg: "Auth file doesn't exist",
cookieNames: keyNames,
};
}
@@ -152,6 +155,7 @@ export default function userAuth({
success: false,
payload: null,
msg: "CSRF_K mismatch",
cookieNames: keyNames,
};
}
}
@@ -166,6 +170,7 @@ export default function userAuth({
success: false,
payload: null,
msg: "Payload Creation Date is not a number",
cookieNames: keyNames,
};
}
@@ -180,6 +185,7 @@ export default function userAuth({
success: false,
payload: null,
msg: "Session has expired",
cookieNames: keyNames,
};
}
@@ -1,5 +1,3 @@
// @ts-check
import _ from "lodash";
import serverError from "../../backend/serverError";
import runQuery, { DbContextsArray } from "../../backend/db/runQuery";
@@ -7,7 +5,6 @@ import {
ApiGetQueryObject,
DSQL_TableSchemaType,
GetReturn,
ServerQueryParam,
} from "../../../types";
import apiGetGrabQueryAndValues from "../../../utils/grab-query-and-values";
@@ -19,6 +16,7 @@ type Param<T extends { [key: string]: any } = { [key: string]: any }> = {
dbSchema?: import("../../../types").DSQL_DatabaseSchemaType;
debug?: boolean;
dbContext?: (typeof DbContextsArray)[number];
forceLocal?: boolean;
};
/**
@@ -34,16 +32,14 @@ export default async function apiGet<
dbSchema,
debug,
dbContext,
forceLocal,
}: Param<T>): Promise<import("../../../types").GetReturn> {
const queryAndValues = apiGetGrabQueryAndValues({
query,
values: queryValues,
});
if (
typeof query == "string" &&
query.match(/^alter|^delete|information_schema|databases|^create/i)
) {
if (typeof query == "string" && query.match(/^alter|^delete|^create/i)) {
return { success: false, msg: "Wrong Input." };
}
@@ -59,6 +55,7 @@ export default async function apiGet<
tableName,
dbContext,
debug,
forceLocal,
});
if (debug && global.DSQL_USE_LOCAL) {
@@ -10,6 +10,7 @@ type Param = {
tableName?: string;
dbSchema?: DSQL_DatabaseSchemaType;
dbContext?: (typeof DbContextsArray)[number];
forceLocal?: boolean;
};
/**
@@ -22,6 +23,7 @@ export default async function apiPost({
tableName,
dbSchema,
dbContext,
forceLocal,
}: Param): Promise<PostReturn> {
if (typeof query === "string" && query?.match(/^create |^alter |^drop /i)) {
return { success: false, msg: "Wrong Input" };
@@ -49,6 +51,7 @@ export default async function apiPost({
queryValuesArray: queryValues,
tableName,
dbContext,
forceLocal,
});
results = result;
@@ -19,6 +19,7 @@ type Param = {
update?: boolean;
encryptionKey?: string;
encryptionSalt?: string;
forceLocal?: boolean;
};
/**
@@ -36,11 +37,14 @@ export default async function addDbEntry({
update,
encryptionKey,
encryptionSalt,
forceLocal,
}: Param): Promise<any> {
/**
* Initialize variables
*/
const isMaster = checkIfIsMaster({ dbContext, dbFullName });
const isMaster = forceLocal
? true
: checkIfIsMaster({ dbContext, dbFullName });
const DB_CONN = isMaster
? global.DSQL_DB_CONN
@@ -9,6 +9,7 @@ type Param = {
tableSchema?: import("../../../types").DSQL_TableSchemaType;
identifierColumnName: string;
identifierValue: string | number;
forceLocal?: boolean;
};
/**
@@ -21,9 +22,12 @@ export default async function deleteDbEntry({
tableName,
identifierColumnName,
identifierValue,
forceLocal,
}: Param): Promise<object | null> {
try {
const isMaster = checkIfIsMaster({ dbContext, dbFullName });
const isMaster = forceLocal
? true
: checkIfIsMaster({ dbContext, dbFullName });
const DB_CONN = isMaster
? global.DSQL_DB_CONN
@@ -18,6 +18,12 @@ type Param = {
dbSchema?: import("../../../types").DSQL_DatabaseSchemaType;
queryValuesArray?: (string | number)[];
tableName?: string;
forceLocal?: boolean;
};
type Return = {
result: any;
error?: string;
};
/**
@@ -32,7 +38,8 @@ export default async function runQuery({
tableName,
debug,
dbContext,
}: Param): Promise<any> {
forceLocal,
}: Param): Promise<Return> {
/**
* Declare variables
*
@@ -40,7 +47,7 @@ export default async function runQuery({
*/
let result: any;
let error: any;
let error: string | undefined;
let tableSchema: DSQL_TableSchemaType | undefined;
if (dbSchema) {
@@ -79,12 +86,7 @@ export default async function runQuery({
*
* @description Input Validation
*/
if (
readOnly &&
formattedQuery.match(
/^alter|^delete|information_schema|^create/i
)
) {
if (readOnly && formattedQuery.match(/^alter|^delete|^create/i)) {
throw new Error("Wrong Input!");
}
@@ -93,12 +95,14 @@ export default async function runQuery({
queryString: formattedQuery,
queryValuesArray: queryValuesArray?.map((vl) => String(vl)),
tableSchema,
forceLocal,
});
} else {
result = await fullAccessDbHandler({
queryString: formattedQuery,
queryValuesArray: queryValuesArray?.map((vl) => String(vl)),
tableSchema,
forceLocal,
});
}
} else if (typeof query === "object") {
@@ -132,7 +136,7 @@ export default async function runQuery({
});
if (!result?.insertId) {
error = new Error("Couldn't insert data");
error = "Couldn't insert data";
}
break;
@@ -167,18 +171,18 @@ export default async function runQuery({
break;
}
}
} catch (error: any) {
} catch (err: any) {
serverError({
component: "functions/backend/runQuery",
message: error.message,
message: err.message,
});
if (debug && global.DSQL_USE_LOCAL) {
console.log("runQuery:error", error.message);
console.log("runQuery:error", err.message);
}
result = null;
error = error.message;
error = err.message;
}
return { result, error };
@@ -15,6 +15,7 @@ type Param = {
tableSchema?: import("../../../types").DSQL_TableSchemaType;
identifierColumnName: string;
identifierValue: string | number;
forceLocal?: boolean;
};
/**
@@ -31,13 +32,16 @@ export default async function updateDbEntry({
identifierValue,
encryptionKey,
encryptionSalt,
forceLocal,
}: Param): Promise<object | null> {
/**
* Check if data is valid
*/
if (!data || !Object.keys(data).length) return null;
const isMaster = checkIfIsMaster({ dbContext, dbFullName });
const isMaster = forceLocal
? true
: checkIfIsMaster({ dbContext, dbFullName });
const DB_CONN = isMaster
? global.DSQL_DB_CONN
@@ -6,6 +6,7 @@ type Param = {
queryString: string;
tableSchema?: import("../../types").DSQL_TableSchemaType | null;
queryValuesArray?: string[];
forceLocal?: boolean;
};
/**
@@ -15,6 +16,7 @@ export default async function fullAccessDbHandler({
queryString,
tableSchema,
queryValuesArray,
forceLocal,
}: Param) {
/**
* Declare variables
@@ -23,7 +25,9 @@ export default async function fullAccessDbHandler({
*/
let results;
const DB_CONN = global.DSQL_FULL_ACCESS_DB_CONN || global.DSQL_DB_CONN;
const DB_CONN = forceLocal
? global.DSQL_DB_CONN
: global.DSQL_FULL_ACCESS_DB_CONN || global.DSQL_DB_CONN;
/**
* Fetch from db
@@ -36,10 +36,3 @@ export default function setUserSchemaData({
return false;
}
}
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
@@ -6,6 +6,7 @@ type Param = {
queryString: string;
queryValuesArray?: string[];
tableSchema?: import("../../types").DSQL_TableSchemaType;
forceLocal?: boolean;
};
/**
@@ -16,47 +17,28 @@ export default async function varReadOnlyDatabaseDbHandler({
queryString,
queryValuesArray,
tableSchema,
forceLocal,
}: Param) {
/**
* Declare variables
*
* @description Declare "results" variable
*/
let results;
const DB_CONN = global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN;
const DB_CONN = forceLocal
? global.DSQL_DB_CONN
: global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN;
/**
* Fetch from db
*
* @description Fetch data from db if no cache
*/
try {
results = await connDbHandler(DB_CONN, queryString, queryValuesArray);
////////////////////////////////////////
} catch (error: any) {
////////////////////////////////////////
serverError({
component: "varReadOnlyDatabaseDbHandler",
message: error.message,
noMail: true,
});
/**
* Return error
*/
return error.message;
} finally {
DB_CONN?.end();
}
/**
* Return results
*
* @description Return results add to cache if "req" param is passed
*/
if (results) {
const unparsedResults = results;
const parsedResults = await parseDbResults({
@@ -73,7 +73,6 @@ export default function sqlGenerator<
sqlSearhValues.push(valueParsed);
}
} else if (Array.isArray(queryObj.value)) {
/** @type {string[]} */
const strArray: string[] = [];
queryObj.value.forEach((val) => {
const valueParsed = val;
@@ -97,15 +96,11 @@ export default function sqlGenerator<
}
const sqlSearhString = queryKeys?.map((field) => {
const queryObj =
/** @type {import("../../../types").ServerQueryQueryObject} */ finalQuery?.[
field
];
const queryObj = finalQuery?.[field];
if (!queryObj) return;
if (queryObj.__query) {
const subQueryGroup =
/** @type {import("../../../types").ServerQueryQueryObject}} */ queryObj.__query;
const subQueryGroup = queryObj.__query;
const subSearchKeys = Object.keys(subQueryGroup);
const subSearchString = subSearchKeys.map((_field) => {
@@ -0,0 +1,72 @@
import varDatabaseDbHandler from "../utils/varDatabaseDbHandler";
import { DSQL_DatabaseSchemaType, PostInsertReturn } from "../../types";
import { DSQL_DATASQUIREL_USER_DATABASES } from "../../types/dsql";
import numberfy from "../../utils/numberfy";
import addDbEntry from "@/package-shared/functions/backend/db/addDbEntry";
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 async function checkDbRecordCreateDbSchema({
userId,
dbSchema,
}: Param): Promise<DSQL_DATASQUIREL_USER_DATABASES | undefined> {
try {
const {
dbFullName,
dbName,
dbSlug,
dbDescription,
dbImage,
childDatabase,
childDatabaseDbFullName,
} = dbSchema;
let recordedDbEntryArray = userId
? await varDatabaseDbHandler({
queryString: `SELECT * FROM datasquirel.user_databases WHERE db_full_name = ?`,
queryValuesArray: [dbFullName],
})
: undefined;
let recordedDbEntry: DSQL_DATASQUIREL_USER_DATABASES | undefined =
recordedDbEntryArray?.[0];
if (!recordedDbEntry?.id && userId) {
const newDbEntryObj: DSQL_DATASQUIREL_USER_DATABASES = {
user_id: numberfy(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 = (await addDbEntry({
data: newDbEntryObj,
tableName: "user_databases",
forceLocal: true,
})) as PostInsertReturn;
if (newDbEntry.insertId) {
recordedDbEntryArray = await varDatabaseDbHandler({
queryString: `SELECT * FROM datasquirel.user_databases WHERE db_full_name = ?`,
queryValuesArray: [dbFullName],
});
recordedDbEntry = recordedDbEntryArray?.[0];
}
}
return recordedDbEntry;
} catch (error) {
return undefined;
}
}
@@ -0,0 +1,115 @@
import varDatabaseDbHandler from "../utils/varDatabaseDbHandler";
import dbHandler from "../utils/dbHandler";
import {
DSQL_DatabaseSchemaType,
DSQL_TableSchemaType,
PostInsertReturn,
} from "../../types";
import {
DSQL_DATASQUIREL_USER_DATABASE_TABLES,
DSQL_DATASQUIREL_USER_DATABASES,
DsqlTables,
} from "../../types/dsql";
import sqlGenerator from "../../functions/dsql/sql/sql-generator";
import numberfy from "@/package-shared/utils/numberfy";
import addDbEntry from "@/package-shared/functions/backend/db/addDbEntry";
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 async function checkTableRecordCreateDbSchema({
userId,
tableSchema,
dbSchema,
dbRecord,
dbFullName,
}: Param): Promise<DSQL_DATASQUIREL_USER_DATABASE_TABLES | undefined> {
if (!tableSchema) return undefined;
try {
const queryObj = sqlGenerator<DSQL_DATASQUIREL_USER_DATABASE_TABLES>({
tableName: "user_database_tables" as (typeof DsqlTables)[number],
genObject: {
query: {
db_id: {
value: String(dbRecord?.id),
},
table_slug: {
value: tableSchema.tableName,
},
user_id: {
value: String(userId),
},
},
},
dbFullName: "datasquirel",
});
let recordedTableEntryArray = userId
? await varDatabaseDbHandler({
queryString: queryObj?.string || "",
queryValuesArray: queryObj?.values,
})
: undefined;
let recordedTableEntry:
| DSQL_DATASQUIREL_USER_DATABASE_TABLES
| undefined = recordedTableEntryArray?.[0];
if (!recordedTableEntry?.id && userId) {
const newTableInsertObject: DSQL_DATASQUIREL_USER_DATABASE_TABLES =
{
user_id: numberfy(userId),
db_id: dbRecord?.id,
db_slug: dbRecord?.db_slug,
table_name: tableSchema.tableFullName,
table_slug: tableSchema.tableName,
};
if (tableSchema?.childTable && tableSchema.childTableName) {
const parentDb = dbSchema.find(
(db) => db.dbFullName == tableSchema.childTableDbFullName
);
const parentDbTable = 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 = (await addDbEntry({
data: newTableInsertObject,
tableName: "user_database_tables",
dbContext: "Master",
forceLocal: true,
})) as PostInsertReturn;
if (newTableRecordEntry.insertId) {
recordedTableEntryArray = await varDatabaseDbHandler({
queryString: queryObj?.string || "",
queryValuesArray: queryObj?.values,
});
recordedTableEntry = recordedTableEntryArray?.[0];
}
}
return recordedTableEntry;
} catch (error) {
return undefined;
}
}
@@ -0,0 +1,59 @@
import varDatabaseDbHandler from "../utils/varDatabaseDbHandler";
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 async function handleIndexescreateDbFromSchema({
dbFullName,
tableName,
indexes,
}: Param) {
for (let g = 0; g < indexes.length; g++) {
const { indexType, indexName, indexTableFields, alias } = indexes[g];
if (!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: import("../../types").DSQL_MYSQL_SHOW_INDEXES_Type[] =
await varDatabaseDbHandler({
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
*/
await varDatabaseDbHandler({
queryString: `CREATE${
indexType?.match(/fullText/i) ? " FULLTEXT" : ""
} INDEX \`${alias}\` ON \`${dbFullName}\`.\`${tableName}\`(${indexTableFields
?.map((nm) => nm.value)
.map((nm) => `\`${nm}\``)
.join(",")}) COMMENT 'schema_index'`,
});
}
}
}
@@ -1,18 +1,21 @@
import path from "path";
import fs from "fs";
import noDatabaseDbHandler from "./utils/noDatabaseDbHandler";
import varDatabaseDbHandler from "./utils/varDatabaseDbHandler";
import createTable from "./utils/createTable";
import updateTable from "./utils/updateTable";
import dbHandler from "./utils/dbHandler";
import EJSON from "../utils/ejson";
import { DSQL_DatabaseSchemaType } from "../types";
import noDatabaseDbHandler from "../utils/noDatabaseDbHandler";
import varDatabaseDbHandler from "../utils/varDatabaseDbHandler";
import createTable from "../utils/createTable";
import updateTable from "../utils/updateTable";
import dbHandler from "../utils/dbHandler";
import EJSON from "../../utils/ejson";
import { DSQL_DatabaseSchemaType } from "../../types";
import grabDirNames from "../../utils/backend/names/grab-dir-names";
import checkDbRecordCreateDbSchema from "./check-db-record";
import checkTableRecordCreateDbSchema from "./check-table-record";
import handleIndexescreateDbFromSchema from "./handle-indexes";
type Param = {
userId?: number | string | null;
targetDatabase?: string;
dbSchemaData?: import("../types").DSQL_DatabaseSchemaType[];
dbSchemaData?: import("../../types").DSQL_DatabaseSchemaType[];
};
/**
@@ -24,12 +27,11 @@ export default async function createDbFromSchema({
targetDatabase,
dbSchemaData,
}: Param) {
const schemaPath = userId
? path.join(
String(process.env.DSQL_USER_DB_SCHEMA_PATH),
`/user-${userId}/main.json`
)
: path.resolve(__dirname, "../../jsonData/dbSchemas/main.json");
const { userSchemaMainJSONFilePath, mainShemaJSONFilePath } = grabDirNames({
userId,
});
const schemaPath = userSchemaMainJSONFilePath || mainShemaJSONFilePath;
const dbSchema: DSQL_DatabaseSchemaType[] | undefined =
dbSchemaData ||
@@ -42,19 +44,15 @@ export default async function createDbFromSchema({
return;
}
// await createDatabasesFromSchema(dbSchema);
for (let i = 0; i < dbSchema.length; i++) {
const database: DSQL_DatabaseSchemaType = dbSchema[i];
const { dbFullName, tables, dbName, dbSlug, childrenDatabases } =
database;
const { dbFullName, tables, dbSlug, childrenDatabases } = database;
if (targetDatabase && dbFullName != targetDatabase) {
continue;
}
/** @type {any} */
const dbCheck: any = await noDatabaseDbHandler(
`SELECT SCHEMA_NAME AS dbFullName FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '${dbFullName}'`
);
@@ -65,16 +63,14 @@ export default async function createDbFromSchema({
);
}
/**
* Select all tables
* @type {any}
* @description Select All tables in target database
*/
const allTables: any = await noDatabaseDbHandler(
`SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='${dbFullName}'`
);
// let tableDropped;
let recordedDbEntry = await checkDbRecordCreateDbSchema({
dbSchema: database,
userId,
});
for (let tb = 0; tb < allTables.length; tb++) {
const { TABLE_NAME } = allTables[tb];
@@ -116,15 +112,6 @@ export default async function createDbFromSchema({
}
}
const recordedDbEntryArray = userId
? await varDatabaseDbHandler({
queryString: `SELECT * FROM datasquirel.user_databases WHERE db_full_name = ?`,
queryValuesArray: [dbFullName],
})
: undefined;
const recordedDbEntry = recordedDbEntryArray?.[0];
/**
* @description Iterate through each table and perform table actions
*/
@@ -190,11 +177,7 @@ export default async function createDbFromSchema({
});
}
}
////////////////////////////////////////
} else {
////////////////////////////////////////
/**
* @description Create new Table if table doesnt exist
*/
@@ -206,66 +189,28 @@ export default async function createDbFromSchema({
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?.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: import("../types").DSQL_MYSQL_SHOW_INDEXES_Type[] =
await varDatabaseDbHandler({
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
*/
await varDatabaseDbHandler({
queryString: `CREATE${
indexType?.match(/fullText/i)
? " FULLTEXT"
: ""
} INDEX \`${alias}\` ON \`${dbFullName}\`.\`${tableName}\`(${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?.[0]) {
handleIndexescreateDbFromSchema({
dbFullName,
indexes,
tableName,
});
}
}
const tableRecord = await checkTableRecordCreateDbSchema({
dbFullName,
dbSchema,
tableSchema: table,
dbRecord: recordedDbEntry,
userId,
});
}
/**
@@ -25,7 +25,7 @@ export default async function dbHandler({
} else {
results = await CONNECTION.query(query);
}
} catch (/** @type {any} */ error: any) {
} catch (error: any) {
if (process.env.FIRST_RUN) {
return null;
}
+323
View File
@@ -0,0 +1,323 @@
export const DsqlTables = [
"users",
"mariadb_users",
"api_keys",
"invitations",
"user_users",
"delegated_user_tables",
"user_databases",
"user_database_tables",
"user_media",
"delegated_users",
"unsubscribes",
"notifications",
"docs_pages",
"docs_page_extra_links",
"deleted_api_keys",
"servers",
] as const
export type DSQL_DATASQUIREL_USERS = {
id?: number;
uuid?: string;
first_name?: string;
last_name?: string;
uid?: string;
email?: string;
user_type?: string;
user_priviledge?: number;
username?: string;
password?: string;
image?: string;
image_thumbnail?: string;
social_login?: number;
social_platform?: string;
social_id?: string;
mariadb_user?: string;
mariadb_host?: string;
mariadb_pass?: string;
disk_usage_in_mb?: number;
verification_status?: number;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_MARIADB_USERS = {
id?: number;
uuid?: string;
user_id?: number;
username?: string;
host?: string;
password?: string;
primary?: number;
grants?: string;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_API_KEYS = {
id?: number;
uuid?: string;
user_id?: number;
name?: string;
slug?: string;
key?: string;
scope?: string;
csrf?: string;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_INVITATIONS = {
id?: number;
uuid?: string;
inviting_user_id?: number;
invited_user_email?: string;
invitation_status?: string;
database_access?: string;
priviledge?: string;
db_tables_data?: string;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_USER_USERS = {
id?: number;
uuid?: string;
user_id?: number;
invited_user_id?: number;
database?: string;
database_access?: string;
first_name?: string;
last_name?: string;
email?: string;
username?: string;
password?: string;
phone?: string;
user_type?: string;
user_priviledge?: string;
image?: string;
image_thumbnail?: string;
city?: string;
state?: string;
country?: string;
zip_code?: string;
address?: string;
social_login?: number;
social_platform?: string;
social_id?: string;
verification_status?: number;
more_user_data?: string;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_DELEGATED_USER_TABLES = {
id?: number;
uuid?: string;
delegated_user_id?: number;
root_user_id?: number;
database?: string;
table?: string;
priviledge?: string;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_USER_DATABASES = {
id?: number;
uuid?: string;
user_id?: number;
db_name?: string;
db_slug?: string;
db_full_name?: string;
db_image?: string;
db_description?: string;
remote_connected?: number;
remote_connection_type?: string;
remote_db_full_name?: string;
remote_connection_host?: string;
remote_connection_key?: string;
active_clone?: number;
active_clone_parent_db?: string;
active_data?: number;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_USER_DATABASE_TABLES = {
id?: number;
uuid?: string;
user_id?: number;
db_id?: number;
db_slug?: string;
table_name?: string;
table_slug?: string;
table_description?: string;
child_table?: number;
child_table_parent_database?: string;
child_table_parent_table?: string;
active_data?: number;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_USER_MEDIA = {
id?: number;
uuid?: string;
user_id?: number;
media_name?: string;
folder?: string;
media_url?: string;
media_thumbnail_url?: string;
media_path?: string;
media_thumbnail_path?: string;
media_type?: string;
width?: number;
height?: number;
size?: number;
private?: number;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_DELEGATED_USERS = {
id?: number;
uuid?: string;
user_id?: number;
delegated_user_id?: number;
permissions?: string;
permission_level_code?: number;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_UNSUBSCRIBES = {
id?: number;
uuid?: string;
user_id?: number;
email?: string;
type?: string;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_NOTIFICATIONS = {
id?: number;
uuid?: string;
user_id?: number;
title?: string;
message?: string;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_DOCS_PAGES = {
id?: number;
uuid?: string;
title?: string;
slug?: string;
description?: string;
content?: string;
text_content?: string;
level?: number;
page_order?: number;
parent_id?: number;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_DOCS_PAGE_EXTRA_LINKS = {
id?: number;
uuid?: string;
docs_page_id?: number;
title?: string;
description?: string;
url?: string;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_DELETED_API_KEYS = {
id?: number;
uuid?: string;
user_id?: number;
key?: string;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
export type DSQL_DATASQUIREL_SERVERS = {
id?: number;
uuid?: string;
server_id?: number;
ip?: string;
ssh_user?: string;
ssh_port?: number;
date_created?: string;
date_created_code?: number;
date_created_timestamp?: string;
date_updated?: string;
date_updated_code?: number;
date_updated_timestamp?: string;
}
+15 -8
View File
@@ -1479,25 +1479,31 @@ export type DsqlMethodCrudParam<
};
user?: DATASQUIREL_LoggedInUser;
extraData?: T;
transform?: DsqlCrudTransformFunction<T>;
transformData?: DsqlCrudTransformDataFunction<T>;
transformQuery?: DsqlCrudTransformQueryFunction<T>;
existingData?: T;
targetId?: string | number;
sanitize?: (data?: T) => T;
debug?: boolean;
};
export type DsqlCrudTransformFunction<
export type DsqlCrudTransformDataFunction<
T extends { [key: string]: any } = { [key: string]: any }
> = ({
data,
existingData,
user,
}: {
user?: DATASQUIREL_LoggedInUser;
> = (params: {
data: T;
user?: DATASQUIREL_LoggedInUser;
existingData?: T;
reqMethod: (typeof DataCrudRequestMethods)[number];
}) => Promise<T>;
export type DsqlCrudTransformQueryFunction<
T extends { [key: string]: any } = { [key: string]: any }
> = (params: {
query: DsqlCrudQueryObject<T>;
user?: DATASQUIREL_LoggedInUser;
reqMethod: (typeof DataCrudRequestMethods)[number];
}) => Promise<DsqlCrudQueryObject<T>>;
export const DsqlCrudActions = ["insert", "update", "delete", "get"] as const;
export type DsqlCrudQueryObject<
@@ -1515,4 +1521,5 @@ export type DsqlCrudParam<
targetId?: string | number;
query?: DsqlCrudQueryObject<T>;
sanitize?: (data?: T) => T;
debug?: boolean;
};
@@ -0,0 +1,38 @@
import { execSync, ExecSyncOptions } from "child_process";
import os from "os";
export type ExportMariaDBDatabaseParam = {
dbFullName: string;
targetFilePath: string;
mariadbUser?: string;
mariadbHost?: string;
mariadbPass?: string;
};
export default function exportMariadbDatabase({
dbFullName,
targetFilePath,
mariadbHost,
mariadbPass,
mariadbUser,
}: ExportMariaDBDatabaseParam) {
const mysqlDumpPath = os.platform().match(/win/i)
? "'" +
"C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysqldump.exe" +
"'"
: "mysqldump";
const finalMariadbUser = mariadbUser || process.env.DSQL_DB_USERNAME;
const finalMariadbHost = mariadbHost || process.env.DSQL_DB_HOST;
const finalMariadbPass = mariadbPass || process.env.DSQL_DB_PASSWORD;
const cmd = `${mysqlDumpPath} -u ${finalMariadbUser} -h ${finalMariadbHost} -p${finalMariadbPass} ${dbFullName} > ${targetFilePath}`;
let execSyncOptions: ExecSyncOptions = {
encoding: "utf-8",
};
const dumpDb = execSync(cmd, execSyncOptions);
return dumpDb;
}
@@ -0,0 +1,44 @@
import datasquirel from "@moduletrace/datasquirel";
import { execSync, ExecSyncOptions } from "child_process";
import os from "os";
export type ExportMariaDBDatabaseParam = {
dbFullName: string;
targetFilePath: string;
mariadbUser?: string;
mariadbHost?: string;
mariadbPass?: string;
};
export default async function importMariadbDatabase({
dbFullName,
targetFilePath,
mariadbHost,
mariadbPass,
mariadbUser,
}: ExportMariaDBDatabaseParam) {
const mysqlPath = os.platform().match(/win/i)
? "'" +
"C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysql.exe" +
"'"
: "mysql";
const finalMariadbUser = mariadbUser || process.env.DSQL_DB_USERNAME;
const finalMariadbHost = mariadbHost || process.env.DSQL_DB_HOST;
const finalMariadbPass = mariadbPass || process.env.DSQL_DB_PASSWORD;
await datasquirel.utils.connDbHandler(
global.DSQL_DB_CONN,
`CREATE DATABASE IF NOT EXISTS ${dbFullName}`
);
const cmd = `${mysqlPath} -u ${finalMariadbUser} -h ${finalMariadbHost} -p${finalMariadbPass} ${dbFullName} < ${targetFilePath}`;
let execSyncOptions: ExecSyncOptions = {
encoding: "utf-8",
};
const importDb = execSync(cmd, execSyncOptions);
return importDb;
}
@@ -0,0 +1,84 @@
import { DATASQUIREL_LoggedInUser, UserType } from "../../../types";
import path from "path";
type Param = {
user?: DATASQUIREL_LoggedInUser | UserType;
userId?: string | number | null;
};
export default function grabDirNames(param?: Param) {
const appDir = process.env.DSQL_APP_DIR;
const schemasDir = process.env.DSQL_DB_SCHEMA_DIR;
const tempDirName = ".tmp";
if (!appDir)
throw new Error("Please provide the `DSQL_APP_DIR` env variable.");
if (!schemasDir)
throw new Error(
"Please provide the `DSQL_DB_SCHEMA_DIR` env variable."
);
const pakageSharedDir = path.join(appDir, `package-shared`);
const mainDbTypeDefFile = path.join(pakageSharedDir, `types/dsql.ts`);
const mainShemaJSONFilePath = path.join(schemasDir, `main.json`);
const defaultTableFieldsJSONFilePath = path.join(
pakageSharedDir,
`data/defaultFields.json`
);
const usersSchemaDir = path.join(schemasDir, `users`);
const userDirPath = param?.user?.id
? path.join(usersSchemaDir, `user-${param.user.id}`)
: param?.userId
? path.join(usersSchemaDir, `user-${param.userId}`)
: undefined;
const userSchemaMainJSONFilePath = userDirPath
? path.join(userDirPath, `main.json`)
: undefined;
const userPrivateMediaDir = userDirPath
? path.join(userDirPath, `media`)
: undefined;
const userPrivateExportsDir = userDirPath
? path.join(userDirPath, `export`)
: undefined;
const userPrivateSQLExportsDir = userPrivateExportsDir
? path.join(userPrivateExportsDir, `sql`)
: undefined;
const userPrivateTempSQLExportsDir = userPrivateSQLExportsDir
? path.join(userPrivateSQLExportsDir, tempDirName)
: undefined;
const userPrivateTempJSONSchemaFilePath = userPrivateTempSQLExportsDir
? path.join(userPrivateTempSQLExportsDir, `schema.json`)
: undefined;
const userPrivateDbExportZipFileName = `db-export.zip`;
const userPrivateDbExportZipFilePath = userPrivateSQLExportsDir
? path.join(userPrivateSQLExportsDir, userPrivateDbExportZipFileName)
: undefined;
const userPrivateDbImportZipFileName = `db-export.zip`;
const userPrivateDbImportZipFilePath = userPrivateSQLExportsDir
? path.join(userPrivateSQLExportsDir, userPrivateDbImportZipFileName)
: undefined;
return {
schemasDir,
userDirPath,
mainShemaJSONFilePath,
mainDbTypeDefFile,
tempDirName,
defaultTableFieldsJSONFilePath,
usersSchemaDir,
userSchemaMainJSONFilePath,
userPrivateMediaDir,
userPrivateExportsDir,
userPrivateSQLExportsDir,
userPrivateTempSQLExportsDir,
userPrivateTempJSONSchemaFilePath,
userPrivateDbExportZipFileName,
userPrivateDbExportZipFilePath,
userPrivateDbImportZipFileName,
userPrivateDbImportZipFilePath,
};
}
@@ -0,0 +1,14 @@
type Param = {
str: string;
userId: string | number;
};
export default function replaceDatasquirelDbName({
str,
userId,
}: Param): string {
const dbNamePrefix = process.env.DSQL_USER_DB_PREFIX;
const userNameRegex = new RegExp(`${dbNamePrefix}\\d+_`, "g");
const newPrefix = `${dbNamePrefix}${userId}_`;
return str.replace(userNameRegex, newPrefix);
}
@@ -12,6 +12,7 @@ export default async function dsqlCrud<
targetId,
query,
sanitize,
debug,
}: DsqlCrudParam<T>): Promise<
| (PostReturn & {
queryObject?: ReturnType<Awaited<typeof sqlGenerator>>;
@@ -32,6 +33,8 @@ export default async function dsqlCrud<
const GET_RES = await get({
query: queryObject?.string || "",
queryValues: queryObject?.values || [],
debug,
forceLocal: true,
});
return { ...GET_RES, queryObject };
@@ -43,6 +46,7 @@ export default async function dsqlCrud<
table,
data: finalData,
},
forceLocal: true,
});
case "update":
@@ -56,6 +60,7 @@ export default async function dsqlCrud<
identifierValue: String(finalId),
data: finalData,
},
forceLocal: true,
});
case "delete":
@@ -66,6 +71,7 @@ export default async function dsqlCrud<
identifierColumnName: "id",
identifierValue: String(finalId),
},
forceLocal: true,
});
default:
@@ -1,3 +1,4 @@
import _ from "lodash";
import sqlGenerator from "../../functions/dsql/sql/sql-generator";
import {
DsqlCrudQueryObject,
@@ -30,12 +31,14 @@ export default async function dsqlMethodCrud<
addUser,
user,
extraData,
transform,
transformData,
existingData,
body,
query,
targetId,
sanitize,
transformQuery,
debug,
}: DsqlMethodCrudParam<T>): Promise<CRUDResponseObject<P>> {
let result: CRUDResponseObject = {
success: false,
@@ -51,23 +54,28 @@ export default async function dsqlMethodCrud<
let PAGE = 1;
let OFFSET = (PAGE - 1) * LIMIT;
if (finalQuery) {
Object.keys(finalQuery).forEach((key) => {
const value = finalQuery[key];
if (method == "GET") {
const newFinalQuery = _.cloneDeep(
finalQuery || ({} as DsqlCrudQueryObject<T>)
);
Object.keys(newFinalQuery).forEach((key) => {
const value = newFinalQuery[key];
if (typeof value == "string" && value.match(/^\{|^\[/)) {
finalQuery[key] = EJSON.stringify(value);
newFinalQuery[key] = EJSON.stringify(value);
}
if (value == "true") {
finalQuery[key] = true;
newFinalQuery[key] = true;
}
if (value == "false") {
finalQuery[key] = false;
newFinalQuery[key] = false;
}
});
if (finalQuery.limit) LIMIT = numberfy(finalQuery.limit);
if (finalQuery.page) PAGE = numberfy(finalQuery.page);
if (newFinalQuery.limit) LIMIT = numberfy(newFinalQuery.limit);
if (newFinalQuery.page) PAGE = numberfy(newFinalQuery.page);
OFFSET = (PAGE - 1) * LIMIT;
finalQuery = newFinalQuery;
}
let finalData = finalBody
@@ -75,46 +83,65 @@ export default async function dsqlMethodCrud<
...finalBody,
...extraData,
} as T)
: undefined;
: ({} as T);
if (finalData && user?.id && addUser) {
if (user?.id && addUser) {
finalData = {
...finalData,
[addUser.field]: String(user.id),
};
} as T;
}
if (transform && finalData) {
finalData = await transform({
if (transformData) {
if (debug) {
console.log("DEBUG:::transforming Data ...");
}
finalData = (await transformData({
data: finalData,
existingData: existingData,
user,
reqMethod: method,
})) as T;
}
if (transformQuery) {
if (debug) {
console.log("DEBUG:::transforming Query ...");
}
finalQuery = await transformQuery({
query: finalQuery || {},
user,
reqMethod: method,
});
}
if (debug) {
console.log("DEBUG:::finalQuery", finalQuery);
console.log("DEBUG:::finalData", finalData);
}
switch (method) {
case "GET":
const GET_RESULT = await dsqlCrud({
action: "get",
table: tableName,
query: finalQuery
? ({
...finalQuery,
query: {
...finalQuery.query,
...(user?.id && addUser
? {
[addUser.field]: {
value: String(user.id),
},
}
: undefined),
},
limit: LIMIT,
offset: OFFSET,
} as any)
: undefined,
query: {
...finalQuery,
query: {
...finalQuery?.query,
...(user?.id && addUser
? {
[addUser.field]: {
value: String(user.id),
},
}
: undefined),
},
limit: LIMIT,
offset: OFFSET,
} as any,
sanitize,
});
@@ -131,7 +158,10 @@ export default async function dsqlMethodCrud<
const POST_RESULT = await dsqlCrud({
action: "insert",
table: tableName,
data: finalData,
data:
finalData && Object.keys(finalData)?.[0]
? finalData
: undefined,
sanitize,
});
result = {
@@ -146,7 +176,10 @@ export default async function dsqlMethodCrud<
const PUT_RESULT = await dsqlCrud({
action: "update",
table: tableName,
data: finalData,
data:
finalData && Object.keys(finalData)?.[0]
? finalData
: undefined,
targetId,
sanitize,
});
@@ -56,6 +56,7 @@ export default async function connDbHandler<ReturnType = any>(
}
} catch (error: any) {
console.log(`connDbHandler Error: ${error.message}`);
console.log(conn?.config());
return null;
} finally {
conn?.end();