Updates
This commit is contained in:
@@ -76,7 +76,7 @@ export default async function post({
|
||||
}
|
||||
|
||||
return await apiPost({
|
||||
dbFullName: DSQL_DB_NAME,
|
||||
dbFullName: database || DSQL_DB_NAME,
|
||||
query,
|
||||
dbSchema,
|
||||
queryValues,
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import http from "http";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import encrypt from "../../functions/dsql/encrypt";
|
||||
@@ -9,40 +8,11 @@ import { writeAuthFile } from "../../functions/backend/auth/write-auth-files";
|
||||
import {
|
||||
APILoginFunctionReturn,
|
||||
DSQL_DatabaseSchemaType,
|
||||
LoginUserParam,
|
||||
PackageUserLoginRequestBody,
|
||||
} from "../../types";
|
||||
import debugLog from "../../utils/logging/debug-log";
|
||||
import grabCookieExpiryDate from "../../utils/grab-cookie-expirt-date";
|
||||
import emailRegexCheck from "../../functions/email/verification/email-regex-test";
|
||||
import emailMxLookup from "../../functions/email/verification/email-mx-lookup";
|
||||
import validateEmail from "../../functions/email/fns/validate-email";
|
||||
|
||||
type Param = {
|
||||
key?: string;
|
||||
database: string;
|
||||
payload: {
|
||||
email?: string;
|
||||
username?: string;
|
||||
password?: string;
|
||||
};
|
||||
additionalFields?: string[];
|
||||
request?: http.IncomingMessage & { [s: string]: any };
|
||||
response?: http.ServerResponse & { [s: string]: any };
|
||||
encryptionKey?: string;
|
||||
encryptionSalt?: string;
|
||||
email_login?: boolean;
|
||||
email_login_code?: string;
|
||||
temp_code_field?: string;
|
||||
token?: boolean;
|
||||
user_id?: string | number;
|
||||
skipPassword?: boolean;
|
||||
debug?: boolean;
|
||||
skipWriteAuthFile?: boolean;
|
||||
apiUserID?: string | number;
|
||||
dbUserId?: string | number;
|
||||
cleanupTokens?: boolean;
|
||||
secureCookie?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Login A user
|
||||
@@ -68,7 +38,7 @@ export default async function loginUser({
|
||||
cleanupTokens,
|
||||
secureCookie,
|
||||
request,
|
||||
}: Param): Promise<APILoginFunctionReturn> {
|
||||
}: LoginUserParam): Promise<APILoginFunctionReturn> {
|
||||
const grabedHostNames = grabHostNames({ userId: user_id || apiUserID });
|
||||
const { host, port, scheme } = grabedHostNames;
|
||||
const COOKIE_EXPIRY_DATE = grabCookieExpiryDate();
|
||||
@@ -156,7 +126,7 @@ export default async function loginUser({
|
||||
} catch (error) {}
|
||||
|
||||
httpResponse = await apiLoginUser({
|
||||
database: process.env.DSQL_DB_NAME || "",
|
||||
database: database || process.env.DSQL_DB_NAME || "",
|
||||
email: payload.email,
|
||||
username: payload.username,
|
||||
password: payload.password,
|
||||
|
||||
@@ -23,20 +23,24 @@ type Param = {
|
||||
/**
|
||||
* # Send Email Code to a User
|
||||
*/
|
||||
export default async function sendEmailCode({
|
||||
key,
|
||||
email,
|
||||
database,
|
||||
temp_code_field_name,
|
||||
mail_domain,
|
||||
mail_password,
|
||||
mail_username,
|
||||
mail_port,
|
||||
sender,
|
||||
user_id,
|
||||
response,
|
||||
extraCookies,
|
||||
}: Param): Promise<SendOneTimeCodeEmailResponse> {
|
||||
export default async function sendEmailCode(
|
||||
params: Param
|
||||
): Promise<SendOneTimeCodeEmailResponse> {
|
||||
const {
|
||||
key,
|
||||
email,
|
||||
database,
|
||||
temp_code_field_name,
|
||||
mail_domain,
|
||||
mail_password,
|
||||
mail_username,
|
||||
mail_port,
|
||||
sender,
|
||||
user_id,
|
||||
response,
|
||||
extraCookies,
|
||||
} = params;
|
||||
|
||||
const grabedHostNames = grabHostNames();
|
||||
const { host, port, scheme } = grabedHostNames;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import decrypt from "../../functions/dsql/decrypt";
|
||||
import getAuthCookieNames from "../../functions/backend/cookies/get-auth-cookie-names";
|
||||
import { checkAuthFile } from "../../functions/backend/auth/write-auth-files";
|
||||
import parseCookies from "../../utils/backend/parseCookies";
|
||||
import { AuthenticatedUser } from "../../types";
|
||||
import { AuthenticatedUser, DATASQUIREL_LoggedInUser } from "../../types";
|
||||
import getCsrfHeaderName from "../../actions/get-csrf-header-name";
|
||||
import grabHostNames from "../../utils/grab-host-names";
|
||||
import debugLog from "../../utils/logging/debug-log";
|
||||
@@ -128,8 +128,7 @@ export default function userAuth({
|
||||
};
|
||||
}
|
||||
|
||||
let userObject: import("../../types").DATASQUIREL_LoggedInUser =
|
||||
JSON.parse(userPayloadJSON);
|
||||
let userObject: DATASQUIREL_LoggedInUser = JSON.parse(userPayloadJSON);
|
||||
|
||||
if (debug) {
|
||||
debugLog({
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import path from "path";
|
||||
import queryDSQLAPI from "../../functions/api/query-dsql-api";
|
||||
import { DsqlCrudQueryObject, SQLDeleteData } from "../../types";
|
||||
import grabAPIBasePath from "../../utils/grab-api-base-path";
|
||||
|
||||
type Params<T extends { [key: string]: any } = { [key: string]: any }> = {
|
||||
dbName: string;
|
||||
tableName: string;
|
||||
deleteSpec?: T & { deleteKeyValues?: SQLDeleteData<T>[] };
|
||||
targetID?: string | number;
|
||||
};
|
||||
|
||||
export default async function apiCrudDELETE<
|
||||
T extends { [key: string]: any } = { [key: string]: any }
|
||||
>({ dbName, tableName, deleteSpec, targetID }: Params<T>) {
|
||||
const basePath = grabAPIBasePath({ paradigm: "crud" });
|
||||
|
||||
const finalID = typeof targetID === "number" ? String(targetID) : targetID;
|
||||
|
||||
const finalPath = path.join(basePath, dbName, tableName, finalID || "");
|
||||
|
||||
const GET_RES = await queryDSQLAPI<DsqlCrudQueryObject<T>>({
|
||||
method: "DELETE",
|
||||
path: finalPath,
|
||||
body: deleteSpec,
|
||||
});
|
||||
|
||||
return GET_RES;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import path from "path";
|
||||
import queryDSQLAPI from "../../functions/api/query-dsql-api";
|
||||
import { APIResponseObject, DsqlCrudQueryObject } from "../../types";
|
||||
import grabAPIBasePath from "../../utils/grab-api-base-path";
|
||||
|
||||
type Params<T extends { [key: string]: any } = { [key: string]: any }> = {
|
||||
dbName: string;
|
||||
tableName: string;
|
||||
query?: DsqlCrudQueryObject<T>;
|
||||
targetId?: string | number;
|
||||
};
|
||||
|
||||
export default async function apiCrudGET<
|
||||
T extends { [key: string]: any } = { [key: string]: any }
|
||||
>({
|
||||
dbName,
|
||||
tableName,
|
||||
query,
|
||||
targetId,
|
||||
}: Params<T>): Promise<APIResponseObject> {
|
||||
const basePath = grabAPIBasePath({ paradigm: "crud" });
|
||||
|
||||
const finalID = typeof targetId === "number" ? String(targetId) : targetId;
|
||||
|
||||
const finalPath = path.join(basePath, dbName, tableName, finalID || "");
|
||||
|
||||
const GET_RES = await queryDSQLAPI<DsqlCrudQueryObject<T>>({
|
||||
method: "GET",
|
||||
path: finalPath,
|
||||
query,
|
||||
});
|
||||
|
||||
return GET_RES;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import apiCrudGET from "./get";
|
||||
import apiCrudPOST from "./post";
|
||||
import apiCrudPUT from "./put";
|
||||
import apiCrudDELETE from "./delete";
|
||||
|
||||
const crud = {
|
||||
get: apiCrudGET,
|
||||
insert: apiCrudPOST,
|
||||
update: apiCrudPUT,
|
||||
delete: apiCrudDELETE,
|
||||
options: async () => {},
|
||||
};
|
||||
|
||||
export default crud;
|
||||
@@ -0,0 +1,42 @@
|
||||
import path from "path";
|
||||
import queryDSQLAPI from "../../functions/api/query-dsql-api";
|
||||
import { APIResponseObject, DsqlCrudQueryObject } from "../../types";
|
||||
import grabAPIBasePath from "../../utils/grab-api-base-path";
|
||||
|
||||
export type APICrudPostParams<
|
||||
T extends { [key: string]: any } = { [key: string]: any }
|
||||
> = {
|
||||
dbName: string;
|
||||
tableName: string;
|
||||
body: T;
|
||||
update?: boolean;
|
||||
};
|
||||
|
||||
export default async function apiCrudPOST<
|
||||
T extends { [key: string]: any } = { [key: string]: any }
|
||||
>({
|
||||
dbName,
|
||||
tableName,
|
||||
body,
|
||||
update,
|
||||
}: APICrudPostParams<T>): Promise<APIResponseObject> {
|
||||
const basePath = grabAPIBasePath({ paradigm: "crud" });
|
||||
|
||||
const passedID = body.id as string | number | undefined;
|
||||
|
||||
const finalID = update
|
||||
? typeof passedID === "number"
|
||||
? String(passedID)
|
||||
: passedID
|
||||
: undefined;
|
||||
|
||||
const finalPath = path.join(basePath, dbName, tableName, finalID || "");
|
||||
|
||||
const GET_RES = await queryDSQLAPI<DsqlCrudQueryObject<T>>({
|
||||
method: update ? "PUT" : "POST",
|
||||
path: finalPath,
|
||||
body,
|
||||
});
|
||||
|
||||
return GET_RES;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import apiCrudPOST, { APICrudPostParams } from "./post";
|
||||
|
||||
type Params<T extends { [key: string]: any } = { [key: string]: any }> = Omit<
|
||||
APICrudPostParams<T>,
|
||||
"update"
|
||||
> & {
|
||||
targetID: string | number;
|
||||
};
|
||||
|
||||
export default async function apiCrudPUT<
|
||||
T extends { [key: string]: any } = { [key: string]: any }
|
||||
>({ dbName, tableName, body, targetID }: Params<T>) {
|
||||
const updatedBody = { ...body } as any;
|
||||
|
||||
if (targetID) {
|
||||
updatedBody["id"] = targetID;
|
||||
}
|
||||
|
||||
return await apiCrudPOST({
|
||||
dbName,
|
||||
tableName,
|
||||
body: updatedBody,
|
||||
update: true,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import queryDSQLAPI from "../../functions/api/query-dsql-api";
|
||||
import { APIResponseObject } from "../../types";
|
||||
import { DSQL_DATASQUIREL_USER_MEDIA } from "../../types/dsql";
|
||||
import path from "path";
|
||||
import grabAPIBasePath from "../../utils/grab-api-base-path";
|
||||
|
||||
export default async function apiMediaDELETE(params: {
|
||||
mediaID?: string | number;
|
||||
}): Promise<
|
||||
APIResponseObject<
|
||||
DSQL_DATASQUIREL_USER_MEDIA | DSQL_DATASQUIREL_USER_MEDIA[]
|
||||
>
|
||||
> {
|
||||
const basePath = grabAPIBasePath({ paradigm: "media" });
|
||||
|
||||
const mediaID = params.mediaID
|
||||
? typeof params.mediaID === "number"
|
||||
? String(params.mediaID)
|
||||
: params.mediaID
|
||||
: undefined;
|
||||
|
||||
const finalPath = path.join(basePath, mediaID || "");
|
||||
|
||||
const DELETE_MEDIA_RES = await queryDSQLAPI({
|
||||
method: "DELETE",
|
||||
path: finalPath,
|
||||
});
|
||||
|
||||
return DELETE_MEDIA_RES as APIResponseObject<
|
||||
DSQL_DATASQUIREL_USER_MEDIA | DSQL_DATASQUIREL_USER_MEDIA[]
|
||||
>;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import queryDSQLAPI from "../../functions/api/query-dsql-api";
|
||||
import { APIGetMediaParams, APIResponseObject } from "../../types";
|
||||
import path from "path";
|
||||
import { DSQL_DATASQUIREL_USER_MEDIA } from "../../types/dsql";
|
||||
import grabAPIBasePath from "../../utils/grab-api-base-path";
|
||||
|
||||
export default async function apiMediaGET(
|
||||
params: APIGetMediaParams
|
||||
): Promise<
|
||||
APIResponseObject<
|
||||
DSQL_DATASQUIREL_USER_MEDIA | DSQL_DATASQUIREL_USER_MEDIA[]
|
||||
>
|
||||
> {
|
||||
const basePath = grabAPIBasePath({ paradigm: "media" });
|
||||
|
||||
const mediaID = params.mediaID
|
||||
? typeof params.mediaID === "number"
|
||||
? String(params.mediaID)
|
||||
: params.mediaID
|
||||
: undefined;
|
||||
|
||||
const finalPath = path.join(basePath, mediaID || "");
|
||||
|
||||
const GET_MEDIA_RES = await queryDSQLAPI<APIGetMediaParams>({
|
||||
method: "GET",
|
||||
path: finalPath,
|
||||
query: params,
|
||||
});
|
||||
|
||||
return GET_MEDIA_RES as APIResponseObject<
|
||||
DSQL_DATASQUIREL_USER_MEDIA | DSQL_DATASQUIREL_USER_MEDIA[]
|
||||
>;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import apiMediaGET from "./get";
|
||||
import apiMediaPOST from "./post";
|
||||
import apiMediaDELETE from "./delete";
|
||||
|
||||
const media = {
|
||||
get: apiMediaGET,
|
||||
add: apiMediaPOST,
|
||||
delete: apiMediaDELETE,
|
||||
};
|
||||
|
||||
export default media;
|
||||
@@ -0,0 +1,24 @@
|
||||
import queryDSQLAPI from "../../functions/api/query-dsql-api";
|
||||
import { AddMediaAPIBody, APIResponseObject } from "../../types";
|
||||
import { DSQL_DATASQUIREL_USER_MEDIA } from "../../types/dsql";
|
||||
import grabAPIBasePath from "../../utils/grab-api-base-path";
|
||||
|
||||
export default async function apiMediaPOST(
|
||||
params: AddMediaAPIBody
|
||||
): Promise<
|
||||
APIResponseObject<
|
||||
DSQL_DATASQUIREL_USER_MEDIA | DSQL_DATASQUIREL_USER_MEDIA[]
|
||||
>
|
||||
> {
|
||||
const basePath = grabAPIBasePath({ paradigm: "media" });
|
||||
|
||||
const POST_MEDIA_RES = await queryDSQLAPI<AddMediaAPIBody>({
|
||||
method: "POST",
|
||||
path: basePath,
|
||||
body: params,
|
||||
});
|
||||
|
||||
return POST_MEDIA_RES as APIResponseObject<
|
||||
DSQL_DATASQUIREL_USER_MEDIA | DSQL_DATASQUIREL_USER_MEDIA[]
|
||||
>;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
const user = {};
|
||||
|
||||
export default user;
|
||||
@@ -0,0 +1,105 @@
|
||||
const DataTypes = [
|
||||
{
|
||||
title: "VARCHAR",
|
||||
name: "VARCHAR",
|
||||
value: "0-255",
|
||||
argument: true,
|
||||
description:
|
||||
"Varchar is simply letters and numbers within the range 0 - 255",
|
||||
maxValue: 255,
|
||||
},
|
||||
{
|
||||
title: "TINYINT",
|
||||
name: "TINYINT",
|
||||
value: "0-100",
|
||||
description: "TINYINT means Integers: 0 to 100",
|
||||
maxValue: 127,
|
||||
},
|
||||
{
|
||||
title: "SMALLINT",
|
||||
name: "SMALLINT",
|
||||
value: "0-255",
|
||||
description: "SMALLINT means Integers: 0 to 240933",
|
||||
maxValue: 32767,
|
||||
},
|
||||
{
|
||||
title: "MEDIUMINT",
|
||||
name: "MEDIUMINT",
|
||||
value: "0-255",
|
||||
description: "MEDIUMINT means Integers: 0 to 1245568545560",
|
||||
maxValue: 8388607,
|
||||
},
|
||||
{
|
||||
title: "INT",
|
||||
name: "INT",
|
||||
value: "0-255",
|
||||
description: "INT means Integers: 0 to 12560",
|
||||
maxValue: 2147483647,
|
||||
},
|
||||
{
|
||||
title: "BIGINT",
|
||||
name: "BIGINT",
|
||||
value: "0-255",
|
||||
description: "BIGINT means Integers: 0 to 1245569056767568545560",
|
||||
maxValue: 2e63,
|
||||
},
|
||||
{
|
||||
title: "TINYTEXT",
|
||||
name: "TINYTEXT",
|
||||
value: "0-255",
|
||||
description: "Text with 255 max characters",
|
||||
maxValue: 127,
|
||||
},
|
||||
{
|
||||
title: "TEXT",
|
||||
name: "TEXT",
|
||||
value: "0-100",
|
||||
description: "MEDIUMTEXT is just text with max length 16,777,215",
|
||||
},
|
||||
{
|
||||
title: "MEDIUMTEXT",
|
||||
name: "MEDIUMTEXT",
|
||||
value: "0-255",
|
||||
description: "MEDIUMTEXT is just text with max length 16,777,215",
|
||||
},
|
||||
{
|
||||
title: "LONGTEXT",
|
||||
name: "LONGTEXT",
|
||||
value: "0-255",
|
||||
description: "LONGTEXT is just text with max length 4,294,967,295",
|
||||
},
|
||||
{
|
||||
title: "DECIMAL",
|
||||
name: "DECIMAL",
|
||||
description: "Numbers with decimals",
|
||||
integer: "1-100",
|
||||
decimals: "1-4",
|
||||
},
|
||||
{
|
||||
title: "FLOAT",
|
||||
name: "FLOAT",
|
||||
description: "Numbers with decimals",
|
||||
integer: "1-100",
|
||||
decimals: "1-4",
|
||||
},
|
||||
{
|
||||
title: "DOUBLE",
|
||||
name: "DOUBLE",
|
||||
description: "Numbers with decimals",
|
||||
integer: "1-100",
|
||||
decimals: "1-4",
|
||||
},
|
||||
{
|
||||
title: "UUID",
|
||||
name: "UUID",
|
||||
valueLiteral: "UUID()",
|
||||
description: "A Unique ID",
|
||||
},
|
||||
{
|
||||
title: "TIMESTAMP",
|
||||
name: "TIMESTAMP",
|
||||
description: "Time Stamp",
|
||||
},
|
||||
] as const;
|
||||
|
||||
export default DataTypes;
|
||||
@@ -88,6 +88,14 @@
|
||||
"integer": "1-100",
|
||||
"decimals": "1-4"
|
||||
},
|
||||
{
|
||||
"title": "OPTIONS",
|
||||
"name": "VARCHAR",
|
||||
"value": "250",
|
||||
"argument": true,
|
||||
"description": "This is a custom field which is a varchar under the hood",
|
||||
"maxValue": 255
|
||||
},
|
||||
{
|
||||
"title": "UUID",
|
||||
"name": "UUID",
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
export const AppNames = {
|
||||
MaxScaleUserName: "dsql_maxscale_user",
|
||||
ReplicaUserName: "dsql_replication_user",
|
||||
DsqlDbPrefix: "datasquirel_user_",
|
||||
PrivateMediaProceedureName: "dsql_UpdateUserMedia",
|
||||
PrivateMediaInsertTriggerName: "dsql_trg_user_private_folders_insert",
|
||||
PrivateMediaDeleteTriggerName: "dsql_trg_user_private_folders_delete",
|
||||
} as const;
|
||||
@@ -0,0 +1,5 @@
|
||||
export const CookieNames = {
|
||||
OneTimeLoginEmail: "dsql-one-time-login-email",
|
||||
DelegatedUserId: "dsql-delegated-user-id",
|
||||
DelegatedDatabase: "dsql-delegated-database",
|
||||
} as const;
|
||||
@@ -0,0 +1,9 @@
|
||||
import getCsrfHeaderName from "../actions/get-csrf-header-name";
|
||||
|
||||
export const LocalStorageDict = {
|
||||
OneTimeEmail: "dsql-one-time-login-email",
|
||||
User: "user",
|
||||
CSRF: getCsrfHeaderName(),
|
||||
CurrentQueue: "current_queue",
|
||||
DiskUsage: "disk_usage",
|
||||
};
|
||||
@@ -0,0 +1,7 @@
|
||||
const ResourceLimits = {
|
||||
user_databases: 20,
|
||||
table_entries: 20,
|
||||
general: 20,
|
||||
} as const;
|
||||
|
||||
export default ResourceLimits;
|
||||
@@ -0,0 +1,116 @@
|
||||
import path from "path";
|
||||
|
||||
import { OutgoingHttpHeaders } from "http";
|
||||
import {
|
||||
APIResponseObject,
|
||||
DataCrudRequestMethods,
|
||||
DataCrudRequestMethodsLowerCase,
|
||||
} from "../../types";
|
||||
import grabHostNames from "../../utils/grab-host-names";
|
||||
import serializeQuery from "../../utils/serialize-query";
|
||||
|
||||
type Param<T = { [k: string]: any }> = {
|
||||
key?: string;
|
||||
body?: T;
|
||||
query?: T;
|
||||
useDefault?: boolean;
|
||||
path: string;
|
||||
method?:
|
||||
| (typeof DataCrudRequestMethods)[number]
|
||||
| (typeof DataCrudRequestMethodsLowerCase)[number];
|
||||
};
|
||||
|
||||
/**
|
||||
* # Query DSQL API
|
||||
*/
|
||||
export default async function queryDSQLAPI<
|
||||
T = { [k: string]: any },
|
||||
P = { [k: string]: any }
|
||||
>({
|
||||
key,
|
||||
body,
|
||||
query,
|
||||
useDefault,
|
||||
path: passedPath,
|
||||
method,
|
||||
}: Param<T>): Promise<APIResponseObject<P>> {
|
||||
const grabedHostNames = grabHostNames({ useDefault });
|
||||
const { host, port, scheme } = grabedHostNames;
|
||||
|
||||
try {
|
||||
/**
|
||||
* Make https request
|
||||
*
|
||||
* @description make a request to datasquirel.com
|
||||
*/
|
||||
const httpResponse = await new Promise((resolve, reject) => {
|
||||
const reqPayload = body ? JSON.stringify(body) : undefined;
|
||||
|
||||
let headers: OutgoingHttpHeaders = {
|
||||
"Content-Type": "application/json",
|
||||
Authorization:
|
||||
key ||
|
||||
(!method || method == "GET" || method == "get"
|
||||
? process.env.DSQL_READ_ONLY_API_KEY
|
||||
: undefined) ||
|
||||
process.env.DSQL_FULL_ACCESS_API_KEY ||
|
||||
process.env.DSQL_API_KEY,
|
||||
};
|
||||
|
||||
if (reqPayload) {
|
||||
headers["Content-Length"] = Buffer.from(reqPayload).length;
|
||||
}
|
||||
|
||||
let finalPath = path.join("/", passedPath);
|
||||
|
||||
if (query) {
|
||||
const queryString = serializeQuery(query);
|
||||
finalPath += `${queryString}`;
|
||||
}
|
||||
|
||||
const httpsRequest = scheme.request(
|
||||
{
|
||||
method: method || "GET",
|
||||
headers,
|
||||
port,
|
||||
hostname: host,
|
||||
path: finalPath,
|
||||
},
|
||||
|
||||
/**
|
||||
* Callback Function
|
||||
*
|
||||
* @description https request callback
|
||||
*/
|
||||
(response) => {
|
||||
var str = "";
|
||||
|
||||
response.on("data", function (chunk) {
|
||||
str += chunk;
|
||||
});
|
||||
|
||||
response.on("end", function () {
|
||||
resolve(JSON.parse(str));
|
||||
});
|
||||
|
||||
response.on("error", (err) => {
|
||||
reject(err);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
if (reqPayload) {
|
||||
httpsRequest.write(reqPayload);
|
||||
}
|
||||
httpsRequest.end();
|
||||
});
|
||||
|
||||
return httpResponse as APIResponseObject<P>;
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
payload: undefined,
|
||||
msg: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -73,11 +73,8 @@ export default async function apiGet<
|
||||
if (targetTable) {
|
||||
const clonedTargetTable = _.cloneDeep(targetTable);
|
||||
delete clonedTargetTable.childTable;
|
||||
delete clonedTargetTable.childTableDbFullName;
|
||||
delete clonedTargetTable.childTableName;
|
||||
delete clonedTargetTable.childrenTables;
|
||||
delete clonedTargetTable.updateData;
|
||||
delete clonedTargetTable.tableNameOld;
|
||||
delete clonedTargetTable.indexes;
|
||||
tableSchema = clonedTargetTable;
|
||||
}
|
||||
|
||||
@@ -48,9 +48,9 @@ export default async function apiPost({
|
||||
*/
|
||||
try {
|
||||
let { result, error } = await runQuery({
|
||||
dbFullName: dbFullName,
|
||||
query: query,
|
||||
dbSchema: dbSchema,
|
||||
dbFullName,
|
||||
query,
|
||||
dbSchema,
|
||||
queryValuesArray: queryValues,
|
||||
tableName,
|
||||
dbContext,
|
||||
@@ -89,11 +89,8 @@ export default async function apiPost({
|
||||
const clonedTargetTable = _.cloneDeep(targetTable);
|
||||
|
||||
delete clonedTargetTable.childTable;
|
||||
delete clonedTargetTable.childTableDbFullName;
|
||||
delete clonedTargetTable.childTableName;
|
||||
delete clonedTargetTable.childrenTables;
|
||||
delete clonedTargetTable.updateData;
|
||||
delete clonedTargetTable.tableNameOld;
|
||||
delete clonedTargetTable.indexes;
|
||||
|
||||
tableSchema = clonedTargetTable;
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
APILoginFunctionReturn,
|
||||
HandleSocialDbFunctionParams,
|
||||
} from "../../../types";
|
||||
import grabDirNames from "../../../utils/backend/names/grab-dir-names";
|
||||
|
||||
/**
|
||||
* # Handle Social DB
|
||||
@@ -151,15 +152,15 @@ export default async function handleSocialDb({
|
||||
},
|
||||
});
|
||||
|
||||
if (newUser?.insertId) {
|
||||
if (newUser?.payload?.insertId) {
|
||||
if (!database) {
|
||||
/**
|
||||
* Add a Mariadb User for this User
|
||||
*/
|
||||
await addMariadbUser({ userId: newUser.insertId });
|
||||
await addMariadbUser({ userId: newUser.payload.insertId });
|
||||
}
|
||||
|
||||
const newUserQueriedQuery = `SELECT * FROM ${dbAppend}users WHERE id='${newUser.insertId}'`;
|
||||
const newUserQueriedQuery = `SELECT * FROM ${dbAppend}users WHERE id='${newUser.payload.insertId}'`;
|
||||
|
||||
const newUserQueried = await varDatabaseDbHandler({
|
||||
database: finalDbName,
|
||||
@@ -182,7 +183,7 @@ export default async function handleSocialDb({
|
||||
*/
|
||||
let generatedToken = encrypt({
|
||||
data: JSON.stringify({
|
||||
id: newUser.insertId,
|
||||
id: newUser.payload.insertId,
|
||||
email: supEmail,
|
||||
dateCode: Date.now(),
|
||||
}),
|
||||
@@ -202,7 +203,7 @@ export default async function handleSocialDb({
|
||||
}).then(() => {});
|
||||
}
|
||||
|
||||
const STATIC_ROOT = process.env.DSQL_STATIC_SERVER_DIR;
|
||||
const { STATIC_ROOT } = grabDirNames();
|
||||
|
||||
if (!STATIC_ROOT) {
|
||||
console.log("Static File ENV not Found!");
|
||||
@@ -219,11 +220,11 @@ export default async function handleSocialDb({
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
if (!database || database?.match(/^datasquirel$/)) {
|
||||
let newUserSchemaFolderPath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${newUser.insertId}`;
|
||||
let newUserSchemaFolderPath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${newUser.payload.insertId}`;
|
||||
|
||||
let newUserMediaFolderPath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}`
|
||||
`images/user-images/user-${newUser.payload.insertId}`
|
||||
);
|
||||
|
||||
fs.mkdirSync(newUserSchemaFolderPath);
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
// @ts-check
|
||||
|
||||
import { findDbNameInSchemaDir } from "../../../shell/createDbFromSchema/grab-required-database-schemas";
|
||||
import { APICreateUserFunctionParams } from "../../../types";
|
||||
import addUsersTableToDb from "../../backend/addUsersTableToDb";
|
||||
import addDbEntry from "../../backend/db/addDbEntry";
|
||||
@@ -39,6 +38,19 @@ export default async function apiCreateUser({
|
||||
};
|
||||
}
|
||||
|
||||
const targetDbSchema = findDbNameInSchemaDir({
|
||||
dbName: dbFullName,
|
||||
userId,
|
||||
});
|
||||
|
||||
if (!targetDbSchema?.id) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "targetDbSchema not found",
|
||||
payload: null,
|
||||
};
|
||||
}
|
||||
|
||||
const hashedPassword = hashPassword({
|
||||
encryptionKey: finalEncryptionKey,
|
||||
password: String(payload.password),
|
||||
@@ -57,8 +69,8 @@ export default async function apiCreateUser({
|
||||
const newTable = await addUsersTableToDb({
|
||||
userId: Number(API_USER_ID),
|
||||
database: dbFullName,
|
||||
|
||||
payload: payload,
|
||||
dbId: targetDbSchema.id,
|
||||
});
|
||||
|
||||
fields = await varDatabaseDbHandler({
|
||||
@@ -87,6 +99,7 @@ export default async function apiCreateUser({
|
||||
newPayload: {
|
||||
[key]: payload[key],
|
||||
},
|
||||
dbId: targetDbSchema.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -143,8 +156,8 @@ export default async function apiCreateUser({
|
||||
},
|
||||
});
|
||||
|
||||
if (addUser?.insertId) {
|
||||
const newlyAddedUserQuery = `SELECT id,uuid,first_name,last_name,email,username,image,image_thumbnail,verification_status FROM ${dbFullName}.users WHERE id='${addUser.insertId}'`;
|
||||
if (addUser?.payload?.insertId) {
|
||||
const newlyAddedUserQuery = `SELECT id,uuid,first_name,last_name,email,username,image,image_thumbnail,verification_status FROM ${dbFullName}.users WHERE id='${addUser.payload.insertId}'`;
|
||||
|
||||
const newlyAddedUser = await varDatabaseDbHandler({
|
||||
queryString: newlyAddedUserQuery,
|
||||
|
||||
@@ -26,6 +26,14 @@ export default async function apiLoginUser({
|
||||
debug,
|
||||
}: APILoginFunctionParams): Promise<APILoginFunctionReturn> {
|
||||
const dbFullName = grabDbFullName({ dbName: database, userId: dbUserId });
|
||||
|
||||
if (!dbFullName) {
|
||||
console.log(`Database Full Name couldn't be grabbed`);
|
||||
return {
|
||||
success: false,
|
||||
msg: `Database Full Name couldn't be grabbed`,
|
||||
};
|
||||
}
|
||||
const dbAppend = global.DSQL_USE_LOCAL ? "" : `${dbFullName}.`;
|
||||
|
||||
/**
|
||||
@@ -152,6 +160,8 @@ export default async function apiLoginUser({
|
||||
|
||||
let userPayload: DATASQUIREL_LoggedInUser = {
|
||||
id: foundUser[0].id,
|
||||
uid: foundUser[0].uid,
|
||||
uuid: foundUser[0].uuid,
|
||||
first_name: foundUser[0].first_name,
|
||||
last_name: foundUser[0].last_name,
|
||||
username: foundUser[0].username,
|
||||
|
||||
@@ -30,11 +30,13 @@ export default async function apiSendResetPasswordLink({
|
||||
}: Param): Promise<Return> {
|
||||
const dbFullName = grabDbFullName({ dbName: database, userId: dbUserId });
|
||||
|
||||
/**
|
||||
* Check input validity
|
||||
*
|
||||
* @description Check input validity
|
||||
*/
|
||||
if (!dbFullName) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Couldn't get database full name`,
|
||||
};
|
||||
}
|
||||
|
||||
if (email?.match(/ /)) {
|
||||
return {
|
||||
success: false,
|
||||
|
||||
@@ -4,6 +4,7 @@ import NO_DB_HANDLER from "../../utils/backend/global-db/NO_DB_HANDLER";
|
||||
import addDbEntry from "./db/addDbEntry";
|
||||
import encrypt from "../dsql/encrypt";
|
||||
import LOCAL_DB_HANDLER from "../../utils/backend/global-db/LOCAL_DB_HANDLER";
|
||||
import grabSQLKeyName from "../../utils/grab-sql-key-name";
|
||||
|
||||
type Param = {
|
||||
userId: number | string;
|
||||
@@ -16,7 +17,7 @@ export default async function addMariadbUser({ userId }: Param): Promise<any> {
|
||||
try {
|
||||
const defaultMariadbUserHost = process.env.DSQL_DB_HOST || "127.0.0.1";
|
||||
|
||||
const username = `dsql_user_${userId}`;
|
||||
const username = grabSQLKeyName({ type: "user", userId });
|
||||
const password = generator.generate({
|
||||
length: 16,
|
||||
numbers: true,
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
import serverError from "./serverError";
|
||||
import DB_HANDLER from "../../utils/backend/global-db/DB_HANDLER";
|
||||
import { default as grabUserSchemaData } from "./grabUserSchemaData";
|
||||
import { default as setUserSchemaData } from "./setUserSchemaData";
|
||||
import addDbEntry from "./db/addDbEntry";
|
||||
import createDbFromSchema from "../../shell/createDbFromSchema";
|
||||
import LOCAL_DB_HANDLER from "../../utils/backend/global-db/LOCAL_DB_HANDLER";
|
||||
import grabNewUsersTableSchema from "./grabNewUsersTableSchema";
|
||||
import {
|
||||
grabPrimaryRequiredDbSchema,
|
||||
writeUpdatedDbSchema,
|
||||
} from "../../shell/createDbFromSchema/grab-required-database-schemas";
|
||||
|
||||
type Param = {
|
||||
userId: number;
|
||||
database: string;
|
||||
payload?: { [s: string]: any };
|
||||
dbId: string | number;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -20,6 +23,7 @@ export default async function addUsersTableToDb({
|
||||
userId,
|
||||
database,
|
||||
payload,
|
||||
dbId,
|
||||
}: Param): Promise<any> {
|
||||
try {
|
||||
const dbFullName = database;
|
||||
@@ -27,12 +31,10 @@ export default async function addUsersTableToDb({
|
||||
const userPreset = grabNewUsersTableSchema({ payload });
|
||||
if (!userPreset) throw new Error("Couldn't Get User Preset!");
|
||||
|
||||
const userSchemaData = grabUserSchemaData({ userId });
|
||||
if (!userSchemaData) throw new Error("User schema data not found!");
|
||||
|
||||
let targetDatabase = userSchemaData.find(
|
||||
(db: any) => db.dbFullName === database
|
||||
);
|
||||
let targetDatabase = grabPrimaryRequiredDbSchema({
|
||||
dbId,
|
||||
userId,
|
||||
});
|
||||
|
||||
if (!targetDatabase) {
|
||||
throw new Error("Couldn't Find Target Database!");
|
||||
@@ -48,7 +50,7 @@ export default async function addUsersTableToDb({
|
||||
targetDatabase.tables.push(userPreset);
|
||||
}
|
||||
|
||||
setUserSchemaData({ schemaData: userSchemaData, userId });
|
||||
writeUpdatedDbSchema({ dbSchema: targetDatabase, userId });
|
||||
|
||||
const targetDb: any[] | null = global.DSQL_USE_LOCAL
|
||||
? await LOCAL_DB_HANDLER(
|
||||
|
||||
@@ -5,56 +5,56 @@ import { CheckApiCredentialsFn } from "../../types";
|
||||
/**
|
||||
* # Grap API Credentials
|
||||
*/
|
||||
const grabApiCred: CheckApiCredentialsFn = ({
|
||||
key,
|
||||
database,
|
||||
table,
|
||||
user_id,
|
||||
media,
|
||||
}) => {
|
||||
if (!key) return null;
|
||||
if (!user_id) return null;
|
||||
// const grabApiCred: CheckApiCredentialsFn = ({
|
||||
// key,
|
||||
// database,
|
||||
// table,
|
||||
// user_id,
|
||||
// media,
|
||||
// }) => {
|
||||
// if (!key) return null;
|
||||
// if (!user_id) return null;
|
||||
|
||||
try {
|
||||
const allowedKeysPath = process.env.DSQL_API_KEYS_PATH;
|
||||
// try {
|
||||
// const allowedKeysPath = process.env.DSQL_API_KEYS_PATH;
|
||||
|
||||
if (!allowedKeysPath)
|
||||
throw new Error(
|
||||
"process.env.DSQL_API_KEYS_PATH variable not found"
|
||||
);
|
||||
// if (!allowedKeysPath)
|
||||
// throw new Error(
|
||||
// "process.env.DSQL_API_KEYS_PATH variable not found"
|
||||
// );
|
||||
|
||||
const ApiJSON = decrypt({ encryptedString: key });
|
||||
// const ApiJSON = decrypt({ encryptedString: key });
|
||||
|
||||
const ApiObject: import("../../types").ApiKeyObject = JSON.parse(
|
||||
ApiJSON || ""
|
||||
);
|
||||
// const ApiObject: import("../../types").ApiKeyObject = JSON.parse(
|
||||
// ApiJSON || ""
|
||||
// );
|
||||
|
||||
const isApiKeyValid = fs.existsSync(
|
||||
`${allowedKeysPath}/${ApiObject.sign}`
|
||||
);
|
||||
// const isApiKeyValid = fs.existsSync(
|
||||
// `${allowedKeysPath}/${ApiObject.sign}`
|
||||
// );
|
||||
|
||||
if (String(ApiObject.user_id) !== String(user_id)) return null;
|
||||
// if (String(ApiObject.user_id) !== String(user_id)) return null;
|
||||
|
||||
if (!isApiKeyValid) return null;
|
||||
if (!ApiObject.target_database) return ApiObject;
|
||||
if (media) return ApiObject;
|
||||
// if (!isApiKeyValid) return null;
|
||||
// if (!ApiObject.target_database) return ApiObject;
|
||||
// if (media) return ApiObject;
|
||||
|
||||
if (!database && ApiObject.target_database) return null;
|
||||
const isDatabaseAllowed = ApiObject.target_database
|
||||
?.split(",")
|
||||
.includes(String(database));
|
||||
// if (!database && ApiObject.target_database) return null;
|
||||
// const isDatabaseAllowed = ApiObject.target_database
|
||||
// ?.split(",")
|
||||
// .includes(String(database));
|
||||
|
||||
if (isDatabaseAllowed && !ApiObject.target_table) return ApiObject;
|
||||
if (isDatabaseAllowed && !table && ApiObject.target_table) return null;
|
||||
const isTableAllowed = ApiObject.target_table
|
||||
?.split(",")
|
||||
.includes(String(table));
|
||||
if (isTableAllowed) return ApiObject;
|
||||
return null;
|
||||
} catch (error: any) {
|
||||
console.log(`api-cred ERROR: ${error.message}`);
|
||||
return { error: `api-cred ERROR: ${error.message}` };
|
||||
}
|
||||
};
|
||||
// if (isDatabaseAllowed && !ApiObject.target_table) return ApiObject;
|
||||
// if (isDatabaseAllowed && !table && ApiObject.target_table) return null;
|
||||
// const isTableAllowed = ApiObject.target_table
|
||||
// ?.split(",")
|
||||
// .includes(String(table));
|
||||
// if (isTableAllowed) return ApiObject;
|
||||
// return null;
|
||||
// } catch (error: any) {
|
||||
// console.log(`api-cred ERROR: ${error.message}`);
|
||||
// return { error: `api-cred ERROR: ${error.message}` };
|
||||
// }
|
||||
// };
|
||||
|
||||
export default grabApiCred;
|
||||
// export default grabApiCred;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import getCsrfHeaderName from "../../../actions/get-csrf-header-name";
|
||||
import { AppNames } from "../../../dict/app-names";
|
||||
|
||||
type Param = {
|
||||
database?: string;
|
||||
@@ -22,8 +23,14 @@ export default function getAuthCookieNames(params?: Param): Return {
|
||||
process.env.DSQL_COOKIES_ONE_TIME_CODE_NAME || "one-time-code";
|
||||
|
||||
const targetDatabase =
|
||||
params?.database?.replace(/^datasquirel_user_\d+_/, "") ||
|
||||
process.env.DSQL_DB_NAME?.replace(/^datasquirel_user_\d+_/, "");
|
||||
params?.database?.replace(
|
||||
new RegExp(`^${AppNames["DsqlDbPrefix"]}\\d+_`),
|
||||
""
|
||||
) ||
|
||||
process.env.DSQL_DB_NAME?.replace(
|
||||
new RegExp(`^${AppNames["DsqlDbPrefix"]}\\d+_`),
|
||||
""
|
||||
);
|
||||
|
||||
let keyCookieName = cookiesPrefix;
|
||||
if (params?.userId) keyCookieName += `user_${params.userId}_`;
|
||||
|
||||
@@ -1,24 +1,35 @@
|
||||
import varDatabaseDbHandler from "../../functions/backend/varDatabaseDbHandler";
|
||||
import { default as grabUserSchemaData } from "../../functions/backend/grabUserSchemaData";
|
||||
import { default as setUserSchemaData } from "../../functions/backend/setUserSchemaData";
|
||||
import addDbEntry from "../../functions/backend/db/addDbEntry";
|
||||
import slugToCamelTitle from "../../shell/utils/slugToCamelTitle";
|
||||
import { DSQL_DATASQUIREL_USER_DATABASES } from "../../types/dsql";
|
||||
import {
|
||||
DSQL_DATASQUIREL_USER_DATABASE_TABLES,
|
||||
DSQL_DATASQUIREL_USER_DATABASES,
|
||||
} from "../../types/dsql";
|
||||
import {
|
||||
DSQL_FieldSchemaType,
|
||||
DSQL_IndexSchemaType,
|
||||
DSQL_MYSQL_SHOW_COLUMNS_Type,
|
||||
DSQL_TableSchemaType,
|
||||
} from "../../types";
|
||||
import grabDSQLSchemaIndexComment from "../../shell/utils/grab-dsql-schema-index-comment";
|
||||
import {
|
||||
grabPrimaryRequiredDbSchema,
|
||||
writeUpdatedDbSchema,
|
||||
} from "../../shell/createDbFromSchema/grab-required-database-schemas";
|
||||
import _n from "../../utils/numberfy";
|
||||
import dataTypeParser from "../../utils/db/schema/data-type-parser";
|
||||
import dataTypeConstructor from "../../utils/db/schema/data-type-constructor";
|
||||
|
||||
type Params = {
|
||||
userId: number | string;
|
||||
database: DSQL_DATASQUIREL_USER_DATABASES;
|
||||
dbId?: string | number;
|
||||
};
|
||||
|
||||
export default async function createDbSchemaFromDb({
|
||||
userId,
|
||||
database,
|
||||
dbId,
|
||||
}: Params) {
|
||||
try {
|
||||
if (!userId) {
|
||||
@@ -26,12 +37,12 @@ export default async function createDbSchemaFromDb({
|
||||
return;
|
||||
}
|
||||
|
||||
const userSchemaData = grabUserSchemaData({ userId });
|
||||
if (!userSchemaData) throw new Error("User schema data not found!");
|
||||
const targetDb = grabPrimaryRequiredDbSchema({
|
||||
userId,
|
||||
dbId: database.db_schema_id || dbId,
|
||||
});
|
||||
|
||||
const targetDb: { tables: object[] } = userSchemaData.filter(
|
||||
(dbObject) => dbObject.dbFullName === database.db_full_name
|
||||
)[0];
|
||||
if (!targetDb) throw new Error(`Target Db not found!`);
|
||||
|
||||
const existingTables = await varDatabaseDbHandler({
|
||||
database: database.db_full_name,
|
||||
@@ -44,21 +55,21 @@ export default async function createDbSchemaFromDb({
|
||||
const table = existingTables[i];
|
||||
const tableName = Object.values(table)[0] as string;
|
||||
|
||||
const tableInsert = await addDbEntry({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "user_database_tables",
|
||||
data: {
|
||||
user_id: userId,
|
||||
db_id: database.id,
|
||||
db_slug: database.db_slug,
|
||||
table_name: slugToCamelTitle(tableName),
|
||||
table_slug: tableName,
|
||||
},
|
||||
});
|
||||
const tableInsert =
|
||||
await addDbEntry<DSQL_DATASQUIREL_USER_DATABASE_TABLES>({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "user_database_tables",
|
||||
data: {
|
||||
user_id: _n(userId),
|
||||
db_id: database.id,
|
||||
db_slug: database.db_slug,
|
||||
table_name: slugToCamelTitle(tableName) || undefined,
|
||||
table_slug: tableName,
|
||||
},
|
||||
});
|
||||
|
||||
const tableObject: DSQL_TableSchemaType = {
|
||||
tableName: tableName,
|
||||
tableFullName: slugToCamelTitle(tableName) || "",
|
||||
fields: [],
|
||||
indexes: [],
|
||||
};
|
||||
@@ -75,9 +86,15 @@ export default async function createDbSchemaFromDb({
|
||||
const { Field, Type, Null, Key, Default, Extra } =
|
||||
tableColumn;
|
||||
|
||||
const parsedDataType = dataTypeParser(Type.toUpperCase());
|
||||
|
||||
const fieldObject: DSQL_FieldSchemaType = {
|
||||
fieldName: Field,
|
||||
dataType: Type.toUpperCase(),
|
||||
dataType: dataTypeConstructor(
|
||||
parsedDataType.type,
|
||||
parsedDataType.limit,
|
||||
parsedDataType.decimal
|
||||
),
|
||||
};
|
||||
|
||||
if (Null?.match(/^no$/i)) fieldObject.notNullValue = true;
|
||||
@@ -112,11 +129,16 @@ export default async function createDbSchemaFromDb({
|
||||
Index_comment,
|
||||
} = indexObject;
|
||||
|
||||
if (!Index_comment?.match(/^schema_index$/)) continue;
|
||||
if (
|
||||
!Index_comment?.match(
|
||||
new RegExp(grabDSQLSchemaIndexComment())
|
||||
)
|
||||
)
|
||||
continue;
|
||||
|
||||
const indexNewObject: DSQL_IndexSchemaType = {
|
||||
indexType: Index_type?.match(/fulltext/i)
|
||||
? "fullText"
|
||||
? "full_text"
|
||||
: "regular",
|
||||
indexName: Key_name,
|
||||
indexTableFields: [],
|
||||
@@ -152,8 +174,7 @@ export default async function createDbSchemaFromDb({
|
||||
targetDb.tables.push(tableObject);
|
||||
}
|
||||
|
||||
setUserSchemaData({ schemaData: userSchemaData, userId });
|
||||
|
||||
writeUpdatedDbSchema({ dbSchema: targetDb, userId });
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
|
||||
@@ -7,17 +7,30 @@ import connDbHandler from "../../../utils/db/conn-db-handler";
|
||||
import checkIfIsMaster from "../../../utils/check-if-is-master";
|
||||
import { DbContextsArray } from "./runQuery";
|
||||
import debugLog from "../../../utils/logging/debug-log";
|
||||
import { PostInsertReturn } from "../../../types";
|
||||
import {
|
||||
APIResponseObject,
|
||||
DSQL_TableSchemaType,
|
||||
PostInsertReturn,
|
||||
} from "../../../types";
|
||||
import purgeDefaultFields from "../../../utils/purge-default-fields";
|
||||
|
||||
type Param<T extends { [k: string]: any } = any> = {
|
||||
export type AddDbEntryParam<
|
||||
T extends { [k: string]: any } = any,
|
||||
K extends string = string
|
||||
> = {
|
||||
dbContext?: (typeof DbContextsArray)[number];
|
||||
paradigm?: "Read Only" | "Full Access";
|
||||
dbFullName?: string;
|
||||
tableName: string;
|
||||
data: T;
|
||||
tableSchema?: import("../../../types").DSQL_TableSchemaType;
|
||||
duplicateColumnName?: string;
|
||||
duplicateColumnValue?: string;
|
||||
tableName: K;
|
||||
data?: T;
|
||||
batchData?: T[];
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
duplicateColumnName?: keyof T;
|
||||
duplicateColumnValue?: string | number;
|
||||
/**
|
||||
* Update Entry if a duplicate is found.
|
||||
* Requires `duplicateColumnName` and `duplicateColumnValue` parameters
|
||||
*/
|
||||
update?: boolean;
|
||||
encryptionKey?: string;
|
||||
encryptionSalt?: string;
|
||||
@@ -28,12 +41,16 @@ type Param<T extends { [k: string]: any } = any> = {
|
||||
/**
|
||||
* Add a db Entry Function
|
||||
*/
|
||||
export default async function addDbEntry<T extends { [k: string]: any } = any>({
|
||||
export default async function addDbEntry<
|
||||
T extends { [k: string]: any } = any,
|
||||
K extends string = string
|
||||
>({
|
||||
dbContext,
|
||||
paradigm,
|
||||
dbFullName,
|
||||
tableName,
|
||||
data,
|
||||
batchData,
|
||||
tableSchema,
|
||||
duplicateColumnName,
|
||||
duplicateColumnValue,
|
||||
@@ -42,7 +59,7 @@ export default async function addDbEntry<T extends { [k: string]: any } = any>({
|
||||
encryptionSalt,
|
||||
forceLocal,
|
||||
debug,
|
||||
}: Param<T>): Promise<PostInsertReturn | null> {
|
||||
}: AddDbEntryParam<T, K>): Promise<APIResponseObject<PostInsertReturn>> {
|
||||
const isMaster = forceLocal
|
||||
? true
|
||||
: checkIfIsMaster({ dbContext, dbFullName });
|
||||
@@ -62,14 +79,21 @@ export default async function addDbEntry<T extends { [k: string]: any } = any>({
|
||||
? global.DSQL_DB_CONN
|
||||
: global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN;
|
||||
|
||||
if (data?.["date_created_timestamp"]) delete data["date_created_timestamp"];
|
||||
if (data?.["date_updated_timestamp"]) delete data["date_updated_timestamp"];
|
||||
if (data?.["date_updated"]) delete data["date_updated"];
|
||||
if (data?.["date_updated_code"]) delete data["date_updated_code"];
|
||||
if (data?.["date_created"]) delete data["date_created"];
|
||||
if (data?.["date_created_code"]) delete data["date_created_code"];
|
||||
let newData = _.cloneDeep(data);
|
||||
if (newData) {
|
||||
newData = purgeDefaultFields(newData);
|
||||
}
|
||||
|
||||
if (duplicateColumnName && typeof duplicateColumnName === "string") {
|
||||
let newBatchData = _.cloneDeep(batchData) as any[];
|
||||
if (newBatchData) {
|
||||
newBatchData = purgeDefaultFields(newBatchData);
|
||||
}
|
||||
|
||||
if (
|
||||
duplicateColumnName &&
|
||||
typeof duplicateColumnName === "string" &&
|
||||
newData
|
||||
) {
|
||||
const checkDuplicateQuery = `SELECT * FROM ${
|
||||
isMaster ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` WHERE \`${duplicateColumnName}\`=?`;
|
||||
@@ -81,13 +105,17 @@ export default async function addDbEntry<T extends { [k: string]: any } = any>({
|
||||
);
|
||||
|
||||
if (duplicateValue?.[0] && !update) {
|
||||
return null;
|
||||
} else if (duplicateValue && duplicateValue[0] && update) {
|
||||
return {
|
||||
success: false,
|
||||
payload: undefined,
|
||||
msg: "Duplicate entry found",
|
||||
};
|
||||
} else if (duplicateValue?.[0] && update) {
|
||||
return await updateDbEntry({
|
||||
dbContext,
|
||||
dbFullName,
|
||||
tableName,
|
||||
data,
|
||||
data: newData,
|
||||
tableSchema,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
@@ -97,140 +125,188 @@ export default async function addDbEntry<T extends { [k: string]: any } = any>({
|
||||
}
|
||||
}
|
||||
|
||||
const dataKeys = Object.keys(data);
|
||||
function generateQuery(data: T) {
|
||||
const dataKeys = Object.keys(data);
|
||||
|
||||
let insertKeysArray = [];
|
||||
let insertValuesArray = [];
|
||||
let insertKeysArray = [];
|
||||
let insertValuesArray = [];
|
||||
|
||||
for (let i = 0; i < dataKeys.length; i++) {
|
||||
try {
|
||||
const dataKey = dataKeys[i];
|
||||
let value = data?.[dataKey];
|
||||
for (let i = 0; i < dataKeys.length; i++) {
|
||||
try {
|
||||
const dataKey = dataKeys[i];
|
||||
let value = data[dataKey];
|
||||
|
||||
const targetFieldSchemaArray = tableSchema
|
||||
? tableSchema?.fields?.filter(
|
||||
(field) => field.fieldName == dataKey
|
||||
)
|
||||
: null;
|
||||
const targetFieldSchema =
|
||||
targetFieldSchemaArray && targetFieldSchemaArray[0]
|
||||
? targetFieldSchemaArray[0]
|
||||
const targetFieldSchemaArray = tableSchema
|
||||
? tableSchema?.fields?.filter(
|
||||
(field) => field.fieldName == dataKey
|
||||
)
|
||||
: null;
|
||||
const targetFieldSchema =
|
||||
targetFieldSchemaArray && targetFieldSchemaArray[0]
|
||||
? targetFieldSchemaArray[0]
|
||||
: null;
|
||||
|
||||
if (value == null || value == undefined) continue;
|
||||
if (value == null || value == undefined) continue;
|
||||
|
||||
if (
|
||||
targetFieldSchema?.dataType?.match(/int$/i) &&
|
||||
typeof value == "string" &&
|
||||
!value?.match(/./)
|
||||
)
|
||||
continue;
|
||||
if (
|
||||
targetFieldSchema?.dataType?.match(/int$/i) &&
|
||||
typeof value == "string" &&
|
||||
!value?.match(/./)
|
||||
)
|
||||
continue;
|
||||
|
||||
if (targetFieldSchema?.encrypted) {
|
||||
value = encrypt({
|
||||
data: value,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
});
|
||||
console.log("DSQL: Encrypted value =>", value);
|
||||
}
|
||||
|
||||
const htmlRegex = /<[^>]+>/g;
|
||||
|
||||
if (targetFieldSchema?.richText || String(value).match(htmlRegex)) {
|
||||
value = sanitizeHtml(value, sanitizeHtmlOptions);
|
||||
}
|
||||
|
||||
if (targetFieldSchema?.pattern) {
|
||||
const pattern = new RegExp(
|
||||
targetFieldSchema.pattern,
|
||||
targetFieldSchema.patternFlags || ""
|
||||
);
|
||||
if (!pattern.test(value)) {
|
||||
console.log("DSQL: Pattern not matched =>", value);
|
||||
value = "";
|
||||
if (targetFieldSchema?.encrypted) {
|
||||
value = encrypt({
|
||||
data: value,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
});
|
||||
console.log("DSQL: Encrypted value =>", value);
|
||||
}
|
||||
}
|
||||
|
||||
insertKeysArray.push("`" + dataKey + "`");
|
||||
const htmlRegex = /<[^>]+>/g;
|
||||
|
||||
if (typeof value === "object") {
|
||||
value = JSON.stringify(value);
|
||||
}
|
||||
if (
|
||||
targetFieldSchema?.richText ||
|
||||
String(value).match(htmlRegex)
|
||||
) {
|
||||
value = sanitizeHtml(value, sanitizeHtmlOptions);
|
||||
}
|
||||
|
||||
if (typeof value == "number") {
|
||||
insertValuesArray.push(String(value));
|
||||
} else {
|
||||
insertValuesArray.push(value);
|
||||
if (targetFieldSchema?.pattern) {
|
||||
const pattern = new RegExp(
|
||||
targetFieldSchema.pattern,
|
||||
targetFieldSchema.patternFlags || ""
|
||||
);
|
||||
if (!pattern.test(value)) {
|
||||
console.log("DSQL: Pattern not matched =>", value);
|
||||
value = "";
|
||||
}
|
||||
}
|
||||
|
||||
insertKeysArray.push("`" + dataKey + "`");
|
||||
|
||||
if (typeof value === "object") {
|
||||
value = JSON.stringify(value);
|
||||
}
|
||||
|
||||
if (typeof value == "number") {
|
||||
insertValuesArray.push(String(value));
|
||||
} else {
|
||||
insertValuesArray.push(value);
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.log(
|
||||
"DSQL: Error in parsing data keys =>",
|
||||
error.message
|
||||
);
|
||||
global.ERROR_CALLBACK?.(
|
||||
`Error parsing Data Keys`,
|
||||
error as Error
|
||||
);
|
||||
continue;
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.log("DSQL: Error in parsing data keys =>", error.message);
|
||||
global.ERROR_CALLBACK?.(`Error parsing Data Keys`, error as Error);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (!data?.["date_created"]) {
|
||||
insertKeysArray.push("`date_created`");
|
||||
insertValuesArray.push(Date());
|
||||
}
|
||||
|
||||
if (!data?.["date_created_code"]) {
|
||||
insertKeysArray.push("`date_created_code`");
|
||||
insertValuesArray.push(Date.now());
|
||||
}
|
||||
|
||||
if (!data?.["date_updated"]) {
|
||||
insertKeysArray.push("`date_updated`");
|
||||
insertValuesArray.push(Date());
|
||||
}
|
||||
|
||||
if (!data?.["date_updated_code"]) {
|
||||
insertKeysArray.push("`date_updated_code`");
|
||||
insertValuesArray.push(Date.now());
|
||||
|
||||
const queryValuesArray = insertValuesArray;
|
||||
|
||||
return { queryValuesArray, insertValuesArray, insertKeysArray };
|
||||
}
|
||||
|
||||
const query = `INSERT INTO ${
|
||||
isMaster ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` (${insertKeysArray.join(",")}) VALUES (${insertValuesArray
|
||||
.map(() => "?")
|
||||
.join(",")})`;
|
||||
const queryValuesArray = insertValuesArray;
|
||||
if (newData) {
|
||||
const { insertKeysArray, insertValuesArray, queryValuesArray } =
|
||||
generateQuery(newData);
|
||||
|
||||
if (debug) {
|
||||
debugLog({
|
||||
log: DB_CONN?.getConfig(),
|
||||
addTime: true,
|
||||
label: "DB_CONN Config",
|
||||
});
|
||||
const query = `INSERT INTO ${
|
||||
isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` (${insertKeysArray.join(
|
||||
","
|
||||
)}) VALUES (${insertValuesArray.map(() => "?").join(",")})`;
|
||||
|
||||
debugLog({
|
||||
log: query,
|
||||
addTime: true,
|
||||
label: "query",
|
||||
});
|
||||
const newInsert = await connDbHandler(
|
||||
DB_CONN,
|
||||
query,
|
||||
queryValuesArray,
|
||||
debug
|
||||
);
|
||||
|
||||
debugLog({
|
||||
log: queryValuesArray,
|
||||
addTime: true,
|
||||
label: "queryValuesArray",
|
||||
});
|
||||
return {
|
||||
success: Boolean(newInsert?.insertId),
|
||||
payload: newInsert,
|
||||
queryObject: {
|
||||
sql: query,
|
||||
params: queryValuesArray,
|
||||
},
|
||||
};
|
||||
} else if (newBatchData) {
|
||||
let batchInsertKeysArray: string[] | undefined;
|
||||
let batchInsertValuesArray: any[][] = [];
|
||||
let batchQueryValuesArray: any[][] = [];
|
||||
|
||||
for (let i = 0; i < newBatchData.length; i++) {
|
||||
const singleBatchData = newBatchData[i];
|
||||
const { insertKeysArray, insertValuesArray, queryValuesArray } =
|
||||
generateQuery(singleBatchData);
|
||||
|
||||
if (!batchInsertKeysArray) {
|
||||
batchInsertKeysArray = insertKeysArray;
|
||||
}
|
||||
|
||||
batchInsertValuesArray.push(insertValuesArray);
|
||||
batchQueryValuesArray.push(queryValuesArray);
|
||||
}
|
||||
|
||||
const query = `INSERT INTO ${
|
||||
isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` (${batchInsertKeysArray?.join(
|
||||
","
|
||||
)}) VALUES ${batchInsertValuesArray
|
||||
.map((vl) => `(${vl.map(() => "?").join(",")})`)
|
||||
.join(",")}`;
|
||||
|
||||
console.log("query", query);
|
||||
console.log("batchQueryValuesArray", batchQueryValuesArray);
|
||||
|
||||
const newInsert = await connDbHandler(
|
||||
DB_CONN,
|
||||
query,
|
||||
batchQueryValuesArray.flat(),
|
||||
debug
|
||||
);
|
||||
|
||||
if (debug) {
|
||||
debugLog({
|
||||
log: newInsert,
|
||||
addTime: true,
|
||||
label: "newInsert",
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
success: Boolean(newInsert?.insertId),
|
||||
payload: newInsert,
|
||||
queryObject: {
|
||||
sql: query,
|
||||
params: batchQueryValuesArray.flat(),
|
||||
},
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
success: false,
|
||||
payload: undefined,
|
||||
msg: "No data provided",
|
||||
};
|
||||
}
|
||||
|
||||
const newInsert = await connDbHandler(
|
||||
DB_CONN,
|
||||
query,
|
||||
queryValuesArray,
|
||||
debug
|
||||
);
|
||||
|
||||
if (debug) {
|
||||
debugLog({
|
||||
log: newInsert,
|
||||
addTime: true,
|
||||
label: "newInsert",
|
||||
});
|
||||
}
|
||||
|
||||
return newInsert;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import { DSQL_TableSchemaType, PostInsertReturn } from "../../../types";
|
||||
import checkIfIsMaster from "../../../utils/check-if-is-master";
|
||||
import connDbHandler from "../../../utils/db/conn-db-handler";
|
||||
import { DbContextsArray } from "./runQuery";
|
||||
|
||||
type Param = {
|
||||
type Param<T extends { [k: string]: any } = any, K extends string = string> = {
|
||||
dbContext?: (typeof DbContextsArray)[number];
|
||||
dbFullName: string;
|
||||
tableName: string;
|
||||
tableSchema?: import("../../../types").DSQL_TableSchemaType;
|
||||
identifierColumnName: string;
|
||||
dbFullName?: string;
|
||||
tableName: K;
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
identifierColumnName: keyof T;
|
||||
identifierValue: string | number;
|
||||
forceLocal?: boolean;
|
||||
};
|
||||
@@ -16,14 +17,17 @@ type Param = {
|
||||
* # Delete DB Entry Function
|
||||
* @description
|
||||
*/
|
||||
export default async function deleteDbEntry({
|
||||
export default async function deleteDbEntry<
|
||||
T extends { [k: string]: any } = any,
|
||||
K extends string = string
|
||||
>({
|
||||
dbContext,
|
||||
dbFullName,
|
||||
tableName,
|
||||
identifierColumnName,
|
||||
identifierValue,
|
||||
forceLocal,
|
||||
}: Param): Promise<object | null> {
|
||||
}: Param<T, K>): Promise<PostInsertReturn | null> {
|
||||
try {
|
||||
const isMaster = forceLocal
|
||||
? true
|
||||
@@ -32,9 +36,6 @@ export default async function deleteDbEntry({
|
||||
const DB_CONN = isMaster
|
||||
? global.DSQL_DB_CONN
|
||||
: global.DSQL_FULL_ACCESS_DB_CONN || global.DSQL_DB_CONN;
|
||||
const DB_RO_CONN = isMaster
|
||||
? global.DSQL_DB_CONN
|
||||
: global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN;
|
||||
|
||||
/**
|
||||
* Execution
|
||||
@@ -42,8 +43,8 @@ export default async function deleteDbEntry({
|
||||
* @description
|
||||
*/
|
||||
const query = `DELETE FROM ${
|
||||
isMaster ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` WHERE \`${identifierColumnName}\`=?`;
|
||||
isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` WHERE \`${identifierColumnName.toString()}\`=?`;
|
||||
|
||||
const deletedEntry = await connDbHandler(DB_CONN, query, [
|
||||
identifierValue,
|
||||
|
||||
@@ -126,7 +126,7 @@ export default async function runQuery({
|
||||
case "insert":
|
||||
result = await addDbEntry({
|
||||
dbContext,
|
||||
dbFullName: dbFullName,
|
||||
dbFullName,
|
||||
tableName: table,
|
||||
data: data,
|
||||
update,
|
||||
@@ -145,7 +145,7 @@ export default async function runQuery({
|
||||
case "update":
|
||||
result = await updateDbEntry({
|
||||
dbContext,
|
||||
dbFullName: dbFullName,
|
||||
dbFullName,
|
||||
tableName: table,
|
||||
data: data,
|
||||
identifierColumnName,
|
||||
@@ -158,7 +158,7 @@ export default async function runQuery({
|
||||
case "delete":
|
||||
result = await deleteDbEntry({
|
||||
dbContext,
|
||||
dbFullName: dbFullName,
|
||||
dbFullName,
|
||||
tableName: table,
|
||||
identifierColumnName,
|
||||
identifierValue,
|
||||
|
||||
@@ -4,7 +4,13 @@ import encrypt from "../../dsql/encrypt";
|
||||
import checkIfIsMaster from "../../../utils/check-if-is-master";
|
||||
import connDbHandler from "../../../utils/db/conn-db-handler";
|
||||
import { DbContextsArray } from "./runQuery";
|
||||
import { PostInsertReturn } from "../../../types";
|
||||
import {
|
||||
APIResponseObject,
|
||||
DSQL_TableSchemaType,
|
||||
PostInsertReturn,
|
||||
} from "../../../types";
|
||||
import _ from "lodash";
|
||||
import purgeDefaultFields from "../../../utils/purge-default-fields";
|
||||
|
||||
type Param<T extends { [k: string]: any } = any> = {
|
||||
dbContext?: (typeof DbContextsArray)[number];
|
||||
@@ -12,8 +18,8 @@ type Param<T extends { [k: string]: any } = any> = {
|
||||
tableName: string;
|
||||
encryptionKey?: string;
|
||||
encryptionSalt?: string;
|
||||
data: any;
|
||||
tableSchema?: import("../../../types").DSQL_TableSchemaType;
|
||||
data?: T;
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
identifierColumnName: keyof T;
|
||||
identifierValue: string | number;
|
||||
forceLocal?: boolean;
|
||||
@@ -36,11 +42,17 @@ export default async function updateDbEntry<
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
forceLocal,
|
||||
}: Param<T>): Promise<PostInsertReturn | null> {
|
||||
}: Param<T>): Promise<APIResponseObject<PostInsertReturn>> {
|
||||
/**
|
||||
* Check if data is valid
|
||||
*/
|
||||
if (!data || !Object.keys(data).length) return null;
|
||||
if (!data || !Object.keys(data).length) {
|
||||
return {
|
||||
success: false,
|
||||
payload: undefined,
|
||||
msg: "No data provided",
|
||||
};
|
||||
}
|
||||
|
||||
const isMaster = forceLocal
|
||||
? true
|
||||
@@ -54,12 +66,15 @@ export default async function updateDbEntry<
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
let newData = _.cloneDeep(data);
|
||||
newData = purgeDefaultFields(newData);
|
||||
|
||||
/**
|
||||
* Declare variables
|
||||
*
|
||||
* @description Declare "results" variable
|
||||
*/
|
||||
const dataKeys = Object.keys(data);
|
||||
const dataKeys = Object.keys(newData);
|
||||
|
||||
let updateKeyValueArray = [];
|
||||
let updateValues = [];
|
||||
@@ -67,8 +82,7 @@ export default async function updateDbEntry<
|
||||
for (let i = 0; i < dataKeys.length; i++) {
|
||||
try {
|
||||
const dataKey = dataKeys[i];
|
||||
// @ts-ignore
|
||||
let value = data[dataKey];
|
||||
let value = newData[dataKey];
|
||||
|
||||
const targetFieldSchemaArray = tableSchema
|
||||
? tableSchema?.fields?.filter(
|
||||
@@ -159,7 +173,7 @@ export default async function updateDbEntry<
|
||||
////////////////////////////////////////
|
||||
|
||||
const query = `UPDATE ${
|
||||
isMaster ? "" : `\`${dbFullName}\`.`
|
||||
isMaster && !dbFullName ? "" : `\`${dbFullName}\`.`
|
||||
}\`${tableName}\` SET ${updateKeyValueArray.join(",")} WHERE \`${
|
||||
identifierColumnName as string
|
||||
}\`=?`;
|
||||
@@ -171,5 +185,12 @@ export default async function updateDbEntry<
|
||||
/**
|
||||
* Return statement
|
||||
*/
|
||||
return updatedEntry;
|
||||
return {
|
||||
success: Boolean(updatedEntry?.affectedRows),
|
||||
payload: updatedEntry,
|
||||
queryObject: {
|
||||
sql: query,
|
||||
params: updateValues,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,69 +1,63 @@
|
||||
import fs from "fs";
|
||||
import serverError from "./serverError";
|
||||
import grabDSQLConnection from "../../utils/grab-dsql-connection";
|
||||
import path from "path";
|
||||
import grabDSQLConnection from "../../utils/grab-dsql-connection";
|
||||
|
||||
type Param = {
|
||||
query: string;
|
||||
values?: string[] | object;
|
||||
noErrorLogs?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
*/
|
||||
export default async function dbHandler(...args: any[]) {
|
||||
process.env.NODE_ENV?.match(/dev/) &&
|
||||
fs.appendFileSync(
|
||||
"./.tmp/sqlQuery.sql",
|
||||
args[0] + "\n" + Date() + "\n\n\n",
|
||||
"utf8"
|
||||
);
|
||||
|
||||
export default async function dbHandler({
|
||||
query,
|
||||
values,
|
||||
noErrorLogs,
|
||||
}: Param): Promise<any[] | object | null> {
|
||||
const CONNECTION = grabDSQLConnection();
|
||||
|
||||
let results;
|
||||
|
||||
/**
|
||||
* Fetch from db
|
||||
*
|
||||
* @description Fetch data from db if no cache
|
||||
*/
|
||||
try {
|
||||
results = await new Promise((resolve, reject) => {
|
||||
CONNECTION.query(
|
||||
...args,
|
||||
(error: any, result: any, fields: any) => {
|
||||
if (error) {
|
||||
resolve({ error: error.message });
|
||||
} else {
|
||||
resolve(result);
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
if (query && values) {
|
||||
results = await CONNECTION.query(query, values);
|
||||
} else {
|
||||
results = await CONNECTION.query(query);
|
||||
}
|
||||
} catch (error: any) {
|
||||
const tmpFolder = path.resolve(process.cwd(), "./.tmp");
|
||||
if (!fs.existsSync(tmpFolder))
|
||||
fs.mkdirSync(tmpFolder, { recursive: true });
|
||||
if (!noErrorLogs) {
|
||||
global.ERROR_CALLBACK?.(`DB Handler Error...`, error as Error);
|
||||
}
|
||||
|
||||
fs.appendFileSync(
|
||||
path.resolve(tmpFolder, "./dbErrorLogs.txt"),
|
||||
JSON.stringify(error, null, 4) + "\n" + Date() + "\n\n\n",
|
||||
"utf8"
|
||||
);
|
||||
if (process.env.FIRST_RUN) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!noErrorLogs) {
|
||||
console.log("ERROR in dbHandler =>", error.message);
|
||||
console.log(error);
|
||||
console.log(CONNECTION.config());
|
||||
|
||||
const tmpFolder = path.resolve(process.cwd(), "./.tmp");
|
||||
|
||||
if (!fs.existsSync(tmpFolder))
|
||||
fs.mkdirSync(tmpFolder, { recursive: true });
|
||||
|
||||
fs.appendFileSync(
|
||||
path.resolve(tmpFolder, "./dbErrorLogs.txt"),
|
||||
JSON.stringify(error, null, 4) + "\n" + Date() + "\n\n\n",
|
||||
"utf8"
|
||||
);
|
||||
}
|
||||
|
||||
results = null;
|
||||
|
||||
global.ERROR_CALLBACK?.(`DB Handler Error`, error as Error);
|
||||
|
||||
serverError({
|
||||
component: "dbHandler",
|
||||
message: error.message,
|
||||
});
|
||||
} finally {
|
||||
await CONNECTION?.end();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return results
|
||||
*
|
||||
* @description Return results add to cache if "req" param is passed
|
||||
*/
|
||||
if (results) {
|
||||
return JSON.parse(JSON.stringify(results));
|
||||
} else {
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
/**
|
||||
* Regular expression to match default fields
|
||||
*
|
||||
* @description Regular expression to match default fields
|
||||
*/
|
||||
const defaultFieldsRegexp =
|
||||
/^id$|^uuid$|^date_created$|^date_created_code$|^date_created_timestamp$|^date_updated$|^date_updated_code$|^date_updated_timestamp$/;
|
||||
|
||||
export default defaultFieldsRegexp;
|
||||
@@ -0,0 +1,70 @@
|
||||
import { UserType } from "../../types";
|
||||
import dbHandler from "./dbHandler";
|
||||
import dsqlCrud from "../../utils/data-fetching/crud";
|
||||
import { DSQL_DATASQUIREL_USERS, DsqlTables } from "../../types/dsql";
|
||||
import decrypt from "../dsql/decrypt";
|
||||
import createUserSQLUser from "../../utils/create-user-sql-user";
|
||||
import grabUserMainSqlUserName from "../../utils/grab-user-main-sql-user-name";
|
||||
|
||||
type Params = {
|
||||
user: UserType;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
fullName?: string;
|
||||
host?: string;
|
||||
username?: string;
|
||||
password?: string;
|
||||
};
|
||||
|
||||
export default async function grabMariadbMainUserForUser({
|
||||
user,
|
||||
}: Params): Promise<Return> {
|
||||
const {
|
||||
fullName,
|
||||
host,
|
||||
username: mariaDBUsername,
|
||||
webHost,
|
||||
} = grabUserMainSqlUserName({ user });
|
||||
|
||||
const existingWebAppUser = (await dbHandler({
|
||||
query: `SELECT * FROM mysql.user WHERE user=? AND host=?`,
|
||||
values: [mariaDBUsername, webHost],
|
||||
})) as any[];
|
||||
|
||||
if (!existingWebAppUser?.[0]) {
|
||||
return await createUserSQLUser(user);
|
||||
} else {
|
||||
const existingUserRecord = await dsqlCrud<
|
||||
DSQL_DATASQUIREL_USERS,
|
||||
(typeof DsqlTables)[number]
|
||||
>({
|
||||
action: "get",
|
||||
table: "users",
|
||||
query: {
|
||||
query: {
|
||||
id: {
|
||||
value: String(user.id),
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const targetUser = (
|
||||
existingUserRecord?.payload as DSQL_DATASQUIREL_USERS[] | undefined
|
||||
)?.[0];
|
||||
|
||||
if (!targetUser?.id) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return {
|
||||
fullName,
|
||||
host,
|
||||
username: mariaDBUsername,
|
||||
password: decrypt({
|
||||
encryptedString: targetUser.mariadb_pass || "",
|
||||
}),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
import { DSQL_DatabaseSchemaType, UserType } from "../../types";
|
||||
import serverError from "./serverError";
|
||||
import fs from "fs";
|
||||
import grabDirNames from "../../utils/backend/names/grab-dir-names";
|
||||
import EJSON from "../../utils/ejson";
|
||||
|
||||
type Params = {
|
||||
userId?: string | number;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Grab User Schema Data
|
||||
*/
|
||||
export default function grabUserSchemaData({
|
||||
userId,
|
||||
}: Params): DSQL_DatabaseSchemaType[] | null {
|
||||
try {
|
||||
const { userSchemaMainJSONFilePath } = grabDirNames({ userId });
|
||||
const schemaJSON = fs.readFileSync(
|
||||
userSchemaMainJSONFilePath || "",
|
||||
"utf-8"
|
||||
);
|
||||
const schemaObj = EJSON.parse(schemaJSON) as DSQL_DatabaseSchemaType[];
|
||||
return schemaObj;
|
||||
} catch (error: any) {
|
||||
serverError({
|
||||
component: "grabUserSchemaData",
|
||||
message: error.message,
|
||||
});
|
||||
|
||||
global.ERROR_CALLBACK?.(
|
||||
`Error Grabbing User Schema Data`,
|
||||
error as Error
|
||||
);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
// @ts-check
|
||||
|
||||
import decrypt from "../dsql/decrypt";
|
||||
import defaultFieldsRegexp from "./defaultFieldsRegexp";
|
||||
import defaultFieldsRegexp from "../dsql/default-fields-regexp";
|
||||
|
||||
type Param = {
|
||||
unparsedResults: any[];
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
import serverError from "./serverError";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import { DSQL_DatabaseSchemaType } from "../../types";
|
||||
import grabDirNames from "../../utils/backend/names/grab-dir-names";
|
||||
|
||||
type Param = {
|
||||
userId: string | number;
|
||||
schemaData: DSQL_DatabaseSchemaType[];
|
||||
};
|
||||
|
||||
/**
|
||||
* # Set User Schema Data
|
||||
*/
|
||||
export default function setUserSchemaData({
|
||||
userId,
|
||||
schemaData,
|
||||
}: Param): boolean {
|
||||
try {
|
||||
const { userSchemaMainJSONFilePath } = grabDirNames({ userId });
|
||||
|
||||
if (!userSchemaMainJSONFilePath) {
|
||||
throw new Error(`No User Schema JSON found!`);
|
||||
}
|
||||
|
||||
fs.writeFileSync(
|
||||
userSchemaMainJSONFilePath,
|
||||
JSON.stringify(schemaData),
|
||||
"utf8"
|
||||
);
|
||||
|
||||
return true;
|
||||
} catch (error: any) {
|
||||
serverError({
|
||||
component: "/functions/backend/setUserSchemaData",
|
||||
message: error.message,
|
||||
});
|
||||
|
||||
global.ERROR_CALLBACK?.(`Error Setting User Schema`, error as Error);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import mysql from "serverless-mysql";
|
||||
import { UserType } from "../../types";
|
||||
import connDbHandler from "../../utils/db/conn-db-handler";
|
||||
|
||||
type Params = {
|
||||
query?: string;
|
||||
values?: any[];
|
||||
database?: string;
|
||||
user: UserType;
|
||||
};
|
||||
|
||||
export default async function suDbHandler({
|
||||
query,
|
||||
database,
|
||||
user,
|
||||
values,
|
||||
}: Params) {
|
||||
const connection = mysql({
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: database,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
|
||||
const results = await connDbHandler(connection, query);
|
||||
|
||||
return results;
|
||||
}
|
||||
@@ -1,14 +1,17 @@
|
||||
import serverError from "./serverError";
|
||||
import grabUserSchemaData from "./grabUserSchemaData";
|
||||
import setUserSchemaData from "./setUserSchemaData";
|
||||
import createDbFromSchema from "../../shell/createDbFromSchema";
|
||||
import grabSchemaFieldsFromData from "./grabSchemaFieldsFromData";
|
||||
import {
|
||||
grabPrimaryRequiredDbSchema,
|
||||
writeUpdatedDbSchema,
|
||||
} from "../../shell/createDbFromSchema/grab-required-database-schemas";
|
||||
|
||||
type Param = {
|
||||
userId: number | string;
|
||||
database: string;
|
||||
newFields?: string[];
|
||||
newPayload?: { [s: string]: any };
|
||||
dbId: string | number;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -19,27 +22,25 @@ export default async function updateUsersTableSchema({
|
||||
database,
|
||||
newFields,
|
||||
newPayload,
|
||||
dbId,
|
||||
}: Param): Promise<any> {
|
||||
try {
|
||||
const dbFullName = database;
|
||||
|
||||
const userSchemaData = grabUserSchemaData({ userId });
|
||||
if (!userSchemaData) throw new Error("User schema data not found!");
|
||||
let targetDatabase = grabPrimaryRequiredDbSchema({
|
||||
dbId,
|
||||
userId,
|
||||
});
|
||||
|
||||
let targetDatabaseIndex = userSchemaData.findIndex(
|
||||
(db) => db.dbFullName === database
|
||||
);
|
||||
|
||||
if (targetDatabaseIndex < 0) {
|
||||
if (!targetDatabase) {
|
||||
throw new Error("Couldn't Find Target Database!");
|
||||
}
|
||||
|
||||
let existingTableIndex = userSchemaData[
|
||||
targetDatabaseIndex
|
||||
]?.tables.findIndex((table) => table.tableName === "users");
|
||||
let existingTableIndex = targetDatabase?.tables.findIndex(
|
||||
(table) => table.tableName === "users"
|
||||
);
|
||||
|
||||
const usersTable =
|
||||
userSchemaData[targetDatabaseIndex].tables[existingTableIndex];
|
||||
const usersTable = targetDatabase.tables[existingTableIndex];
|
||||
|
||||
if (!usersTable?.fields?.[0]) throw new Error("Users Table Not Found!");
|
||||
|
||||
@@ -56,7 +57,7 @@ export default async function updateUsersTableSchema({
|
||||
|
||||
usersTable.fields.splice(finalSpliceStartIndex, 0, ...additionalFields);
|
||||
|
||||
setUserSchemaData({ schemaData: userSchemaData, userId });
|
||||
writeUpdatedDbSchema({ dbSchema: targetDatabase, userId });
|
||||
|
||||
const dbShellUpdate = await createDbFromSchema({
|
||||
userId,
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import mysql from "serverless-mysql";
|
||||
import { DSQL_TableSchemaType, UserType } from "../../types";
|
||||
import grabMariadbMainUserForUser from "./grab-mariadb-main-user-for-user";
|
||||
import connDbHandler from "../../utils/db/conn-db-handler";
|
||||
|
||||
type Params = {
|
||||
query?: string;
|
||||
values?: any[];
|
||||
database?: string;
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
debug?: boolean;
|
||||
user: UserType;
|
||||
};
|
||||
|
||||
export default async function userDbHandler({
|
||||
query,
|
||||
user,
|
||||
database,
|
||||
debug,
|
||||
tableSchema,
|
||||
values,
|
||||
}: Params) {
|
||||
const { fullName, host, username, password } =
|
||||
await grabMariadbMainUserForUser({ user });
|
||||
|
||||
const connection = mysql({
|
||||
config: {
|
||||
host,
|
||||
user: username,
|
||||
password: password,
|
||||
database: database,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
|
||||
const results = await connDbHandler(connection, query);
|
||||
|
||||
return results;
|
||||
}
|
||||
@@ -2,12 +2,13 @@ import parseDbResults from "./parseDbResults";
|
||||
import serverError from "./serverError";
|
||||
import grabDSQLConnection from "../../utils/grab-dsql-connection";
|
||||
import connDbHandler from "../../utils/db/conn-db-handler";
|
||||
import { DSQL_TableSchemaType } from "../../types";
|
||||
|
||||
type Param = {
|
||||
queryString: string;
|
||||
queryValuesArray?: any[];
|
||||
database?: string;
|
||||
tableSchema?: import("../../types").DSQL_TableSchemaType;
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
debug?: boolean;
|
||||
};
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
import _ from "lodash";
|
||||
import EJSON from "../../utils/ejson";
|
||||
import generateTypeDefinition from "./generate-type-definitions";
|
||||
import path from "path";
|
||||
import { AppNames } from "../../dict/app-names";
|
||||
|
||||
type Params = {
|
||||
dbSchema?: DSQL_DatabaseSchemaType;
|
||||
@@ -16,16 +16,12 @@ type Params = {
|
||||
|
||||
export default function dbSchemaToType(params?: Params): string[] | undefined {
|
||||
let datasquirelSchema;
|
||||
const defaultTableFieldsJSONFilePath = path.resolve(
|
||||
__dirname,
|
||||
"../../data/defaultFields.json"
|
||||
);
|
||||
const { mainShemaJSONFilePath, defaultTableFieldsJSONFilePath } =
|
||||
grabDirNames();
|
||||
|
||||
if (params?.dbSchema) {
|
||||
datasquirelSchema = params.dbSchema;
|
||||
} else {
|
||||
const { mainShemaJSONFilePath } = grabDirNames();
|
||||
|
||||
const mainSchema = EJSON.parse(
|
||||
fs.readFileSync(mainShemaJSONFilePath, "utf-8")
|
||||
) as DSQL_DatabaseSchemaType[];
|
||||
@@ -49,7 +45,7 @@ export default function dbSchemaToType(params?: Params): string[] | undefined {
|
||||
let newDefaultFields = _.cloneDeep(defaultFields);
|
||||
return {
|
||||
...tblSchm,
|
||||
fields: params?.dbSchema
|
||||
fields: tblSchm.fields.find((fld) => fld.fieldName == "id")
|
||||
? tblSchm.fields
|
||||
: [
|
||||
newDefaultFields.shift(),
|
||||
@@ -62,7 +58,10 @@ export default function dbSchemaToType(params?: Params): string[] | undefined {
|
||||
|
||||
const defDbName = (
|
||||
datasquirelSchema.dbName ||
|
||||
datasquirelSchema.dbFullName?.replace(/datasquirel_user_\d+_/, "")
|
||||
datasquirelSchema.dbFullName?.replace(
|
||||
new RegExp(`${AppNames["DsqlDbPrefix"]}\\d+_`),
|
||||
""
|
||||
)
|
||||
)
|
||||
?.toUpperCase()
|
||||
.replace(/ /g, "_");
|
||||
|
||||
@@ -52,6 +52,7 @@ export default function decrypt({
|
||||
return decrypted;
|
||||
} catch (error: any) {
|
||||
console.log("Error in decrypting =>", error.message);
|
||||
console.log("encryptedString =>", encryptedString);
|
||||
global.ERROR_CALLBACK?.(`Error Decrypting data`, error as Error);
|
||||
return encryptedString;
|
||||
}
|
||||
|
||||
@@ -1,16 +1,9 @@
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* Check for user in local storage
|
||||
* Regular expression to match default fields
|
||||
*
|
||||
* @description Preventdefault, declare variables
|
||||
* @description Regular expression to match default fields
|
||||
*/
|
||||
|
||||
const defaultFieldsRegexp =
|
||||
/^id$|^uuid$|^date_created$|^date_created_code$|^date_created_timestamp$|^date_updated$|^date_updated_code$|^date_updated_timestamp$/;
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
/^id$|^uuid$|^uid$|^date_created$|^date_created_code$|^date_created_timestamp$|^date_updated$|^date_updated_code$|^date_updated_timestamp$/;
|
||||
|
||||
export default defaultFieldsRegexp;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { DSQL_TableSchemaType } from "../../types";
|
||||
import { DSQL_FieldSchemaType, DSQL_TableSchemaType } from "../../types";
|
||||
import defaultFieldsRegexp from "./default-fields-regexp";
|
||||
|
||||
type Param = {
|
||||
@@ -8,6 +8,7 @@ type Param = {
|
||||
typeDefName?: string;
|
||||
allValuesOptional?: boolean;
|
||||
addExport?: boolean;
|
||||
dbName?: string;
|
||||
};
|
||||
|
||||
export default function generateTypeDefinition({
|
||||
@@ -17,21 +18,34 @@ export default function generateTypeDefinition({
|
||||
typeDefName,
|
||||
allValuesOptional,
|
||||
addExport,
|
||||
dbName,
|
||||
}: Param): string | null {
|
||||
let typeDefinition: string | null = ``;
|
||||
|
||||
try {
|
||||
const tdName =
|
||||
typeDefName ||
|
||||
`DSQL_${query.single}_${query.single_table}`.toUpperCase();
|
||||
const tdName = typeDefName
|
||||
? typeDefName
|
||||
: dbName
|
||||
? `DSQL_${dbName}_${table.tableName}`.toUpperCase()
|
||||
: `DSQL_${query.single}_${query.single_table}`.toUpperCase();
|
||||
|
||||
const fields = table.fields;
|
||||
|
||||
function typeMap(type: string) {
|
||||
if (type?.match(/int/i)) {
|
||||
function typeMap(schemaType: DSQL_FieldSchemaType) {
|
||||
if (schemaType.options && schemaType.options.length > 0) {
|
||||
return schemaType.options
|
||||
.map((opt) =>
|
||||
schemaType.dataType?.match(/int/i) ||
|
||||
typeof opt == "number"
|
||||
? `${opt}`
|
||||
: `"${opt}"`
|
||||
)
|
||||
.join(" | ");
|
||||
}
|
||||
if (schemaType.dataType?.match(/int/i)) {
|
||||
return "number";
|
||||
}
|
||||
if (type?.match(/text|varchar|timestamp/i)) {
|
||||
if (schemaType.dataType?.match(/text|varchar|timestamp/i)) {
|
||||
return "string";
|
||||
}
|
||||
|
||||
@@ -48,21 +62,19 @@ export default function generateTypeDefinition({
|
||||
|
||||
fields.forEach((field) => {
|
||||
const nullValue = allValuesOptional
|
||||
? "?"
|
||||
: field.nullValue
|
||||
? "?"
|
||||
: field.fieldName?.match(defaultFieldsRegexp)
|
||||
? "?"
|
||||
: "";
|
||||
: field.notNullValue
|
||||
? ""
|
||||
: "?";
|
||||
|
||||
typesArrayTypeScript.push(
|
||||
` ${field.fieldName}${nullValue}: ${typeMap(
|
||||
field.dataType || ""
|
||||
)};`
|
||||
` ${field.fieldName}${nullValue}: ${typeMap(field)};`
|
||||
);
|
||||
|
||||
typesArrayJavascript.push(
|
||||
` * @property {${typeMap(field.dataType || "")}${nullValue}} ${
|
||||
` * @property {${typeMap(field)}${nullValue}} ${
|
||||
field.fieldName
|
||||
}`
|
||||
);
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import { SQLDeleteGeneratorParams } from "../../../types";
|
||||
import sqlEqualityParser from "../../../utils/sql-equality-parser";
|
||||
|
||||
interface SQLDeleteGenReturn {
|
||||
query: string;
|
||||
values: string[];
|
||||
@@ -8,13 +11,10 @@ interface SQLDeleteGenReturn {
|
||||
*/
|
||||
export default function sqlDeleteGenerator({
|
||||
tableName,
|
||||
data,
|
||||
deleteKeyValues,
|
||||
dbFullName,
|
||||
}: {
|
||||
data: any;
|
||||
tableName: string;
|
||||
dbFullName?: string;
|
||||
}): SQLDeleteGenReturn | undefined {
|
||||
data,
|
||||
}: SQLDeleteGeneratorParams): SQLDeleteGenReturn | undefined {
|
||||
const finalDbName = dbFullName ? `${dbFullName}.` : "";
|
||||
|
||||
try {
|
||||
@@ -23,17 +23,44 @@ export default function sqlDeleteGenerator({
|
||||
let deleteBatch: string[] = [];
|
||||
let queryArr: string[] = [];
|
||||
|
||||
Object.keys(data).forEach((ky) => {
|
||||
deleteBatch.push(`${ky}=?`);
|
||||
queryArr.push(data[ky]);
|
||||
});
|
||||
if (data) {
|
||||
Object.keys(data).forEach((ky) => {
|
||||
let value = data[ky] as string | number | null | undefined;
|
||||
const parsedValue =
|
||||
typeof value == "number" ? String(value) : value;
|
||||
|
||||
if (!parsedValue) return;
|
||||
|
||||
if (parsedValue.match(/%/)) {
|
||||
deleteBatch.push(`${ky} LIKE ?`);
|
||||
queryArr.push(parsedValue);
|
||||
} else {
|
||||
deleteBatch.push(`${ky}=?`);
|
||||
queryArr.push(parsedValue);
|
||||
}
|
||||
});
|
||||
} else if (deleteKeyValues) {
|
||||
deleteKeyValues.forEach((ky) => {
|
||||
let value = ky.value as string | number | null | undefined;
|
||||
const parsedValue =
|
||||
typeof value == "number" ? String(value) : value;
|
||||
|
||||
if (!parsedValue) return;
|
||||
|
||||
const operator = sqlEqualityParser(ky.operator || "EQUAL");
|
||||
|
||||
deleteBatch.push(`${ky.key} ${operator} ?`);
|
||||
queryArr.push(parsedValue);
|
||||
});
|
||||
}
|
||||
|
||||
queryStr += ` WHERE ${deleteBatch.join(" AND ")}`;
|
||||
|
||||
return {
|
||||
query: queryStr,
|
||||
values: queryArr,
|
||||
};
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
} catch (error: any) {
|
||||
console.log(`SQL delete gen ERROR: ${error.message}`);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import sqlEqualityParser from "../../../utils/sql-equality-parser";
|
||||
import { ServerQueryEqualities } from "../../../types";
|
||||
|
||||
type Params = {
|
||||
fieldName: string;
|
||||
value?: string;
|
||||
equality: (typeof ServerQueryEqualities)[number];
|
||||
};
|
||||
|
||||
/**
|
||||
* # SQL Gen Operator Gen
|
||||
* @description Generates an SQL operator for node module `mysql` or `serverless-mysql`
|
||||
*/
|
||||
export default function sqlGenOperatorGen({
|
||||
fieldName,
|
||||
value,
|
||||
equality,
|
||||
}: Params): string {
|
||||
if (value) {
|
||||
if (equality == "LIKE") {
|
||||
return `LOWER(${fieldName}) LIKE LOWER('%${value}%')`;
|
||||
} else if (equality == "LIKE_RAW") {
|
||||
return `LOWER(${fieldName}) LIKE LOWER('${value}')`;
|
||||
} else if (equality == "NOT LIKE") {
|
||||
return `LOWER(${fieldName}) NOT LIKE LOWER('%${value}%')`;
|
||||
} else if (equality == "NOT LIKE_RAW") {
|
||||
return `LOWER(${fieldName}) NOT LIKE LOWER('${value}')`;
|
||||
} else if (equality == "REGEXP") {
|
||||
return `LOWER(${fieldName}) REGEXP LOWER('${value}')`;
|
||||
} else if (equality == "FULLTEXT") {
|
||||
return `MATCH(${fieldName}) AGAINST('${value}' IN BOOLEAN MODE)`;
|
||||
} else if (equality == "NOT EQUAL") {
|
||||
return `${fieldName} != ${value}`;
|
||||
} else if (equality) {
|
||||
return `${fieldName} ${sqlEqualityParser(equality)} ${value}`;
|
||||
} else {
|
||||
return `${fieldName} = ${value}`;
|
||||
}
|
||||
} else {
|
||||
if (equality == "IS NULL") {
|
||||
return `${fieldName} IS NULL`;
|
||||
} else if (equality == "IS NOT NULL") {
|
||||
return `${fieldName} IS NOT NULL`;
|
||||
} else if (equality) {
|
||||
return `${fieldName} ${sqlEqualityParser(equality)} ?`;
|
||||
} else {
|
||||
return `${fieldName} = ?`;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import sqlEqualityParser from "../../../utils/sql-equality-parser";
|
||||
import {
|
||||
ServerQueryParam,
|
||||
ServerQueryParamsJoin,
|
||||
@@ -64,16 +65,30 @@ export default function sqlGenerator<
|
||||
typeof queryObj.value == "number"
|
||||
) {
|
||||
const valueParsed = String(queryObj.value);
|
||||
const operator = sqlEqualityParser(queryObj.equality || "EQUAL");
|
||||
|
||||
if (queryObj.equality == "LIKE") {
|
||||
str = `LOWER(${finalFieldName}) LIKE LOWER('%${valueParsed}%')`;
|
||||
} else if (queryObj.equality == "LIKE_RAW") {
|
||||
str = `LOWER(${finalFieldName}) LIKE LOWER(?)`;
|
||||
sqlSearhValues.push(valueParsed);
|
||||
} else if (queryObj.equality == "NOT LIKE") {
|
||||
str = `LOWER(${finalFieldName}) NOT LIKE LOWER('%${valueParsed}%')`;
|
||||
} else if (queryObj.equality == "NOT LIKE_RAW") {
|
||||
str = `LOWER(${finalFieldName}) NOT LIKE LOWER(?)`;
|
||||
sqlSearhValues.push(valueParsed);
|
||||
} else if (queryObj.equality == "REGEXP") {
|
||||
str = `${finalFieldName} REGEXP '${valueParsed}'`;
|
||||
str = `LOWER(${finalFieldName}) REGEXP LOWER(?)`;
|
||||
sqlSearhValues.push(valueParsed);
|
||||
} else if (queryObj.equality == "FULLTEXT") {
|
||||
str = `MATCH(${finalFieldName}) AGAINST('${valueParsed}' IN BOOLEAN MODE)`;
|
||||
str = `MATCH(${finalFieldName}) AGAINST(? IN BOOLEAN MODE)`;
|
||||
sqlSearhValues.push(valueParsed);
|
||||
} else if (queryObj.equality == "NOT EQUAL") {
|
||||
str = `${finalFieldName} != ?`;
|
||||
sqlSearhValues.push(valueParsed);
|
||||
} else if (queryObj.equality) {
|
||||
str = `${finalFieldName} ${operator} ?`;
|
||||
sqlSearhValues.push(valueParsed);
|
||||
} else {
|
||||
sqlSearhValues.push(valueParsed);
|
||||
}
|
||||
@@ -173,7 +188,7 @@ export default function sqlGenerator<
|
||||
} else if (genObject?.selectFields?.[0]) {
|
||||
if (genObject.join) {
|
||||
str += ` ${genObject.selectFields
|
||||
?.map((fld) => `${finalDbName}${tableName}.${fld}`)
|
||||
?.map((fld) => `${finalDbName}${tableName}.${String(fld)}`)
|
||||
.join(",")}`;
|
||||
} else {
|
||||
str += ` ${genObject.selectFields?.join(",")}`;
|
||||
@@ -272,12 +287,19 @@ export default function sqlGenerator<
|
||||
queryString += ` WHERE ${sqlSearhString.join(` ${stringOperator} `)}`;
|
||||
}
|
||||
|
||||
if (genObject?.order && !count)
|
||||
if (genObject?.group?.[0]) {
|
||||
queryString += ` GROUP BY ${genObject.group
|
||||
.map((g) => `\`${g.toString()}\``)
|
||||
.join(",")}`;
|
||||
}
|
||||
|
||||
if (genObject?.order && !count) {
|
||||
queryString += ` ORDER BY ${
|
||||
genObject.join
|
||||
? `${finalDbName}${tableName}.${String(genObject.order.field)}`
|
||||
: String(genObject.order.field)
|
||||
} ${genObject.order.strategy}`;
|
||||
}
|
||||
|
||||
if (genObject?.limit && !count) queryString += ` LIMIT ${genObject.limit}`;
|
||||
if (genObject?.offset && !count)
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
import mysql, { Connection } from "mysql";
|
||||
import { exec } from "child_process";
|
||||
import { promisify } from "util";
|
||||
|
||||
// Configuration interface
|
||||
interface DatabaseConfig {
|
||||
host: string;
|
||||
user: string;
|
||||
password: string;
|
||||
database?: string; // Optional for global connection
|
||||
}
|
||||
|
||||
// Master status interface
|
||||
interface MasterStatus {
|
||||
File: string;
|
||||
Position: number;
|
||||
Binlog_Do_DB?: string;
|
||||
Binlog_Ignore_DB?: string;
|
||||
}
|
||||
|
||||
function getConnection(config: DatabaseConfig): Connection {
|
||||
return mysql.createConnection(config);
|
||||
}
|
||||
|
||||
function getMasterStatus(config: DatabaseConfig): Promise<MasterStatus> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const connection = getConnection(config);
|
||||
connection.query("SHOW MASTER STATUS", (error, results) => {
|
||||
connection.end();
|
||||
if (error) reject(error);
|
||||
else resolve(results[0] as MasterStatus);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function syncDatabases() {
|
||||
const config: DatabaseConfig = {
|
||||
host: "localhost",
|
||||
user: "root",
|
||||
password: "your_password",
|
||||
};
|
||||
|
||||
let lastPosition: number | null = null; // Track last synced position
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
// Get current master status
|
||||
const { File, Position } = await getMasterStatus(config);
|
||||
|
||||
// Determine start position (use lastPosition or 4 if first run)
|
||||
const startPosition = lastPosition !== null ? lastPosition + 1 : 4;
|
||||
if (startPosition >= Position) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 5000)); // Wait 5 seconds if no new changes
|
||||
continue;
|
||||
}
|
||||
|
||||
// Execute mysqlbinlog to get changes
|
||||
const execPromise = promisify(exec);
|
||||
const { stdout } = await execPromise(
|
||||
`mysqlbinlog --database=db_master ${File} --start-position=${startPosition} --stop-position=${Position}`
|
||||
);
|
||||
|
||||
if (stdout) {
|
||||
const connection = getConnection({
|
||||
...config,
|
||||
database: "db_slave",
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
connection.query(stdout, (error) => {
|
||||
connection.end();
|
||||
if (error) reject(error);
|
||||
else {
|
||||
lastPosition = Position;
|
||||
console.log(
|
||||
`Synced up to position ${Position} at ${new Date().toISOString()}`
|
||||
);
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Sync error:", error);
|
||||
}
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 5000)); // Check every 5 seconds
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize db_slave with db_master data
|
||||
async function initializeSlave() {
|
||||
const config: DatabaseConfig = {
|
||||
host: "localhost",
|
||||
user: "root",
|
||||
password: "your_password",
|
||||
};
|
||||
|
||||
try {
|
||||
await promisify(exec)(
|
||||
`mysqldump -u ${config.user} -p${config.password} db_master > db_master_backup.sql`
|
||||
);
|
||||
await promisify(exec)(
|
||||
`mysql -u ${config.user} -p${config.password} db_slave < db_master_backup.sql`
|
||||
);
|
||||
console.log("Slave initialized with master data");
|
||||
} catch (error) {
|
||||
console.error("Initialization error:", error);
|
||||
}
|
||||
}
|
||||
|
||||
// Run the sync process
|
||||
async function main() {
|
||||
await initializeSlave();
|
||||
await syncDatabases();
|
||||
}
|
||||
|
||||
main().catch(console.error);
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
type Params = {};
|
||||
|
||||
function createDuplicateTablesTriggers({}: Params) {}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
```sql
|
||||
DELIMITER //
|
||||
|
||||
CREATE PROCEDURE dsql_replicate_databases(IN source_db VARCHAR(64), IN target_db VARCHAR(64))
|
||||
BEGIN
|
||||
-- Declare variables
|
||||
DECLARE done INT DEFAULT FALSE;
|
||||
DECLARE table_name VARCHAR(64);
|
||||
DECLARE column_list TEXT;
|
||||
DECLARE trigger_sql TEXT;
|
||||
|
||||
-- Cursor to iterate over tables in source_db
|
||||
DECLARE cur CURSOR FOR
|
||||
SELECT TABLE_NAME
|
||||
FROM INFORMATION_SCHEMA.TABLES
|
||||
WHERE TABLE_SCHEMA = source_db;
|
||||
|
||||
-- Handler for end of cursor
|
||||
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
|
||||
|
||||
-- Start transaction to ensure consistency
|
||||
START TRANSACTION;
|
||||
|
||||
-- Open cursor
|
||||
OPEN cur;
|
||||
|
||||
read_loop: LOOP
|
||||
FETCH cur INTO table_name;
|
||||
IF done THEN
|
||||
LEAVE read_loop;
|
||||
END IF;
|
||||
|
||||
-- Dynamically get column names for the table
|
||||
SELECT GROUP_CONCAT(CONCAT('NEW.', COLUMN_NAME))
|
||||
INTO column_list
|
||||
FROM INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE TABLE_SCHEMA = source_db
|
||||
AND TABLE_NAME = table_name;
|
||||
|
||||
-- Drop existing triggers if they exist
|
||||
SET @drop_trigger_insert = CONCAT('DROP TRIGGER IF EXISTS after_insert_', table_name);
|
||||
SET @drop_trigger_update = CONCAT('DROP TRIGGER IF EXISTS after_update_', table_name);
|
||||
SET @drop_trigger_delete = CONCAT('DROP TRIGGER IF EXISTS after_delete_', table_name);
|
||||
PREPARE stmt_drop_insert FROM @drop_trigger_insert;
|
||||
EXECUTE stmt_drop_insert;
|
||||
DEALLOCATE PREPARE stmt_drop_insert;
|
||||
PREPARE stmt_drop_update FROM @drop_trigger_update;
|
||||
EXECUTE stmt_drop_update;
|
||||
DEALLOCATE PREPARE stmt_drop_update;
|
||||
PREPARE stmt_drop_delete FROM @drop_trigger_delete;
|
||||
EXECUTE stmt_drop_delete;
|
||||
DEALLOCATE PREPARE stmt_drop_delete;
|
||||
|
||||
-- Create INSERT trigger
|
||||
SET @trigger_sql = CONCAT(
|
||||
'CREATE TRIGGER after_insert_', table_name,
|
||||
' AFTER INSERT ON ', source_db, '.', table_name, ' FOR EACH ROW ',
|
||||
'BEGIN ',
|
||||
'INSERT INTO ', target_db, '.', table_name, ' (',
|
||||
(SELECT GROUP_CONCAT(COLUMN_NAME) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = source_db AND TABLE_NAME = table_name), ') ',
|
||||
'VALUES (', column_list, '); ',
|
||||
'END;'
|
||||
);
|
||||
PREPARE stmt FROM @trigger_sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- Create UPDATE trigger
|
||||
SET @trigger_sql = CONCAT(
|
||||
'CREATE TRIGGER after_update_', table_name,
|
||||
' AFTER UPDATE ON ', source_db, '.', table_name, ' FOR EACH ROW ',
|
||||
'BEGIN ',
|
||||
'UPDATE ', target_db, '.', table_name, ' SET ',
|
||||
(SELECT GROUP_CONCAT(CONCAT(COLUMN_NAME, '=NEW.', COLUMN_NAME))
|
||||
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = source_db AND TABLE_NAME = table_name),
|
||||
' WHERE ',
|
||||
(SELECT CONCAT('id=NEW.id')
|
||||
FROM INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE TABLE_SCHEMA = source_db AND TABLE_NAME = table_name AND COLUMN_NAME = 'id' LIMIT 1), '; ',
|
||||
'END;'
|
||||
);
|
||||
PREPARE stmt FROM @trigger_sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- Create DELETE trigger
|
||||
SET @trigger_sql = CONCAT(
|
||||
'CREATE TRIGGER after_delete_', table_name,
|
||||
' AFTER DELETE ON ', source_db, '.', table_name, ' FOR EACH ROW ',
|
||||
'BEGIN ',
|
||||
'DELETE FROM ', target_db, '.', table_name, ' WHERE id=OLD.id; ',
|
||||
'END;'
|
||||
);
|
||||
PREPARE stmt FROM @trigger_sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
END LOOP;
|
||||
|
||||
CLOSE cur;
|
||||
COMMIT;
|
||||
|
||||
END //
|
||||
|
||||
DELIMITER ;
|
||||
```
|
||||
+212
@@ -0,0 +1,212 @@
|
||||
DELIMITER / / CREATE PROCEDURE replicate_databases(
|
||||
IN source_db VARCHAR(64),
|
||||
IN target_db VARCHAR(64)
|
||||
) BEGIN -- Declare variables
|
||||
DECLARE done INT DEFAULT FALSE;
|
||||
|
||||
DECLARE table_name VARCHAR(64);
|
||||
|
||||
DECLARE column_list TEXT;
|
||||
|
||||
DECLARE trigger_sql TEXT;
|
||||
|
||||
-- Cursor to iterate over tables in source_db
|
||||
DECLARE cur CURSOR FOR
|
||||
SELECT
|
||||
TABLE_NAME
|
||||
FROM
|
||||
INFORMATION_SCHEMA.TABLES
|
||||
WHERE
|
||||
TABLE_SCHEMA = source_db;
|
||||
|
||||
-- Handler for end of cursor
|
||||
DECLARE CONTINUE HANDLER FOR NOT FOUND
|
||||
SET
|
||||
done = TRUE;
|
||||
|
||||
-- Start transaction to ensure consistency
|
||||
START TRANSACTION;
|
||||
|
||||
-- Open cursor
|
||||
OPEN cur;
|
||||
|
||||
read_loop: LOOP FETCH cur INTO table_name;
|
||||
|
||||
IF done THEN LEAVE read_loop;
|
||||
|
||||
END IF;
|
||||
|
||||
-- Dynamically get column names for the table
|
||||
SELECT
|
||||
GROUP_CONCAT(CONCAT('NEW.', COLUMN_NAME)) INTO column_list
|
||||
FROM
|
||||
INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE
|
||||
TABLE_SCHEMA = source_db
|
||||
AND TABLE_NAME = table_name;
|
||||
|
||||
-- Drop existing triggers if they exist
|
||||
SET
|
||||
@drop_trigger_insert = CONCAT(
|
||||
'DROP TRIGGER IF EXISTS after_insert_',
|
||||
table_name
|
||||
);
|
||||
|
||||
SET
|
||||
@drop_trigger_update = CONCAT(
|
||||
'DROP TRIGGER IF EXISTS after_update_',
|
||||
table_name
|
||||
);
|
||||
|
||||
SET
|
||||
@drop_trigger_delete = CONCAT(
|
||||
'DROP TRIGGER IF EXISTS after_delete_',
|
||||
table_name
|
||||
);
|
||||
|
||||
PREPARE stmt_drop_insert
|
||||
FROM
|
||||
@drop_trigger_insert;
|
||||
|
||||
EXECUTE stmt_drop_insert;
|
||||
|
||||
DEALLOCATE PREPARE stmt_drop_insert;
|
||||
|
||||
PREPARE stmt_drop_update
|
||||
FROM
|
||||
@drop_trigger_update;
|
||||
|
||||
EXECUTE stmt_drop_update;
|
||||
|
||||
DEALLOCATE PREPARE stmt_drop_update;
|
||||
|
||||
PREPARE stmt_drop_delete
|
||||
FROM
|
||||
@drop_trigger_delete;
|
||||
|
||||
EXECUTE stmt_drop_delete;
|
||||
|
||||
DEALLOCATE PREPARE stmt_drop_delete;
|
||||
|
||||
-- Create INSERT trigger
|
||||
SET
|
||||
@trigger_sql = CONCAT(
|
||||
'CREATE TRIGGER after_insert_',
|
||||
table_name,
|
||||
' AFTER INSERT ON ',
|
||||
source_db,
|
||||
'.',
|
||||
table_name,
|
||||
' FOR EACH ROW ',
|
||||
'BEGIN ',
|
||||
'INSERT INTO ',
|
||||
target_db,
|
||||
'.',
|
||||
table_name,
|
||||
' (',
|
||||
(
|
||||
SELECT
|
||||
GROUP_CONCAT(COLUMN_NAME)
|
||||
FROM
|
||||
INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE
|
||||
TABLE_SCHEMA = source_db
|
||||
AND TABLE_NAME = table_name
|
||||
),
|
||||
') ',
|
||||
'VALUES (',
|
||||
column_list,
|
||||
'); ',
|
||||
'END;'
|
||||
);
|
||||
|
||||
PREPARE stmt
|
||||
FROM
|
||||
@trigger_sql;
|
||||
|
||||
EXECUTE stmt;
|
||||
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- Create UPDATE trigger
|
||||
SET
|
||||
@trigger_sql = CONCAT(
|
||||
'CREATE TRIGGER after_update_',
|
||||
table_name,
|
||||
' AFTER UPDATE ON ',
|
||||
source_db,
|
||||
'.',
|
||||
table_name,
|
||||
' FOR EACH ROW ',
|
||||
'BEGIN ',
|
||||
'UPDATE ',
|
||||
target_db,
|
||||
'.',
|
||||
table_name,
|
||||
' SET ',
|
||||
(
|
||||
SELECT
|
||||
GROUP_CONCAT(CONCAT(COLUMN_NAME, '=NEW.', COLUMN_NAME))
|
||||
FROM
|
||||
INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE
|
||||
TABLE_SCHEMA = source_db
|
||||
AND TABLE_NAME = table_name
|
||||
),
|
||||
' WHERE ',
|
||||
(
|
||||
SELECT
|
||||
CONCAT('id=NEW.id')
|
||||
FROM
|
||||
INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE
|
||||
TABLE_SCHEMA = source_db
|
||||
AND TABLE_NAME = table_name
|
||||
AND COLUMN_NAME = 'id'
|
||||
LIMIT
|
||||
1
|
||||
), '; ', 'END;'
|
||||
);
|
||||
|
||||
PREPARE stmt
|
||||
FROM
|
||||
@trigger_sql;
|
||||
|
||||
EXECUTE stmt;
|
||||
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- Create DELETE trigger
|
||||
SET
|
||||
@trigger_sql = CONCAT(
|
||||
'CREATE TRIGGER after_delete_',
|
||||
table_name,
|
||||
' AFTER DELETE ON ',
|
||||
source_db,
|
||||
'.',
|
||||
table_name,
|
||||
' FOR EACH ROW ',
|
||||
'BEGIN ',
|
||||
'DELETE FROM ',
|
||||
target_db,
|
||||
'.',
|
||||
table_name,
|
||||
' WHERE id=OLD.id; ',
|
||||
'END;'
|
||||
);
|
||||
|
||||
PREPARE stmt
|
||||
FROM
|
||||
@trigger_sql;
|
||||
|
||||
EXECUTE stmt;
|
||||
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
END LOOP;
|
||||
|
||||
CLOSE cur;
|
||||
|
||||
COMMIT;
|
||||
|
||||
END / / DELIMITER;
|
||||
@@ -0,0 +1,23 @@
|
||||
export const TriggerParadigms = ["sync_tables", "sync_dbs"] as const;
|
||||
|
||||
type Params = {
|
||||
userId?: string | number;
|
||||
paradigm: (typeof TriggerParadigms)[number];
|
||||
dbId?: string | number;
|
||||
tableName?: string;
|
||||
};
|
||||
|
||||
export default function grabTriggerName({
|
||||
userId,
|
||||
paradigm,
|
||||
dbId,
|
||||
tableName,
|
||||
}: Params) {
|
||||
let triggerName = `dsql_trig_${paradigm}`;
|
||||
|
||||
if (userId) triggerName += `_${userId}`;
|
||||
if (dbId) triggerName += `_${dbId}`;
|
||||
if (tableName) triggerName += `_${tableName}`;
|
||||
|
||||
return triggerName;
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
import { DSQL_DatabaseSchemaType, DSQL_TableSchemaType } from "../../../types";
|
||||
|
||||
const TriggerTypes = [
|
||||
{
|
||||
name: "after_insert",
|
||||
value: "INSERT",
|
||||
},
|
||||
{
|
||||
name: "after_update",
|
||||
value: "UPDATE",
|
||||
},
|
||||
{
|
||||
name: "after_delete",
|
||||
value: "DELETE",
|
||||
},
|
||||
] as const;
|
||||
|
||||
export type TriggerSQLGenParams = {
|
||||
type: (typeof TriggerTypes)[number];
|
||||
srcDbSchema: DSQL_DatabaseSchemaType;
|
||||
srcTableSchema: DSQL_TableSchemaType;
|
||||
content: string;
|
||||
proceedureName: string;
|
||||
};
|
||||
|
||||
export default function triggerSQLGen({
|
||||
type,
|
||||
srcDbSchema,
|
||||
srcTableSchema,
|
||||
content,
|
||||
proceedureName,
|
||||
}: TriggerSQLGenParams) {
|
||||
let sql = `DELIMITER //\n`;
|
||||
|
||||
sql += `CREATE PROCEDURE ${proceedureName}`;
|
||||
sql += `\nBEGIN`;
|
||||
|
||||
sql += ` ${content}`;
|
||||
|
||||
sql += `\nEND //`;
|
||||
sql += `\nDELIMITER\n`;
|
||||
|
||||
return sql;
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
import { DSQL_DatabaseSchemaType, DSQL_TableSchemaType } from "../../../types";
|
||||
import triggerSQLGen, { TriggerSQLGenParams } from "./trigger-sql-gen";
|
||||
|
||||
type Params = TriggerSQLGenParams & {
|
||||
dstDbSchema: DSQL_DatabaseSchemaType;
|
||||
dstTableSchema: DSQL_TableSchemaType;
|
||||
};
|
||||
|
||||
export default function tableReplicationTriggerSQLGen({
|
||||
type,
|
||||
dstDbSchema,
|
||||
dstTableSchema,
|
||||
srcDbSchema,
|
||||
srcTableSchema,
|
||||
userId,
|
||||
paradigm,
|
||||
}: Params) {
|
||||
let sql = `CREATE TRIGGER`;
|
||||
|
||||
const srcColumns = srcTableSchema.fields
|
||||
.map((fld) => fld.fieldName)
|
||||
.filter((fld) => typeof fld == "string");
|
||||
const dstColumns = dstTableSchema.fields
|
||||
.map((fld) => fld.fieldName)
|
||||
.filter((fld) => typeof fld == "string");
|
||||
|
||||
if (type.name == "after_insert") {
|
||||
sql += ` INSERT INTO ${dstDbSchema.dbFullName}.${dstTableSchema.tableName}`;
|
||||
sql += ` (${dstColumns.join(",")})`;
|
||||
sql += ` VALUES (${dstColumns.map((c) => `NEW.${c}`).join(",")})`;
|
||||
} else if (type.name == "after_update") {
|
||||
sql += ` UPDATE ${dstDbSchema.dbFullName}.${dstTableSchema.tableName}`;
|
||||
sql += ` SET ${dstColumns.map((c) => `${c}=NEW.${c}`).join(",")}`;
|
||||
sql += ` WHERE id = NEW.id`;
|
||||
} else if (type.name == "after_delete") {
|
||||
sql += ` DELETE FROM ${dstDbSchema.dbFullName}.${dstTableSchema.tableName}`;
|
||||
sql += ` WHERE id = OLD.id`;
|
||||
}
|
||||
|
||||
return triggerSQLGen({
|
||||
content: sql,
|
||||
srcDbSchema,
|
||||
srcTableSchema,
|
||||
type,
|
||||
paradigm,
|
||||
userId,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
```sql
|
||||
DELIMITER //
|
||||
|
||||
CREATE PROCEDURE dsql_replicate_two_tables(
|
||||
IN source_db VARCHAR(64),
|
||||
IN target_db VARCHAR(64),
|
||||
IN source_table VARCHAR(64),
|
||||
IN target_table VARCHAR(64)
|
||||
)
|
||||
BEGIN
|
||||
-- Declare variables
|
||||
DECLARE column_list TEXT;
|
||||
DECLARE set_clause TEXT;
|
||||
DECLARE trigger_sql TEXT;
|
||||
|
||||
-- Start transaction to ensure consistency
|
||||
START TRANSACTION;
|
||||
|
||||
-- Dynamically get column names for the source table
|
||||
SELECT GROUP_CONCAT(CONCAT('NEW.', COLUMN_NAME))
|
||||
INTO column_list
|
||||
FROM INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE TABLE_SCHEMA = source_db
|
||||
AND TABLE_NAME = source_table;
|
||||
|
||||
SELECT GROUP_CONCAT(CONCAT(COLUMN_NAME, '=NEW.', COLUMN_NAME))
|
||||
INTO set_clause
|
||||
FROM INFORMATION_SCHEMA.COLUMNS
|
||||
WHERE TABLE_SCHEMA = source_db
|
||||
AND TABLE_NAME = source_table;
|
||||
|
||||
-- Drop existing triggers if they exist
|
||||
SET @drop_trigger_insert = CONCAT('DROP TRIGGER IF EXISTS after_insert_', source_table);
|
||||
SET @drop_trigger_update = CONCAT('DROP TRIGGER IF EXISTS after_update_', source_table);
|
||||
SET @drop_trigger_delete = CONCAT('DROP TRIGGER IF EXISTS after_delete_', source_table);
|
||||
PREPARE stmt_drop_insert FROM @drop_trigger_insert;
|
||||
EXECUTE stmt_drop_insert;
|
||||
DEALLOCATE PREPARE stmt_drop_insert;
|
||||
PREPARE stmt_drop_update FROM @drop_trigger_update;
|
||||
EXECUTE stmt_drop_update;
|
||||
DEALLOCATE PREPARE stmt_drop_update;
|
||||
PREPARE stmt_drop_delete FROM @drop_trigger_delete;
|
||||
EXECUTE stmt_drop_delete;
|
||||
DEALLOCATE PREPARE stmt_drop_delete;
|
||||
|
||||
-- Create INSERT trigger
|
||||
SET @trigger_sql = CONCAT(
|
||||
'CREATE TRIGGER after_insert_', source_table,
|
||||
' AFTER INSERT ON ', source_db, '.', source_table, ' FOR EACH ROW ',
|
||||
'BEGIN ',
|
||||
'INSERT INTO ', target_db, '.', target_table, ' (',
|
||||
(SELECT GROUP_CONCAT(COLUMN_NAME) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = source_db AND TABLE_NAME = source_table), ') ',
|
||||
'VALUES (', column_list, '); ',
|
||||
'END;'
|
||||
);
|
||||
PREPARE stmt FROM @trigger_sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- Create UPDATE trigger
|
||||
-- Assume 'id' as the primary key; adjust if different
|
||||
SET @trigger_sql = CONCAT(
|
||||
'CREATE TRIGGER after_update_', source_table,
|
||||
' AFTER UPDATE ON ', source_db, '.', source_table, ' FOR EACH ROW ',
|
||||
'BEGIN ',
|
||||
'UPDATE ', target_db, '.', target_table, ' SET ',
|
||||
set_clause,
|
||||
' WHERE id = NEW.id; ',
|
||||
'END;'
|
||||
);
|
||||
PREPARE stmt FROM @trigger_sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- Create DELETE trigger
|
||||
SET @trigger_sql = CONCAT(
|
||||
'CREATE TRIGGER after_delete_', source_table,
|
||||
' AFTER DELETE ON ', source_db, '.', source_table, ' FOR EACH ROW ',
|
||||
'BEGIN ',
|
||||
'DELETE FROM ', target_db, '.', target_table, ' WHERE id = OLD.id; ',
|
||||
'END;'
|
||||
);
|
||||
PREPARE stmt FROM @trigger_sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
COMMIT;
|
||||
|
||||
END //
|
||||
|
||||
DELIMITER ;
|
||||
```
|
||||
@@ -0,0 +1,53 @@
|
||||
import { DSQL_DatabaseSchemaType, DSQL_TableSchemaType } from "../../../types";
|
||||
import grabTriggerName, { TriggerParadigms } from "./grab-trigger-name";
|
||||
|
||||
const TriggerTypes = [
|
||||
{
|
||||
name: "after_insert",
|
||||
value: "INSERT",
|
||||
},
|
||||
{
|
||||
name: "after_update",
|
||||
value: "UPDATE",
|
||||
},
|
||||
{
|
||||
name: "after_delete",
|
||||
value: "DELETE",
|
||||
},
|
||||
] as const;
|
||||
|
||||
export type TriggerSQLGenParams = {
|
||||
type: (typeof TriggerTypes)[number];
|
||||
srcDbSchema: DSQL_DatabaseSchemaType;
|
||||
srcTableSchema: DSQL_TableSchemaType;
|
||||
content: string;
|
||||
userId?: string | number;
|
||||
paradigm: (typeof TriggerParadigms)[number];
|
||||
};
|
||||
|
||||
export default function triggerSQLGen({
|
||||
type,
|
||||
srcDbSchema,
|
||||
srcTableSchema,
|
||||
content,
|
||||
userId,
|
||||
paradigm,
|
||||
}: TriggerSQLGenParams) {
|
||||
let sql = `CREATE TRIGGER`;
|
||||
|
||||
let triggerName = grabTriggerName({
|
||||
paradigm,
|
||||
dbId: srcDbSchema.id,
|
||||
tableName: srcTableSchema.tableName,
|
||||
userId,
|
||||
});
|
||||
|
||||
sql += ` ${triggerName}`;
|
||||
sql += ` AFTER ${type.value} ON ${srcTableSchema.tableName}`;
|
||||
sql += ` FOR EACH ROW BEGIN`;
|
||||
|
||||
sql += ` ${content}`;
|
||||
sql += ` END`;
|
||||
|
||||
return sql;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import { DsqlCrudQueryObject } from "../../../../types";
|
||||
import { DsqlTables } from "../../../../types/dsql";
|
||||
import dsqlCrud from "../../../../utils/data-fetching/crud";
|
||||
import query from "./query";
|
||||
import _ from "lodash";
|
||||
import _n from "../../../../utils/numberfy";
|
||||
|
||||
export type GrabUserResourceParams<T extends { [k: string]: any } = any> = {
|
||||
query?: DsqlCrudQueryObject<T>;
|
||||
userId?: string | number;
|
||||
tableName: (typeof DsqlTables)[number];
|
||||
count?: boolean;
|
||||
countOnly?: boolean;
|
||||
noLimit?: boolean;
|
||||
isSuperUser?: boolean;
|
||||
targetID?: string | number;
|
||||
};
|
||||
|
||||
export default async function dbGrabUserResource<
|
||||
T extends { [k: string]: any } = any
|
||||
>(params: GrabUserResourceParams<T>) {
|
||||
let queryObject = query(params);
|
||||
|
||||
let result = await dsqlCrud({
|
||||
action: "get",
|
||||
table: params.tableName,
|
||||
query: queryObject,
|
||||
count: params.count,
|
||||
countOnly: params.countOnly,
|
||||
});
|
||||
|
||||
const payload = result?.payload as T[] | undefined;
|
||||
|
||||
return {
|
||||
batch: payload || null,
|
||||
single: payload?.[0] || null,
|
||||
debug: {
|
||||
queryObject: result?.queryObject,
|
||||
error: result?.error,
|
||||
msg: result?.msg,
|
||||
},
|
||||
count: _n(result?.count),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { GrabUserResourceParams } from ".";
|
||||
import _ from "lodash";
|
||||
import { DsqlCrudQueryObject } from "../../../../types";
|
||||
import ResourceLimits from "../../../../dict/resource-limits";
|
||||
import _n from "../../../../utils/numberfy";
|
||||
|
||||
export default function (params?: GrabUserResourceParams) {
|
||||
let queryObject: DsqlCrudQueryObject<{ [k: string]: any }> = {
|
||||
limit: params?.noLimit ? undefined : ResourceLimits["general"],
|
||||
order: {
|
||||
field: "id",
|
||||
strategy: "DESC",
|
||||
},
|
||||
};
|
||||
|
||||
if (params?.targetID) {
|
||||
const targetIDQuery: DsqlCrudQueryObject<{ [k: string]: any }> = {
|
||||
query: {
|
||||
id: {
|
||||
value: _n(params.targetID).toString(),
|
||||
},
|
||||
},
|
||||
};
|
||||
queryObject = _.merge(queryObject, targetIDQuery);
|
||||
}
|
||||
|
||||
let queryFixedObject: DsqlCrudQueryObject<{ [k: string]: any }> =
|
||||
params?.isSuperUser
|
||||
? {}
|
||||
: {
|
||||
query: {
|
||||
user_id: {
|
||||
value: String(params?.userId || 0),
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
return _.merge(queryObject, params?.query, queryFixedObject);
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import { DSQL_DATASQUIREL_MARIADB_USERS } from "../../../types/dsql";
|
||||
import { UserType } from "../../../types";
|
||||
import dbHandler from "../../backend/dbHandler";
|
||||
import normalizeText from "../../../utils/normalize-text";
|
||||
import decrypt from "../../dsql/decrypt";
|
||||
|
||||
type Params = {
|
||||
user: UserType;
|
||||
existingRecord?: DSQL_DATASQUIREL_MARIADB_USERS | null;
|
||||
updatedRecord: DSQL_DATASQUIREL_MARIADB_USERS;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
msg?: string;
|
||||
success?: boolean;
|
||||
};
|
||||
|
||||
export default async function handleMariadbUserCreation({
|
||||
user,
|
||||
existingRecord,
|
||||
updatedRecord,
|
||||
}: Params): Promise<Return> {
|
||||
const parsedPassword = decrypt({
|
||||
encryptedString: updatedRecord?.password || "",
|
||||
});
|
||||
|
||||
if (existingRecord?.id && updatedRecord?.id) {
|
||||
if (
|
||||
existingRecord.username !== updatedRecord.username ||
|
||||
existingRecord.host !== updatedRecord.host
|
||||
) {
|
||||
const renameSQLUser = await dbHandler({
|
||||
query: normalizeText(`
|
||||
RENAME USER '${existingRecord.username}'@'${existingRecord.host}' \
|
||||
TO '${updatedRecord.username}'@'${updatedRecord.host}'
|
||||
`),
|
||||
});
|
||||
|
||||
if (!renameSQLUser) {
|
||||
await createNewSQLUser({
|
||||
host: updatedRecord.host,
|
||||
password: parsedPassword,
|
||||
username: updatedRecord.username,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const updateSQLUser = await dbHandler({
|
||||
query: normalizeText(`
|
||||
ALTER USER '${updatedRecord.username}'@'${updatedRecord.host}' \
|
||||
IDENTIFIED BY '${parsedPassword}'
|
||||
`),
|
||||
});
|
||||
|
||||
if (!updateSQLUser) {
|
||||
await createNewSQLUser({
|
||||
host: updatedRecord.host,
|
||||
password: parsedPassword,
|
||||
username: updatedRecord.username,
|
||||
});
|
||||
}
|
||||
} else if (!existingRecord?.id && updatedRecord?.id) {
|
||||
await createNewSQLUser({
|
||||
host: updatedRecord.host,
|
||||
password: parsedPassword,
|
||||
username: updatedRecord.username,
|
||||
});
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
type CreateNewUserParams = {
|
||||
username?: string;
|
||||
host?: string;
|
||||
password?: string;
|
||||
};
|
||||
|
||||
export async function createNewSQLUser({
|
||||
host,
|
||||
password,
|
||||
username,
|
||||
}: CreateNewUserParams) {
|
||||
return await dbHandler({
|
||||
query: `CREATE USER IF NOT EXISTS '${username}'@'${host}' IDENTIFIED BY '${password}'`,
|
||||
});
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
import {
|
||||
DSQL_DATASQUIREL_MARIADB_USER_DATABASES,
|
||||
DSQL_DATASQUIREL_MARIADB_USER_PRIVILEGES,
|
||||
DSQL_DATASQUIREL_MARIADB_USER_TABLES,
|
||||
DSQL_DATASQUIREL_MARIADB_USERS,
|
||||
DsqlTables,
|
||||
} from "../../../types/dsql";
|
||||
import { UserType } from "../../../types";
|
||||
import dsqlCrud from "../../../utils/data-fetching/crud";
|
||||
import _n from "../../../utils/numberfy";
|
||||
|
||||
type Params = {
|
||||
user: UserType;
|
||||
updatedRecord: DSQL_DATASQUIREL_MARIADB_USERS;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
msg?: string;
|
||||
success?: boolean;
|
||||
};
|
||||
|
||||
export default async function handleMariadbUserGrantsForDatabasesCleanUpRecords({
|
||||
user,
|
||||
updatedRecord,
|
||||
}: Params): Promise<Return> {
|
||||
/**
|
||||
* # Clean up Records
|
||||
*/
|
||||
await dsqlCrud<
|
||||
DSQL_DATASQUIREL_MARIADB_USER_DATABASES,
|
||||
(typeof DsqlTables)[number]
|
||||
>({
|
||||
action: "delete",
|
||||
table: "mariadb_user_databases",
|
||||
deleteData: {
|
||||
user_id: user.id,
|
||||
mariadb_user_id: updatedRecord.id,
|
||||
},
|
||||
});
|
||||
|
||||
await dsqlCrud<
|
||||
DSQL_DATASQUIREL_MARIADB_USER_PRIVILEGES,
|
||||
(typeof DsqlTables)[number]
|
||||
>({
|
||||
action: "delete",
|
||||
table: "mariadb_user_privileges",
|
||||
deleteData: {
|
||||
user_id: user.id,
|
||||
mariadb_user_id: updatedRecord.id,
|
||||
},
|
||||
});
|
||||
|
||||
await dsqlCrud<
|
||||
DSQL_DATASQUIREL_MARIADB_USER_TABLES,
|
||||
(typeof DsqlTables)[number]
|
||||
>({
|
||||
action: "delete",
|
||||
table: "mariadb_user_tables",
|
||||
deleteData: {
|
||||
user_id: user.id,
|
||||
mariadb_user_id: updatedRecord.id,
|
||||
},
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
import { DSQL_DATASQUIREL_MARIADB_USERS } from "../../../types/dsql";
|
||||
import {
|
||||
DatabaseScopedAccessObject,
|
||||
UserSQLPermissions,
|
||||
UserType,
|
||||
} from "../../../types";
|
||||
import grabDbFullName from "../../../utils/grab-db-full-name";
|
||||
import dbHandler from "../../backend/dbHandler";
|
||||
import normalizeText from "../../../utils/normalize-text";
|
||||
|
||||
type Params = {
|
||||
currentAccessedDatabase: DatabaseScopedAccessObject;
|
||||
user: UserType;
|
||||
updatedRecord: DSQL_DATASQUIREL_MARIADB_USERS;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
msg?: string;
|
||||
success?: boolean;
|
||||
};
|
||||
|
||||
export default async function handleMariadbUserGrantsForDatabasesRecreateGrants({
|
||||
currentAccessedDatabase,
|
||||
user,
|
||||
updatedRecord,
|
||||
}: Params): Promise<Return> {
|
||||
const { accessedDatabase, dbSlug, allGrants, allTables, grants, tables } =
|
||||
currentAccessedDatabase;
|
||||
|
||||
const dbFullName = grabDbFullName({
|
||||
user,
|
||||
dbName: dbSlug,
|
||||
});
|
||||
|
||||
if (allGrants && allTables) {
|
||||
const grantAllPrivileges = await dbHandler({
|
||||
query: normalizeText(`
|
||||
GRANT ALL PRIVILEGES ON \`${dbFullName}\`.* TO \
|
||||
'${updatedRecord.username}'@'${updatedRecord.host}'
|
||||
`),
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
if (allGrants && tables?.[0]) {
|
||||
for (let t = 0; t < tables.length; t++) {
|
||||
const table = tables[t];
|
||||
|
||||
// queries.push(
|
||||
// normalizeText(`
|
||||
// GRANT ALL PRIVILEGES ON \`${dbFullName}\`.\`${table.tableSlug}\` \
|
||||
// TO '${updatedRecord.username}'@'${updatedRecord.host}'
|
||||
// `)
|
||||
// );
|
||||
|
||||
const grantAllPrivilegesToTables = await dbHandler({
|
||||
query: normalizeText(`
|
||||
GRANT ALL PRIVILEGES ON \`${dbFullName}\`.\`${table.tableSlug}\` \
|
||||
TO '${updatedRecord.username}'@'${updatedRecord.host}'
|
||||
`),
|
||||
});
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
if (grants?.[0]) {
|
||||
const isGrantsInalid = grants.find(
|
||||
(g) => !UserSQLPermissions.includes(g)
|
||||
);
|
||||
|
||||
if (isGrantsInalid) {
|
||||
return { msg: `grants is/are invalid!` };
|
||||
}
|
||||
|
||||
if (tables?.[0]) {
|
||||
for (let t = 0; t < tables.length; t++) {
|
||||
const table = tables[t];
|
||||
|
||||
const grantSpecificPrivilegesToTables = await dbHandler({
|
||||
query: normalizeText(`
|
||||
GRANT ${grants.join(",")} ON \
|
||||
\`${dbFullName}\`.\`${table.tableSlug}\` TO \
|
||||
'${updatedRecord.username}'@'${updatedRecord.host}'
|
||||
`),
|
||||
});
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
} else {
|
||||
const grantSecificPrivilegesToAllTables = await dbHandler({
|
||||
query: normalizeText(`
|
||||
GRANT ${grants.join(",")} ON \
|
||||
\`${dbFullName}\`.* TO \
|
||||
'${updatedRecord.username}'@'${updatedRecord.host}'
|
||||
`),
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
import {
|
||||
DSQL_DATASQUIREL_MARIADB_USER_DATABASES,
|
||||
DSQL_DATASQUIREL_MARIADB_USER_PRIVILEGES,
|
||||
DSQL_DATASQUIREL_MARIADB_USER_TABLES,
|
||||
DSQL_DATASQUIREL_MARIADB_USERS,
|
||||
DsqlTables,
|
||||
} from "../../../types/dsql";
|
||||
import {
|
||||
DatabaseScopedAccessObject,
|
||||
UserSQLPermissions,
|
||||
UserType,
|
||||
} from "../../../types";
|
||||
import dsqlCrud from "../../../utils/data-fetching/crud";
|
||||
import _n from "../../../utils/numberfy";
|
||||
|
||||
type Params = {
|
||||
currentAccessedDatabase: DatabaseScopedAccessObject;
|
||||
user: UserType;
|
||||
updatedRecord: DSQL_DATASQUIREL_MARIADB_USERS;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
msg?: string;
|
||||
success?: boolean;
|
||||
};
|
||||
|
||||
export default async function handleMariadbUserGrantsForDatabasesRecreateRecordsForDatabase({
|
||||
currentAccessedDatabase,
|
||||
user,
|
||||
updatedRecord,
|
||||
}: Params): Promise<Return> {
|
||||
const { accessedDatabase, dbSlug, allGrants, allTables, grants, tables } =
|
||||
currentAccessedDatabase;
|
||||
|
||||
const insertSQLDbRecord = await dsqlCrud<
|
||||
DSQL_DATASQUIREL_MARIADB_USER_DATABASES,
|
||||
(typeof DsqlTables)[number]
|
||||
>({
|
||||
action: "insert",
|
||||
table: "mariadb_user_databases",
|
||||
data: {
|
||||
all_privileges: allGrants ? 1 : 0,
|
||||
all_tables: allTables ? 1 : 0,
|
||||
db_id: _n(accessedDatabase.dbId),
|
||||
db_slug: accessedDatabase.dbSlug,
|
||||
db_schema_id: _n(accessedDatabase.dbSchemaId),
|
||||
user_id: user.id,
|
||||
mariadb_user_id: updatedRecord.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (tables?.[0]) {
|
||||
for (let t = 0; t < tables.length; t++) {
|
||||
const table = tables[t];
|
||||
|
||||
const insertTable = await dsqlCrud<
|
||||
DSQL_DATASQUIREL_MARIADB_USER_TABLES,
|
||||
(typeof DsqlTables)[number]
|
||||
>({
|
||||
action: "insert",
|
||||
table: "mariadb_user_tables",
|
||||
data: {
|
||||
all_privileges: allGrants ? 1 : 0,
|
||||
all_fields: 1,
|
||||
user_id: user.id,
|
||||
mariadb_user_id: updatedRecord.id,
|
||||
table_slug: table.tableSlug,
|
||||
db_id: _n(table.dbId),
|
||||
db_slug: table.dbSlug,
|
||||
db_schema_id: _n(table.dbSchemaId),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (grants?.[0]) {
|
||||
const isGrantsInalid = grants.find(
|
||||
(g) => !UserSQLPermissions.includes(g)
|
||||
);
|
||||
|
||||
if (isGrantsInalid) {
|
||||
return { msg: `grants is/are invalid!` };
|
||||
}
|
||||
|
||||
for (let t = 0; t < grants.length; t++) {
|
||||
const grant = grants[t];
|
||||
|
||||
await dsqlCrud<
|
||||
DSQL_DATASQUIREL_MARIADB_USER_PRIVILEGES,
|
||||
(typeof DsqlTables)[number]
|
||||
>({
|
||||
action: "insert",
|
||||
table: "mariadb_user_privileges",
|
||||
data: {
|
||||
user_id: user.id,
|
||||
mariadb_user_id: updatedRecord.id,
|
||||
privilege: grant,
|
||||
db_id: _n(accessedDatabase.dbId),
|
||||
db_slug: accessedDatabase.dbSlug,
|
||||
db_schema_id: _n(accessedDatabase.dbSchemaId),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
import { DSQL_DATASQUIREL_MARIADB_USERS } from "../../../types/dsql";
|
||||
import { DatabaseScopedAccessObject, UserType } from "../../../types";
|
||||
import handleMariadbUserGrantsForDatabasesRecreateRecordsForDatabase from "./handle-mariadb-user-grants-for-databases-recreate-records";
|
||||
import handleMariadbUserGrantsForDatabasesRecreateGrants from "./handle-mariadb-user-grants-for-databases-recreate-grants";
|
||||
|
||||
type Params = {
|
||||
accessedDatabases: DatabaseScopedAccessObject[];
|
||||
user: UserType;
|
||||
updatedRecord: DSQL_DATASQUIREL_MARIADB_USERS;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
msg?: string;
|
||||
success?: boolean;
|
||||
};
|
||||
|
||||
export default async function handleMariadbUserGrantsForDatabases({
|
||||
accessedDatabases,
|
||||
user,
|
||||
updatedRecord,
|
||||
}: Params): Promise<Return> {
|
||||
/**
|
||||
* # Recreate Records
|
||||
*/
|
||||
for (let i = 0; i < accessedDatabases.length; i++) {
|
||||
await handleMariadbUserGrantsForDatabasesRecreateRecordsForDatabase({
|
||||
currentAccessedDatabase: accessedDatabases[i],
|
||||
updatedRecord,
|
||||
user,
|
||||
});
|
||||
|
||||
await handleMariadbUserGrantsForDatabasesRecreateGrants({
|
||||
currentAccessedDatabase: accessedDatabases[i],
|
||||
updatedRecord,
|
||||
user,
|
||||
});
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
import {
|
||||
AddUpdateMariadbUserAPIReqBody,
|
||||
UserSQLPermissions,
|
||||
UserType,
|
||||
} from "../../../types";
|
||||
import {
|
||||
DSQL_DATASQUIREL_MARIADB_USER_PRIVILEGES,
|
||||
DSQL_DATASQUIREL_MARIADB_USERS,
|
||||
DsqlTables,
|
||||
} from "../../../types/dsql";
|
||||
import dsqlCrud from "../../../utils/data-fetching/crud";
|
||||
import grabDbNames from "../../../utils/grab-db-names";
|
||||
import normalizeText from "../../../utils/normalize-text";
|
||||
import dbHandler from "../../backend/dbHandler";
|
||||
import handleMariadbUserGrantsForDatabases from "./handle-mariadb-user-grants-for-databases";
|
||||
import revokeAllExistingGrants from "./revoke-all-existing-grants";
|
||||
|
||||
type Params = AddUpdateMariadbUserAPIReqBody & {
|
||||
user: UserType;
|
||||
updatedRecord: DSQL_DATASQUIREL_MARIADB_USERS;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
msg?: string;
|
||||
success?: boolean;
|
||||
};
|
||||
|
||||
export default async function handleMariadbUserGrants({
|
||||
accessedDatabases,
|
||||
grants,
|
||||
isAllDbsAccess,
|
||||
isAllGrants,
|
||||
user,
|
||||
updatedRecord,
|
||||
}: Params): Promise<Return> {
|
||||
const { userDbPrefix } = grabDbNames({ user });
|
||||
|
||||
/**
|
||||
* # Revoke All Existing Grants
|
||||
*/
|
||||
await revokeAllExistingGrants({ updatedRecord, user });
|
||||
|
||||
/**
|
||||
* # Recreate Grants
|
||||
*/
|
||||
if (isAllGrants && isAllDbsAccess) {
|
||||
const grantAllPrivileges = await dbHandler({
|
||||
query: normalizeText(`
|
||||
GRANT ALL PRIVILEGES ON \
|
||||
\`${userDbPrefix.replace(/\_/g, "\\_")}%\`.* TO \
|
||||
'${updatedRecord.username}'@'${updatedRecord.host}'
|
||||
`),
|
||||
});
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
if (isAllDbsAccess && grants) {
|
||||
const isGrantsInalid = grants.find(
|
||||
(g) => !UserSQLPermissions.includes(g)
|
||||
);
|
||||
|
||||
if (isGrantsInalid) {
|
||||
return { msg: `grants is/are invalid!` };
|
||||
}
|
||||
|
||||
const grantQuery = normalizeText(`
|
||||
GRANT ${grants.join(",")} ON \`${userDbPrefix}%\`.* TO \
|
||||
'${updatedRecord.username}'@'${updatedRecord.host}'
|
||||
`);
|
||||
|
||||
const grantSpecificPrivilegesToAllDbs = await dbHandler({
|
||||
query: grantQuery,
|
||||
});
|
||||
|
||||
for (let t = 0; t < grants.length; t++) {
|
||||
const grant = grants[t];
|
||||
|
||||
const addGrant = await dsqlCrud<
|
||||
DSQL_DATASQUIREL_MARIADB_USER_PRIVILEGES,
|
||||
(typeof DsqlTables)[number]
|
||||
>({
|
||||
action: "insert",
|
||||
table: "mariadb_user_privileges",
|
||||
data: {
|
||||
user_id: user.id,
|
||||
mariadb_user_id: updatedRecord.id,
|
||||
privilege: grant,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
if (accessedDatabases?.[0]) {
|
||||
const res = await handleMariadbUserGrantsForDatabases({
|
||||
accessedDatabases,
|
||||
updatedRecord,
|
||||
user,
|
||||
});
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
import { AddUpdateMariadbUserAPIReqBody, UserType } from "../../../types";
|
||||
import {
|
||||
DSQL_DATASQUIREL_MARIADB_USERS,
|
||||
DsqlTables,
|
||||
} from "../../../types/dsql";
|
||||
import grabSQLUserName from "../../../utils/grab-sql-user-name";
|
||||
import addDbEntry from "../../backend/db/addDbEntry";
|
||||
import encrypt from "../../dsql/encrypt";
|
||||
import dbGrabUserResource from "../db/grab-user-resource";
|
||||
|
||||
type Params = AddUpdateMariadbUserAPIReqBody & {
|
||||
user: UserType;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
existingRecord?: DSQL_DATASQUIREL_MARIADB_USERS | null;
|
||||
updatedRecord?: DSQL_DATASQUIREL_MARIADB_USERS | null;
|
||||
msg?: string;
|
||||
};
|
||||
|
||||
export default async function handleMariadbUserRecord({
|
||||
mariadbUser,
|
||||
accessedDatabases,
|
||||
grants,
|
||||
isAllDbsAccess,
|
||||
isAllGrants,
|
||||
user,
|
||||
}: Params): Promise<Return> {
|
||||
const { name: finalMariadbUserName } = grabSQLUserName({
|
||||
name: mariadbUser.username,
|
||||
user,
|
||||
});
|
||||
|
||||
const finalPassword = mariadbUser.password?.replace(/ /g, "");
|
||||
if (!finalPassword) return { msg: `Couldn't get password` };
|
||||
|
||||
const encryptedFinalPassword = encrypt({ data: finalPassword });
|
||||
const finalHost = mariadbUser.host?.replace(/ /g, "");
|
||||
|
||||
const newMariadbUser: DSQL_DATASQUIREL_MARIADB_USERS = {
|
||||
password: encryptedFinalPassword || undefined,
|
||||
username: finalMariadbUserName,
|
||||
all_databases: isAllDbsAccess ? 1 : 0,
|
||||
all_grants: isAllGrants ? 1 : 0,
|
||||
host: finalHost,
|
||||
user_id: user.id,
|
||||
};
|
||||
|
||||
let { single: existingRecord } =
|
||||
await dbGrabUserResource<DSQL_DATASQUIREL_MARIADB_USERS>({
|
||||
tableName: "mariadb_users",
|
||||
userId: user.id,
|
||||
query: {
|
||||
query: {
|
||||
id: {
|
||||
value: String(mariadbUser.id || 0),
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const record = await addDbEntry<
|
||||
DSQL_DATASQUIREL_MARIADB_USERS,
|
||||
(typeof DsqlTables)[number]
|
||||
>({
|
||||
tableName: "mariadb_users",
|
||||
data: newMariadbUser,
|
||||
update: true,
|
||||
duplicateColumnName: "id",
|
||||
duplicateColumnValue: (existingRecord?.id || 0).toString(),
|
||||
});
|
||||
|
||||
let { single: updatedRecord } =
|
||||
await dbGrabUserResource<DSQL_DATASQUIREL_MARIADB_USERS>({
|
||||
tableName: "mariadb_users",
|
||||
userId: user.id,
|
||||
query: {
|
||||
query: {
|
||||
id: {
|
||||
value: String(
|
||||
existingRecord?.id || record?.payload?.insertId || 0
|
||||
),
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return { existingRecord, updatedRecord };
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import { UserType } from "../../../types";
|
||||
import { DSQL_DATASQUIREL_MARIADB_USERS } from "../../../types/dsql";
|
||||
import grabDbNames from "../../../utils/grab-db-names";
|
||||
import normalizeText from "../../../utils/normalize-text";
|
||||
import dbHandler from "../../backend/dbHandler";
|
||||
import decrypt from "../../dsql/decrypt";
|
||||
import { createNewSQLUser } from "./handle-mariadb-user-creation";
|
||||
|
||||
type Params = {
|
||||
user: UserType;
|
||||
updatedRecord: DSQL_DATASQUIREL_MARIADB_USERS;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
msg?: string;
|
||||
success?: boolean;
|
||||
};
|
||||
|
||||
export default async function revokeAllExistingGrants({
|
||||
user,
|
||||
updatedRecord,
|
||||
}: Params): Promise<Return> {
|
||||
const { userDbPrefix } = grabDbNames({ user });
|
||||
const parsedPassword = decrypt({
|
||||
encryptedString: updatedRecord?.password || "",
|
||||
});
|
||||
|
||||
const revokeAllPrivileges = await dbHandler({
|
||||
query: normalizeText(`
|
||||
REVOKE ALL PRIVILEGES ON *.* FROM '${updatedRecord.username}'@'${updatedRecord.host}'
|
||||
`),
|
||||
});
|
||||
|
||||
if (!revokeAllPrivileges) {
|
||||
await createNewSQLUser({
|
||||
host: updatedRecord.host,
|
||||
password: parsedPassword,
|
||||
username: updatedRecord.username,
|
||||
});
|
||||
}
|
||||
|
||||
const revokeGrantOption = await dbHandler({
|
||||
query: normalizeText(`
|
||||
REVOKE GRANT OPTION ON *.* FROM '${updatedRecord.username}'@'${updatedRecord.host}'
|
||||
`),
|
||||
});
|
||||
|
||||
const userGrants = (await dbHandler({
|
||||
query: `SHOW GRANTS FOR '${updatedRecord.username}'@'${updatedRecord.host}'`,
|
||||
})) as any[];
|
||||
|
||||
for (let i = 0; i < userGrants.length; i++) {
|
||||
const grantObject = userGrants[i];
|
||||
const grant = grantObject?.[Object.keys(grantObject)[0]];
|
||||
|
||||
if (!grant?.match(/GRANT USAGE .* IDENTIFIED BY PASSWORD/)) {
|
||||
const revokeGrantText = grant
|
||||
.replace(/GRANT/, "REVOKE")
|
||||
.replace(/ TO /, " FROM ");
|
||||
|
||||
const revokePrivilege = await dbHandler({ query: revokeGrantText });
|
||||
}
|
||||
}
|
||||
|
||||
const flushPrivileges = await dbHandler({
|
||||
query: `FLUSH PRIVILEGES`,
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
}
|
||||
@@ -3,10 +3,12 @@ import { DSQL_DatabaseSchemaType, PostInsertReturn } from "../../types";
|
||||
import { DSQL_DATASQUIREL_USER_DATABASES } from "../../types/dsql";
|
||||
import numberfy from "../../utils/numberfy";
|
||||
import addDbEntry from "../../functions/backend/db/addDbEntry";
|
||||
import updateDbEntry from "../../functions/backend/db/updateDbEntry";
|
||||
|
||||
type Param = {
|
||||
userId?: number | string | null;
|
||||
dbSchema: DSQL_DatabaseSchemaType;
|
||||
isMain?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -16,7 +18,10 @@ type Param = {
|
||||
export default async function checkDbRecordCreateDbSchema({
|
||||
userId,
|
||||
dbSchema,
|
||||
isMain,
|
||||
}: Param): Promise<DSQL_DATASQUIREL_USER_DATABASES | undefined> {
|
||||
if (isMain) return undefined;
|
||||
|
||||
try {
|
||||
const {
|
||||
dbFullName,
|
||||
@@ -25,7 +30,8 @@ export default async function checkDbRecordCreateDbSchema({
|
||||
dbDescription,
|
||||
dbImage,
|
||||
childDatabase,
|
||||
childDatabaseDbFullName,
|
||||
childDatabaseDbId,
|
||||
id,
|
||||
} = dbSchema;
|
||||
|
||||
let recordedDbEntryArray = userId
|
||||
@@ -38,31 +44,41 @@ export default async function checkDbRecordCreateDbSchema({
|
||||
let recordedDbEntry: DSQL_DATASQUIREL_USER_DATABASES | undefined =
|
||||
recordedDbEntryArray?.[0];
|
||||
|
||||
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,
|
||||
db_schema_id: numberfy(id),
|
||||
active_clone_parent_db_id: numberfy(childDatabaseDbId),
|
||||
};
|
||||
|
||||
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<DSQL_DATASQUIREL_USER_DATABASES>({
|
||||
data: newDbEntryObj,
|
||||
tableName: "user_databases",
|
||||
forceLocal: true,
|
||||
});
|
||||
|
||||
const newDbEntry = (await addDbEntry({
|
||||
data: newDbEntryObj,
|
||||
tableName: "user_databases",
|
||||
forceLocal: true,
|
||||
})) as PostInsertReturn;
|
||||
|
||||
if (newDbEntry.insertId) {
|
||||
if (newDbEntry.payload?.insertId) {
|
||||
recordedDbEntryArray = await varDatabaseDbHandler({
|
||||
queryString: `SELECT * FROM datasquirel.user_databases WHERE db_full_name = ?`,
|
||||
queryValuesArray: [dbFullName || "NULL"],
|
||||
});
|
||||
recordedDbEntry = recordedDbEntryArray?.[0];
|
||||
}
|
||||
} else if (recordedDbEntry?.id) {
|
||||
await updateDbEntry<DSQL_DATASQUIREL_USER_DATABASES>({
|
||||
data: newDbEntryObj,
|
||||
tableName: "user_databases",
|
||||
forceLocal: true,
|
||||
identifierColumnName: "id",
|
||||
identifierValue: String(recordedDbEntry.id),
|
||||
});
|
||||
}
|
||||
|
||||
return recordedDbEntry;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import varDatabaseDbHandler from "../utils/varDatabaseDbHandler";
|
||||
import dbHandler from "../utils/dbHandler";
|
||||
import {
|
||||
DSQL_DatabaseSchemaType,
|
||||
DSQL_TableSchemaType,
|
||||
@@ -71,34 +70,34 @@ export default async function checkTableRecordCreateDbSchema({
|
||||
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) {
|
||||
if (tableSchema?.childTable && tableSchema.childTableId) {
|
||||
const parentDb = dbSchema.find(
|
||||
(db) => db.dbFullName == tableSchema.childTableDbFullName
|
||||
(db) => db.id == tableSchema.childTableDbId
|
||||
);
|
||||
const parentDbTable = parentDb?.tables.find(
|
||||
(tbl) => tbl.tableName == tableSchema.childTableName
|
||||
(tbl) => tbl.id == tableSchema.childTableId
|
||||
);
|
||||
if (parentDb && parentDbTable) {
|
||||
newTableInsertObject["child_table"] = 1;
|
||||
newTableInsertObject["child_table_parent_database"] =
|
||||
parentDb.dbFullName;
|
||||
newTableInsertObject["child_table_parent_table"] =
|
||||
parentDbTable.tableName;
|
||||
newTableInsertObject[
|
||||
"child_table_parent_database_schema_id"
|
||||
] = numberfy(parentDb.id);
|
||||
newTableInsertObject["child_table_parent_table_schema_id"] =
|
||||
numberfy(parentDbTable.id);
|
||||
}
|
||||
}
|
||||
|
||||
const newTableRecordEntry = (await addDbEntry({
|
||||
const newTableRecordEntry = await addDbEntry({
|
||||
data: newTableInsertObject,
|
||||
tableName: "user_database_tables",
|
||||
dbContext: "Master",
|
||||
forceLocal: true,
|
||||
})) as PostInsertReturn;
|
||||
});
|
||||
|
||||
if (newTableRecordEntry.insertId) {
|
||||
if (newTableRecordEntry.payload?.insertId) {
|
||||
recordedTableEntryArray = await varDatabaseDbHandler({
|
||||
queryString: queryObj?.string || "",
|
||||
queryValuesArray: queryObj?.values,
|
||||
|
||||
@@ -0,0 +1,317 @@
|
||||
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;
|
||||
dbId?: string | number;
|
||||
dbSlug?: string;
|
||||
};
|
||||
|
||||
export default function grabRequiredDatabaseSchemas(
|
||||
params: Params
|
||||
): DSQL_DatabaseSchemaType[] | undefined {
|
||||
const primaryDbSchema = grabPrimaryRequiredDbSchema(params);
|
||||
if (!primaryDbSchema) return undefined;
|
||||
|
||||
let relatedDatabases: DSQL_DatabaseSchemaType[] = [];
|
||||
|
||||
const childrenDatabases = primaryDbSchema.childrenDatabases || [];
|
||||
const childrenTables =
|
||||
primaryDbSchema.tables
|
||||
.map((tbl) => {
|
||||
return tbl.childrenTables || [];
|
||||
})
|
||||
.flat() || [];
|
||||
|
||||
for (let i = 0; i < childrenDatabases.length; i++) {
|
||||
const childDb = childrenDatabases[i];
|
||||
const childDbSchema = grabPrimaryRequiredDbSchema({
|
||||
userId: params.userId,
|
||||
dbId: childDb.dbId,
|
||||
});
|
||||
if (!childDbSchema?.dbSlug) continue;
|
||||
relatedDatabases.push(childDbSchema);
|
||||
}
|
||||
|
||||
for (let i = 0; i < childrenTables.length; i++) {
|
||||
const childTbl = childrenTables[i];
|
||||
const childTableDbSchema = grabPrimaryRequiredDbSchema({
|
||||
userId: params.userId,
|
||||
dbId: childTbl.dbId,
|
||||
});
|
||||
if (!childTableDbSchema?.dbSlug) continue;
|
||||
relatedDatabases.push(childTableDbSchema);
|
||||
}
|
||||
|
||||
return uniqueByKey([primaryDbSchema, ...relatedDatabases], "dbFullName");
|
||||
}
|
||||
|
||||
export function grabPrimaryRequiredDbSchema({ userId, dbId, dbSlug }: Params) {
|
||||
let finalDbId = dbId;
|
||||
|
||||
if (!finalDbId && userId && dbSlug) {
|
||||
const searchedDb = findDbNameInSchemaDir({ dbName: dbSlug, userId });
|
||||
|
||||
if (searchedDb?.id) {
|
||||
finalDbId = searchedDb.id;
|
||||
}
|
||||
}
|
||||
|
||||
if (!finalDbId) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const { targetUserPrivateDir, oldSchemasDir } = grabDirNames({
|
||||
userId,
|
||||
});
|
||||
|
||||
const finalSchemaDir = targetUserPrivateDir || oldSchemasDir;
|
||||
|
||||
if (!finalSchemaDir) {
|
||||
console.log(`finalSchemaDir not found!`);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (finalDbId) {
|
||||
const dbIdSchema = path.resolve(finalSchemaDir, `${finalDbId}.json`);
|
||||
if (fs.existsSync(dbIdSchema)) {
|
||||
const dbIdSchemaObject = EJSON.parse(
|
||||
fs.readFileSync(dbIdSchema, "utf-8")
|
||||
) as DSQL_DatabaseSchemaType | undefined;
|
||||
return dbIdSchemaObject;
|
||||
}
|
||||
}
|
||||
|
||||
const dbSchemasFiles = fs.readdirSync(finalSchemaDir);
|
||||
|
||||
let targetDbSchema: DSQL_DatabaseSchemaType | undefined;
|
||||
|
||||
try {
|
||||
for (let i = 0; i < dbSchemasFiles.length; i++) {
|
||||
const fileOrPath = dbSchemasFiles[i];
|
||||
if (!fileOrPath.endsWith(`.json`)) continue;
|
||||
if (!fileOrPath.match(/^\d+.json/)) continue;
|
||||
|
||||
const targetFileJSONPath = path.join(finalSchemaDir, fileOrPath);
|
||||
const targetSchema = EJSON.parse(
|
||||
fs.readFileSync(targetFileJSONPath, "utf-8")
|
||||
) as DSQL_DatabaseSchemaType | undefined;
|
||||
|
||||
if (targetSchema && finalDbId && targetSchema?.id == finalDbId) {
|
||||
targetDbSchema = targetSchema;
|
||||
}
|
||||
}
|
||||
} catch (error) {}
|
||||
|
||||
if (targetDbSchema) {
|
||||
return targetDbSchema;
|
||||
}
|
||||
// else if ( dbFullName) {
|
||||
// let existingSchemaInMainJSON = findTargetDbSchemaFromMainSchema(
|
||||
// dbFullName
|
||||
// );
|
||||
|
||||
// const nextID = grabLatestDbSchemaID(finalSchemaDir);
|
||||
|
||||
// if (existingSchemaInMainJSON) {
|
||||
// existingSchemaInMainJSON.id = nextID;
|
||||
// fs.writeFileSync(
|
||||
// path.join(finalSchemaDir, `${nextID}.json`),
|
||||
// EJSON.stringify(existingSchemaInMainJSON) || "[]"
|
||||
// );
|
||||
// return existingSchemaInMainJSON;
|
||||
// }
|
||||
// }
|
||||
|
||||
console.log(`userSchemaDir not found!`);
|
||||
console.log(`userId`, userId);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function findDbNameInSchemaDir({
|
||||
userId,
|
||||
dbName,
|
||||
}: {
|
||||
userId?: string | number;
|
||||
dbName?: string;
|
||||
}) {
|
||||
if (!userId) {
|
||||
console.log(`userId not provided!`);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (!dbName) {
|
||||
console.log(`dbName not provided!`);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const { targetUserPrivateDir } = grabDirNames({
|
||||
userId,
|
||||
});
|
||||
|
||||
if (!targetUserPrivateDir) {
|
||||
console.log(`targetUserPrivateDir not found!`);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const dbSchemasFiles = fs.readdirSync(targetUserPrivateDir);
|
||||
|
||||
let targetDbSchema: DSQL_DatabaseSchemaType | undefined;
|
||||
|
||||
try {
|
||||
for (let i = 0; i < dbSchemasFiles.length; i++) {
|
||||
const fileOrPath = dbSchemasFiles[i];
|
||||
if (!fileOrPath.endsWith(`.json`)) continue;
|
||||
if (!fileOrPath.match(/^\d+.json/)) continue;
|
||||
|
||||
const targetFileJSONPath = path.join(
|
||||
targetUserPrivateDir,
|
||||
fileOrPath
|
||||
);
|
||||
const targetSchema = EJSON.parse(
|
||||
fs.readFileSync(targetFileJSONPath, "utf-8")
|
||||
) as DSQL_DatabaseSchemaType | undefined;
|
||||
|
||||
if (!targetSchema) continue;
|
||||
|
||||
if (
|
||||
targetSchema.dbFullName == dbName ||
|
||||
targetSchema.dbSlug == dbName
|
||||
) {
|
||||
targetDbSchema = targetSchema;
|
||||
return targetSchema;
|
||||
}
|
||||
}
|
||||
} catch (error) {}
|
||||
|
||||
return targetDbSchema;
|
||||
}
|
||||
|
||||
type UpdateDbSchemaParam = {
|
||||
dbSchema: DSQL_DatabaseSchemaType;
|
||||
userId?: string | number | null;
|
||||
};
|
||||
|
||||
export function writeUpdatedDbSchema({
|
||||
dbSchema,
|
||||
userId,
|
||||
}: UpdateDbSchemaParam): { success?: boolean; dbSchemaId?: string | number } {
|
||||
const { targetUserPrivateDir } = grabDirNames({
|
||||
userId,
|
||||
});
|
||||
|
||||
if (!targetUserPrivateDir) {
|
||||
console.log(`user ${userId} has no targetUserPrivateDir`);
|
||||
return {};
|
||||
}
|
||||
|
||||
if (dbSchema.id) {
|
||||
const dbIdSchemaPath = path.join(
|
||||
targetUserPrivateDir,
|
||||
`${dbSchema.id}.json`
|
||||
);
|
||||
|
||||
fs.writeFileSync(dbIdSchemaPath, EJSON.stringify(dbSchema) || "[]");
|
||||
|
||||
return { success: true };
|
||||
} else {
|
||||
const nextID = grabLatestDbSchemaID(targetUserPrivateDir);
|
||||
|
||||
dbSchema.id = nextID;
|
||||
|
||||
fs.writeFileSync(
|
||||
path.join(targetUserPrivateDir, `${nextID}.json`),
|
||||
EJSON.stringify(dbSchema) || "[]"
|
||||
);
|
||||
|
||||
return { success: true, dbSchemaId: nextID };
|
||||
}
|
||||
}
|
||||
|
||||
export function deleteDbSchema({ dbSchema, userId }: UpdateDbSchemaParam) {
|
||||
const { targetUserPrivateDir, userSchemaMainJSONFilePath } = grabDirNames({
|
||||
userId,
|
||||
});
|
||||
|
||||
if (!targetUserPrivateDir) return;
|
||||
|
||||
const targetDbSchema = grabPrimaryRequiredDbSchema({
|
||||
dbId: dbSchema.id,
|
||||
userId,
|
||||
});
|
||||
|
||||
const schemaFile = path.join(
|
||||
targetUserPrivateDir,
|
||||
`${targetDbSchema?.id}.json`
|
||||
);
|
||||
|
||||
try {
|
||||
fs.unlinkSync(schemaFile);
|
||||
} catch (error) {}
|
||||
|
||||
if (
|
||||
userSchemaMainJSONFilePath &&
|
||||
fs.existsSync(userSchemaMainJSONFilePath)
|
||||
) {
|
||||
try {
|
||||
let allDbSchemas = EJSON.parse(
|
||||
fs.readFileSync(userSchemaMainJSONFilePath, "utf-8")
|
||||
) as DSQL_DatabaseSchemaType[] | undefined;
|
||||
|
||||
if (allDbSchemas?.[0]) {
|
||||
for (let i = 0; i < allDbSchemas.length; i++) {
|
||||
const dbSch = allDbSchemas[i];
|
||||
if (
|
||||
dbSch.dbFullName == dbSchema.dbFullName ||
|
||||
dbSch.id == dbSchema.id
|
||||
) {
|
||||
allDbSchemas.splice(i, 1);
|
||||
}
|
||||
}
|
||||
|
||||
fs.writeFileSync(
|
||||
userSchemaMainJSONFilePath,
|
||||
EJSON.stringify(allDbSchemas) || "[]"
|
||||
);
|
||||
}
|
||||
} catch (error) {}
|
||||
}
|
||||
}
|
||||
|
||||
export function findTargetDbSchemaFromMainSchema(
|
||||
schemas: DSQL_DatabaseSchemaType[],
|
||||
dbFullName?: string,
|
||||
dbId?: string | number
|
||||
): DSQL_DatabaseSchemaType | undefined {
|
||||
const targetDbSchema = schemas.find(
|
||||
(sch) => sch.dbFullName == dbFullName || (dbId && sch.id == dbId)
|
||||
);
|
||||
return targetDbSchema;
|
||||
}
|
||||
|
||||
export function grabLatestDbSchemaID(userSchemaDir: string) {
|
||||
const dbSchemasFiles = fs.readdirSync(userSchemaDir);
|
||||
const dbNumbers = dbSchemasFiles
|
||||
.filter((dbSch) => {
|
||||
if (!dbSch.endsWith(`.json`)) return false;
|
||||
if (dbSch.match(/^\d+\.json/)) return true;
|
||||
return false;
|
||||
})
|
||||
.map((dbSch) => numberfy(dbSch.replace(/[^0-9]/g, "")));
|
||||
|
||||
if (dbNumbers[0])
|
||||
return (
|
||||
(dbNumbers
|
||||
.sort((a, b) => {
|
||||
return a - b;
|
||||
})
|
||||
.pop() || 0) + 1
|
||||
);
|
||||
return 1;
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
import varDatabaseDbHandler from "../utils/varDatabaseDbHandler";
|
||||
import { DSQL_IndexSchemaType } from "../../types";
|
||||
import {
|
||||
DSQL_IndexSchemaType,
|
||||
DSQL_MYSQL_SHOW_INDEXES_Type,
|
||||
} from "../../types";
|
||||
import grabDSQLSchemaIndexComment from "../utils/grab-dsql-schema-index-comment";
|
||||
|
||||
type Param = {
|
||||
tableName: string;
|
||||
@@ -18,6 +22,11 @@ export default async function handleIndexescreateDbFromSchema({
|
||||
tableName,
|
||||
indexes,
|
||||
}: Param) {
|
||||
const allExistingIndexes: DSQL_MYSQL_SHOW_INDEXES_Type[] =
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\``,
|
||||
});
|
||||
|
||||
for (let g = 0; g < indexes.length; g++) {
|
||||
const { indexType, indexName, indexTableFields, alias } = indexes[g];
|
||||
|
||||
@@ -27,38 +36,32 @@ export default async function handleIndexescreateDbFromSchema({
|
||||
* @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) {
|
||||
global.ERROR_CALLBACK?.(
|
||||
`Error Handling Indexes on Creating Schema`,
|
||||
error as 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'`,
|
||||
});
|
||||
const queryString = `CREATE${
|
||||
indexType == "full_text" ? " FULLTEXT" : ""
|
||||
} INDEX \`${alias}\` ON \`${dbFullName}\`.\`${tableName}\`(${indexTableFields
|
||||
?.map((nm) => nm.value)
|
||||
.map((nm) => `\`${nm}\``)
|
||||
.join(
|
||||
","
|
||||
)}) COMMENT '${grabDSQLSchemaIndexComment()} ${indexName}'`;
|
||||
|
||||
const addIndex = await varDatabaseDbHandler({ queryString });
|
||||
}
|
||||
}
|
||||
|
||||
const allExistingIndexesAfterUpdate: DSQL_MYSQL_SHOW_INDEXES_Type[] =
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\``,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,21 +1,22 @@
|
||||
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 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";
|
||||
import grabRequiredDatabaseSchemas, {
|
||||
grabPrimaryRequiredDbSchema,
|
||||
} from "./grab-required-database-schemas";
|
||||
import dbHandler from "../../functions/backend/dbHandler";
|
||||
|
||||
type Param = {
|
||||
userId?: number | string | null;
|
||||
targetDatabase?: string;
|
||||
dbSchemaData?: import("../../types").DSQL_DatabaseSchemaType[];
|
||||
dbSchemaData?: DSQL_DatabaseSchemaType[];
|
||||
targetTable?: string;
|
||||
dbId?: string | number;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -26,84 +27,79 @@ export default async function createDbFromSchema({
|
||||
userId,
|
||||
targetDatabase,
|
||||
dbSchemaData,
|
||||
targetTable,
|
||||
dbId,
|
||||
}: Param): Promise<boolean> {
|
||||
const { userSchemaMainJSONFilePath, mainShemaJSONFilePath } = grabDirNames({
|
||||
userId,
|
||||
});
|
||||
|
||||
const schemaPath = userSchemaMainJSONFilePath || mainShemaJSONFilePath;
|
||||
|
||||
const dbSchema: DSQL_DatabaseSchemaType[] | undefined =
|
||||
dbSchemaData ||
|
||||
(EJSON.parse(fs.readFileSync(schemaPath, "utf8")) as
|
||||
| DSQL_DatabaseSchemaType[]
|
||||
| undefined);
|
||||
|
||||
if (!dbSchema) {
|
||||
console.log("Schema Not Found!");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (let i = 0; i < dbSchema.length; i++) {
|
||||
const database: DSQL_DatabaseSchemaType = dbSchema[i];
|
||||
|
||||
const { dbFullName, tables, dbSlug, childrenDatabases } = database;
|
||||
|
||||
if (!dbFullName) continue;
|
||||
|
||||
if (targetDatabase && dbFullName != targetDatabase) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const dbCheck: any = await noDatabaseDbHandler(
|
||||
`SELECT SCHEMA_NAME AS dbFullName FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '${dbFullName}'`
|
||||
);
|
||||
|
||||
if (!dbCheck?.[0]?.dbFullName) {
|
||||
const newDatabase = await noDatabaseDbHandler(
|
||||
`CREATE DATABASE IF NOT EXISTS \`${dbFullName}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`
|
||||
);
|
||||
}
|
||||
|
||||
const allTables: any = await noDatabaseDbHandler(
|
||||
`SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='${dbFullName}'`
|
||||
);
|
||||
|
||||
let recordedDbEntry = await checkDbRecordCreateDbSchema({
|
||||
dbSchema: database,
|
||||
try {
|
||||
const { userSchemaMainJSONFilePath } = grabDirNames({
|
||||
userId,
|
||||
});
|
||||
|
||||
for (let tb = 0; tb < allTables.length; tb++) {
|
||||
const { TABLE_NAME } = allTables[tb];
|
||||
let dbSchema = dbSchemaData
|
||||
? dbSchemaData
|
||||
: dbId
|
||||
? grabRequiredDatabaseSchemas({
|
||||
dbId,
|
||||
userId,
|
||||
})
|
||||
: undefined;
|
||||
|
||||
const targetTableSchema = tables.find(
|
||||
(_table) => _table.tableName === TABLE_NAME
|
||||
if (!dbSchema) {
|
||||
console.log("Schema Not Found!");
|
||||
return false;
|
||||
}
|
||||
|
||||
const isMain = !userSchemaMainJSONFilePath;
|
||||
|
||||
for (let i = 0; i < dbSchema.length; i++) {
|
||||
const database: DSQL_DatabaseSchemaType = dbSchema[i];
|
||||
|
||||
const { dbFullName, tables, dbSlug, childrenDatabases } = database;
|
||||
|
||||
if (!dbFullName) continue;
|
||||
|
||||
if (targetDatabase && dbFullName != targetDatabase) {
|
||||
continue;
|
||||
}
|
||||
|
||||
console.log(`Handling database => ${dbFullName}`);
|
||||
|
||||
const dbCheck: any = await noDatabaseDbHandler(
|
||||
`SELECT SCHEMA_NAME AS dbFullName FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '${dbFullName}'`
|
||||
);
|
||||
|
||||
/**
|
||||
* @description Check if TABLE_NAME is part of the tables contained
|
||||
* in the user schema JSON. If it's not, the table is either deleted
|
||||
* or the table name has been recently changed
|
||||
*/
|
||||
if (!targetTableSchema) {
|
||||
const oldTable = tables.find(
|
||||
(_table) =>
|
||||
_table.tableNameOld &&
|
||||
_table.tableNameOld === TABLE_NAME
|
||||
if (!dbCheck?.[0]?.dbFullName) {
|
||||
const newDatabase = await noDatabaseDbHandler(
|
||||
`CREATE DATABASE IF NOT EXISTS \`${dbFullName}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`
|
||||
);
|
||||
}
|
||||
|
||||
const allTables: any = await noDatabaseDbHandler(
|
||||
`SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='${dbFullName}'`
|
||||
);
|
||||
|
||||
let recordedDbEntry = await checkDbRecordCreateDbSchema({
|
||||
dbSchema: database,
|
||||
userId,
|
||||
isMain,
|
||||
});
|
||||
|
||||
for (let tb = 0; tb < allTables.length; tb++) {
|
||||
const { TABLE_NAME } = allTables[tb];
|
||||
|
||||
const targetTableSchema = tables.find(
|
||||
(_table) => _table.tableName === TABLE_NAME
|
||||
);
|
||||
|
||||
/**
|
||||
* @description Check if this table has been recently renamed. Rename
|
||||
* table id true. Drop table if false
|
||||
* @description Check if TABLE_NAME is part of the tables contained
|
||||
* in the user schema JSON. If it's not, the table is either deleted
|
||||
* or the table name has been recently changed
|
||||
*/
|
||||
if (oldTable) {
|
||||
console.log("Renaming Table");
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `RENAME TABLE \`${dbFullName}\`.\`${oldTable.tableNameOld}\` TO \`${oldTable.tableName}\``,
|
||||
});
|
||||
} else {
|
||||
console.log(`Dropping Table from ${dbFullName}`);
|
||||
if (!targetTableSchema) {
|
||||
console.log(
|
||||
`Dropping Table ${TABLE_NAME} from ${dbFullName}`
|
||||
);
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `DROP TABLE \`${dbFullName}\`.\`${TABLE_NAME}\``,
|
||||
});
|
||||
@@ -112,130 +108,175 @@ export default async function createDbFromSchema({
|
||||
query: `DELETE FROM datasquirel.user_database_tables WHERE user_id = ? AND db_slug = ? AND table_slug = ?`,
|
||||
values: [userId, dbSlug, TABLE_NAME],
|
||||
});
|
||||
|
||||
// const oldTable = tables.find(
|
||||
// (_table) =>
|
||||
// _table.tableNameOld &&
|
||||
// _table.tableNameOld === TABLE_NAME
|
||||
// );
|
||||
|
||||
// /**
|
||||
// * @description Check if this table has been recently renamed. Rename
|
||||
// * table id true. Drop table if false
|
||||
// */
|
||||
// if (oldTable) {
|
||||
// console.log("Renaming Table");
|
||||
// await varDatabaseDbHandler({
|
||||
// queryString: `RENAME TABLE \`${dbFullName}\`.\`${oldTable.tableNameOld}\` TO \`${oldTable.tableName}\``,
|
||||
// });
|
||||
// } else {
|
||||
// console.log(
|
||||
// `Dropping Table ${TABLE_NAME} from ${dbFullName}`
|
||||
// );
|
||||
// await varDatabaseDbHandler({
|
||||
// queryString: `DROP TABLE \`${dbFullName}\`.\`${TABLE_NAME}\``,
|
||||
// });
|
||||
|
||||
// const deleteTableEntry = await dbHandler({
|
||||
// query: `DELETE FROM datasquirel.user_database_tables WHERE user_id = ? AND db_slug = ? AND table_slug = ?`,
|
||||
// values: [userId, dbSlug, TABLE_NAME],
|
||||
// });
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Iterate through each table and perform table actions
|
||||
*/
|
||||
for (let t = 0; t < tables.length; t++) {
|
||||
const table = tables[t];
|
||||
|
||||
const { tableName, fields, indexes } = table;
|
||||
|
||||
/**
|
||||
* @description Check if table exists
|
||||
* @type {any}
|
||||
* @description Iterate through each table and perform table actions
|
||||
*/
|
||||
const tableCheck: any = await varDatabaseDbHandler({
|
||||
queryString: `
|
||||
SELECT EXISTS (
|
||||
SELECT
|
||||
TABLE_NAME
|
||||
FROM
|
||||
information_schema.TABLES
|
||||
WHERE
|
||||
TABLE_SCHEMA = ? AND
|
||||
TABLE_NAME = ?
|
||||
) AS tableExists`,
|
||||
queryValuesArray: [dbFullName, table.tableName],
|
||||
});
|
||||
for (let t = 0; t < tables.length; t++) {
|
||||
const table = tables[t];
|
||||
|
||||
////////////////////////////////////////
|
||||
const { tableName, fields, indexes } = table;
|
||||
|
||||
if (targetTable && tableName !== targetTable) continue;
|
||||
|
||||
console.log(`Handling table => ${tableName}`);
|
||||
|
||||
if (tableCheck && tableCheck[0]?.tableExists > 0) {
|
||||
/**
|
||||
* @description Update table if table exists
|
||||
* @description Check if table exists
|
||||
* @type {any}
|
||||
*/
|
||||
const updateExistingTable = await updateTable({
|
||||
dbFullName: dbFullName,
|
||||
tableName: tableName,
|
||||
tableNameFull: table.tableFullName,
|
||||
tableInfoArray: fields,
|
||||
userId,
|
||||
dbSchema,
|
||||
tableIndexes: indexes,
|
||||
tableIndex: t,
|
||||
childDb: database.childDatabase || undefined,
|
||||
recordedDbEntry,
|
||||
tableSchema: table,
|
||||
const tableCheck: any = await varDatabaseDbHandler({
|
||||
queryString: `
|
||||
SELECT EXISTS (
|
||||
SELECT
|
||||
TABLE_NAME
|
||||
FROM
|
||||
information_schema.TABLES
|
||||
WHERE
|
||||
TABLE_SCHEMA = ? AND
|
||||
TABLE_NAME = ?
|
||||
) AS tableExists`,
|
||||
queryValuesArray: [dbFullName, table.tableName],
|
||||
});
|
||||
|
||||
if (table.childrenTables && table.childrenTables[0]) {
|
||||
for (let ch = 0; ch < table.childrenTables.length; ch++) {
|
||||
const childTable = table.childrenTables[ch];
|
||||
if (tableCheck && tableCheck[0]?.tableExists > 0) {
|
||||
/**
|
||||
* @description Update table if table exists
|
||||
*/
|
||||
const updateExistingTable = await updateTable({
|
||||
dbFullName: dbFullName,
|
||||
tableName: tableName,
|
||||
tableFields: fields,
|
||||
userId,
|
||||
dbSchema: database,
|
||||
tableIndexes: indexes,
|
||||
recordedDbEntry,
|
||||
tableSchema: table,
|
||||
isMain,
|
||||
});
|
||||
|
||||
const updateExistingChildTable = await updateTable({
|
||||
dbFullName: childTable.dbNameFull,
|
||||
tableName: childTable.tableName,
|
||||
tableNameFull: childTable.tableNameFull,
|
||||
tableInfoArray: fields,
|
||||
userId,
|
||||
dbSchema,
|
||||
tableIndexes: indexes,
|
||||
clone: true,
|
||||
childDb: database.childDatabase || undefined,
|
||||
recordedDbEntry,
|
||||
tableSchema: table,
|
||||
if (table.childrenTables && table.childrenTables[0]) {
|
||||
for (
|
||||
let ch = 0;
|
||||
ch < table.childrenTables.length;
|
||||
ch++
|
||||
) {
|
||||
const childTable = table.childrenTables[ch];
|
||||
|
||||
const childTableParentDbSchema =
|
||||
grabPrimaryRequiredDbSchema({
|
||||
dbId: childTable.dbId,
|
||||
userId,
|
||||
});
|
||||
|
||||
if (!childTableParentDbSchema?.dbFullName) continue;
|
||||
|
||||
const childTableSchema =
|
||||
childTableParentDbSchema.tables.find(
|
||||
(tbl) => tbl.id == childTable.tableId
|
||||
);
|
||||
|
||||
if (!childTableSchema) continue;
|
||||
|
||||
const updateExistingChildTable = await updateTable({
|
||||
dbFullName: childTableParentDbSchema.dbFullName,
|
||||
tableName: childTableSchema.tableName,
|
||||
tableFields: childTableSchema.fields,
|
||||
userId,
|
||||
dbSchema: childTableParentDbSchema,
|
||||
tableIndexes: childTableSchema.indexes,
|
||||
clone: true,
|
||||
recordedDbEntry,
|
||||
tableSchema: table,
|
||||
isMain,
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/**
|
||||
* @description Create new Table if table doesnt exist
|
||||
*/
|
||||
const createNewTable = await createTable({
|
||||
tableName: tableName,
|
||||
tableInfoArray: fields,
|
||||
dbFullName: dbFullName,
|
||||
tableSchema: table,
|
||||
recordedDbEntry,
|
||||
isMain,
|
||||
});
|
||||
|
||||
/**
|
||||
* Handle DATASQUIREL Table Indexes
|
||||
* ===================================================
|
||||
* @description Iterate through each datasquirel schema
|
||||
* table index(if available), and perform operations
|
||||
*/
|
||||
if (indexes?.[0]) {
|
||||
handleIndexescreateDbFromSchema({
|
||||
dbFullName,
|
||||
indexes,
|
||||
tableName,
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/**
|
||||
* @description Create new Table if table doesnt exist
|
||||
*/
|
||||
const createNewTable = await createTable({
|
||||
tableName: tableName,
|
||||
tableInfoArray: fields,
|
||||
dbFullName: dbFullName,
|
||||
tableSchema: table,
|
||||
recordedDbEntry,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle DATASQUIREL Table Indexes
|
||||
* ===================================================
|
||||
* @description Iterate through each datasquirel schema
|
||||
* table index(if available), and perform operations
|
||||
*/
|
||||
if (indexes?.[0]) {
|
||||
handleIndexescreateDbFromSchema({
|
||||
dbFullName,
|
||||
indexes,
|
||||
tableName,
|
||||
});
|
||||
/**
|
||||
* @description Check all children databases
|
||||
*/
|
||||
if (childrenDatabases?.[0]) {
|
||||
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?.id) {
|
||||
await createDbFromSchema({
|
||||
userId,
|
||||
dbId: targetDatabase?.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const tableRecord = await checkTableRecordCreateDbSchema({
|
||||
dbFullName,
|
||||
dbSchema,
|
||||
tableSchema: table,
|
||||
dbRecord: recordedDbEntry,
|
||||
userId,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Check all children databases
|
||||
*/
|
||||
if (childrenDatabases?.[0]) {
|
||||
for (let ch = 0; ch < childrenDatabases.length; ch++) {
|
||||
const childDb = childrenDatabases[ch];
|
||||
const { dbId } = childDb;
|
||||
|
||||
const targetDatabase = dbSchema.find(
|
||||
(dbSch) => dbSch.id == dbId
|
||||
);
|
||||
|
||||
await createDbFromSchema({
|
||||
userId,
|
||||
targetDatabase: targetDatabase?.dbFullName,
|
||||
});
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (error: any) {
|
||||
console.log(`createDbFromSchema ERROR => ${error.message}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
import { AppNames } from "../dict/app-names";
|
||||
import serverError from "../functions/backend/serverError";
|
||||
import noDatabaseDbHandler from "./utils/noDatabaseDbHandler";
|
||||
|
||||
@@ -16,26 +17,17 @@ async function grantFullPrivileges({ userId }: { userId: string | null }) {
|
||||
|
||||
const allDatabases = await noDatabaseDbHandler(`SHOW DATABASES`);
|
||||
|
||||
const datasquirelUserDatabases = allDatabases.filter(
|
||||
(/** @type {any} */ database: any) =>
|
||||
database.Database.match(/datasquirel_user_/)
|
||||
const datasquirelUserDatabases = allDatabases.filter((database: any) =>
|
||||
database.Database.match(new RegExp(`^${AppNames["DsqlDbPrefix"]}`))
|
||||
);
|
||||
|
||||
for (let i = 0; i < datasquirelUserDatabases.length; i++) {
|
||||
const datasquirelUserDatabase = datasquirelUserDatabases[i];
|
||||
const { Database } = datasquirelUserDatabase;
|
||||
|
||||
// const grantDbPriviledges = await noDatabaseDbHandler(
|
||||
// `GRANT ALL PRIVILEGES ON ${Database}.* TO '${process.env.DSQL_DB_FULL_ACCESS_USERNAME}'@'%' WITH GRANT OPTION`
|
||||
// );
|
||||
|
||||
// const grantRead = await noDatabaseDbHandler(
|
||||
// `GRANT SELECT ON ${Database}.* TO '${process.env.DSQL_DB_READ_ONLY_USERNAME}'@'%'`
|
||||
// );
|
||||
}
|
||||
|
||||
const flushPriviledged = await noDatabaseDbHandler(`FLUSH PRIVILEGES`);
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
} catch (error: any) {
|
||||
serverError({
|
||||
component: "shell/grantDbPriviledges/main-catch-error",
|
||||
message: error.message,
|
||||
|
||||
@@ -1,137 +0,0 @@
|
||||
import generator from "generate-password";
|
||||
import noDatabaseDbHandler from "../utils/noDatabaseDbHandler";
|
||||
import dbHandler from "../utils/dbHandler";
|
||||
import handleGrants from "./handleGrants";
|
||||
import encrypt from "../../functions/dsql/encrypt";
|
||||
import decrypt from "../../functions/dsql/decrypt";
|
||||
import { DSQL_DATASQUIREL_MARIADB_USERS } from "../../types/dsql";
|
||||
import { MariaDBUser } from "../../types";
|
||||
|
||||
type Param = {
|
||||
userId?: number | string;
|
||||
mariadbUserHost?: string;
|
||||
mariadbUsername?: string;
|
||||
sqlUserID?: string | number;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Refresh Mariadb User Grants
|
||||
*/
|
||||
export default async function refreshUsersAndGrants({
|
||||
userId,
|
||||
mariadbUserHost,
|
||||
mariadbUsername,
|
||||
sqlUserID,
|
||||
}: Param) {
|
||||
const mariadbUsers = (await dbHandler({
|
||||
query: `SELECT * FROM mariadb_users`,
|
||||
})) as any[] | null;
|
||||
|
||||
if (!mariadbUsers?.[0]) {
|
||||
return;
|
||||
}
|
||||
|
||||
const isRootUser = userId
|
||||
? userId == Number(process.env.DSQL_SU_USER_ID)
|
||||
: false;
|
||||
|
||||
const isWildcardHost = mariadbUserHost == "%";
|
||||
|
||||
if (isWildcardHost && !isRootUser) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (let i = 0; i < mariadbUsers.length; i++) {
|
||||
const mariadbUser = mariadbUsers[i] as
|
||||
| DSQL_DATASQUIREL_MARIADB_USERS
|
||||
| undefined;
|
||||
|
||||
if (!mariadbUser) continue;
|
||||
if (userId && mariadbUser.user_id != userId) continue;
|
||||
if (sqlUserID && mariadbUser.id != sqlUserID) continue;
|
||||
|
||||
try {
|
||||
const { username, password, host, user_id } = mariadbUser;
|
||||
|
||||
const existingUser = await noDatabaseDbHandler(
|
||||
`SELECT * FROM mysql.user WHERE User = '${username}' AND Host = '${host}'`
|
||||
);
|
||||
|
||||
const isUserExisting = Boolean(existingUser?.[0]?.User);
|
||||
|
||||
const isPrimary = String(mariadbUser.primary)?.match(/1/)
|
||||
? true
|
||||
: false;
|
||||
|
||||
const dsqlPassword = mariadbUser?.password
|
||||
? decrypt({ encryptedString: mariadbUser.password })
|
||||
: isUserExisting && password
|
||||
? decrypt({ encryptedString: password })
|
||||
: generator.generate({
|
||||
length: 16,
|
||||
numbers: true,
|
||||
symbols: true,
|
||||
uppercase: true,
|
||||
exclude: "*#.'`\"",
|
||||
});
|
||||
|
||||
const encryptedPassword = mariadbUser?.password
|
||||
? mariadbUser.password
|
||||
: isUserExisting
|
||||
? password
|
||||
: encrypt({ data: dsqlPassword });
|
||||
|
||||
if (!isUserExisting) {
|
||||
if (isWildcardHost) {
|
||||
const _existingUsers = (await noDatabaseDbHandler(
|
||||
`SELECT * FROM mysql.user WHERE user='${mariadbUsername}'`
|
||||
)) as MariaDBUser[];
|
||||
|
||||
for (let i = 0; i < _existingUsers.length; i++) {
|
||||
const exUsr = _existingUsers[i];
|
||||
await noDatabaseDbHandler(
|
||||
`DROP USER '${exUsr.User}'@'${exUsr.Host}'`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const createNewUser = await noDatabaseDbHandler(
|
||||
`CREATE USER IF NOT EXISTS '${mariadbUsername}'@'${mariadbUserHost}' IDENTIFIED BY '${dsqlPassword}'`
|
||||
);
|
||||
}
|
||||
|
||||
if (isPrimary) {
|
||||
const updateUser = await dbHandler({
|
||||
query: `UPDATE users SET mariadb_user = ?, mariadb_host = ?, mariadb_pass = ? WHERE id = ?`,
|
||||
values: [
|
||||
mariadbUsername,
|
||||
mariadbUserHost,
|
||||
encryptedPassword,
|
||||
user_id,
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
const isGrantHandled = await handleGrants({
|
||||
username: mariadbUser.username,
|
||||
host: mariadbUser.host,
|
||||
grants:
|
||||
mariadbUser.grants && typeof mariadbUser.grants == "string"
|
||||
? JSON.parse(mariadbUser.grants)
|
||||
: [],
|
||||
userId: String(user_id),
|
||||
});
|
||||
|
||||
if (!isGrantHandled) {
|
||||
console.log(
|
||||
`Error in handling grants for user ${mariadbUser.username}@${mariadbUser.host}`
|
||||
);
|
||||
}
|
||||
} catch (error: any) {
|
||||
global.ERROR_CALLBACK?.(
|
||||
`Error Refreshing MariaDB Users and Grants`,
|
||||
error as Error
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
require("dotenv").config({ path: "../../.env" });
|
||||
import generator from "generate-password";
|
||||
import noDatabaseDbHandler from "../utils/noDatabaseDbHandler";
|
||||
import dbHandler from "../utils/dbHandler";
|
||||
import dbHandler from "../../functions/backend/dbHandler";
|
||||
import encrypt from "../../functions/dsql/encrypt";
|
||||
import grabSQLKeyName from "../../utils/grab-sql-key-name";
|
||||
|
||||
/**
|
||||
* # Reset SQL Passwords
|
||||
@@ -23,7 +24,9 @@ async function resetSQLCredentialsPasswords() {
|
||||
|
||||
try {
|
||||
const maridbUsers = (await dbHandler({
|
||||
query: `SELECT * FROM mysql.user WHERE User = 'dsql_user_${user.id}'`,
|
||||
query: `SELECT * FROM mysql.user WHERE User = '${grabSQLKeyName(
|
||||
{ type: "user", userId: user.id }
|
||||
)}'`,
|
||||
})) as any[];
|
||||
|
||||
for (let j = 0; j < maridbUsers.length; j++) {
|
||||
|
||||
@@ -8,6 +8,8 @@ import addDbEntry from "../../../functions/backend/db/addDbEntry";
|
||||
import addMariadbUser from "../../../functions/backend/addMariadbUser";
|
||||
import updateDbEntry from "../../../functions/backend/db/updateDbEntry";
|
||||
import hashPassword from "../../../functions/dsql/hashPassword";
|
||||
import { DSQL_DATASQUIREL_USERS } from "../../../types/dsql";
|
||||
import grabDirNames from "../../../utils/backend/names/grab-dir-names";
|
||||
|
||||
const tmpDir = process.argv[process.argv.length - 1];
|
||||
|
||||
@@ -76,14 +78,14 @@ async function createUser() {
|
||||
data: { ...userObj, password: hashedPassword },
|
||||
});
|
||||
|
||||
if (!newUser?.insertId) return false;
|
||||
if (!newUser?.payload?.insertId) return false;
|
||||
|
||||
/**
|
||||
* Add a Mariadb User for this User
|
||||
*/
|
||||
await addMariadbUser({ userId: newUser.insertId });
|
||||
await addMariadbUser({ userId: newUser.payload.insertId });
|
||||
|
||||
const STATIC_ROOT = process.env.DSQL_STATIC_SERVER_DIR;
|
||||
const { STATIC_ROOT } = grabDirNames();
|
||||
|
||||
if (!STATIC_ROOT) {
|
||||
console.log("Static File ENV not Found!");
|
||||
@@ -95,10 +97,10 @@ async function createUser() {
|
||||
*
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
let newUserSchemaFolderPath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${newUser.insertId}`;
|
||||
let newUserSchemaFolderPath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${newUser.payload.insertId}`;
|
||||
let newUserMediaFolderPath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}`
|
||||
`images/user-images/user-${newUser.payload.insertId}`
|
||||
);
|
||||
|
||||
fs.mkdirSync(newUserSchemaFolderPath, { recursive: true });
|
||||
@@ -112,7 +114,7 @@ async function createUser() {
|
||||
|
||||
const imageBasePath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}`
|
||||
`images/user-images/user-${newUser.payload.insertId}`
|
||||
);
|
||||
|
||||
if (!fs.existsSync(imageBasePath)) {
|
||||
@@ -121,12 +123,12 @@ async function createUser() {
|
||||
|
||||
let imagePath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}/user-${newUser.insertId}-profile.jpg`
|
||||
`images/user-images/user-${newUser.payload.insertId}/user-${newUser.payload.insertId}-profile.jpg`
|
||||
);
|
||||
|
||||
let imageThumbnailPath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}/user-${newUser.insertId}-profile-thumbnail.jpg`
|
||||
`images/user-images/user-${newUser.payload.insertId}/user-${newUser.payload.insertId}-profile-thumbnail.jpg`
|
||||
);
|
||||
|
||||
let prodImageUrl = imagePath.replace(
|
||||
@@ -149,11 +151,11 @@ async function createUser() {
|
||||
|
||||
execSync(`chmod 644 ${imagePath} ${imageThumbnailPath}`);
|
||||
|
||||
const updateImages = await updateDbEntry({
|
||||
const updateImages = await updateDbEntry<DSQL_DATASQUIREL_USERS>({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "users",
|
||||
identifierColumnName: "id",
|
||||
identifierValue: newUser.insertId,
|
||||
identifierValue: newUser.payload.insertId,
|
||||
data: {
|
||||
image: prodImageUrl,
|
||||
image_thumbnail: prodImageThumbnailUrl,
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
import generator from "generate-password";
|
||||
import noDatabaseDbHandler from "./utils/noDatabaseDbHandler";
|
||||
import dbHandler from "./utils/dbHandler";
|
||||
import encrypt from "../functions/dsql/encrypt";
|
||||
|
||||
/**
|
||||
* Create database from Schema Function
|
||||
* ==============================================================================
|
||||
* @param {object} params - Single object params
|
||||
* @param {number|string|null} params.userId - User ID or null
|
||||
*/
|
||||
async function resetSQLCredentials() {
|
||||
const users = (await dbHandler({
|
||||
query: `SELECT * FROM users`,
|
||||
})) as any[];
|
||||
|
||||
if (!users) {
|
||||
process.exit();
|
||||
}
|
||||
|
||||
for (let i = 0; i < users.length; i++) {
|
||||
const user = users[i];
|
||||
|
||||
if (!user) continue;
|
||||
|
||||
const defaultMariadbUserHost = process.env.DSQL_DB_HOST || "127.0.0.1";
|
||||
|
||||
try {
|
||||
const username = `dsql_user_${user.id}`;
|
||||
const password = generator.generate({
|
||||
length: 16,
|
||||
numbers: true,
|
||||
symbols: true,
|
||||
uppercase: true,
|
||||
exclude: "*#.'`\"",
|
||||
});
|
||||
const encryptedPassword = encrypt({ data: password });
|
||||
|
||||
await noDatabaseDbHandler(`DROP USER IF EXISTS '${username}'@'%'`);
|
||||
await noDatabaseDbHandler(
|
||||
`DROP USER IF EXISTS '${username}'@'${defaultMariadbUserHost}'`
|
||||
);
|
||||
|
||||
await noDatabaseDbHandler(
|
||||
`CREATE USER IF NOT EXISTS '${username}'@'${defaultMariadbUserHost}' IDENTIFIED BY '${password}'`
|
||||
);
|
||||
|
||||
await noDatabaseDbHandler(
|
||||
`GRANT ALL PRIVILEGES ON \`datasquirel_user_${user.id}_%\`.* TO '${username}'@'${defaultMariadbUserHost}'`
|
||||
);
|
||||
|
||||
await noDatabaseDbHandler(`FLUSH PRIVILEGES`);
|
||||
|
||||
const updateUser = await dbHandler({
|
||||
query: `UPDATE users SET mariadb_user = ?, mariadb_host = ?, mariadb_pass = ? WHERE id = ?`,
|
||||
values: [
|
||||
username,
|
||||
defaultMariadbUserHost,
|
||||
encryptedPassword,
|
||||
user.id,
|
||||
],
|
||||
});
|
||||
|
||||
console.log(
|
||||
`User ${user.id}: ${user.first_name} ${user.last_name} SQL credentials successfully added.`
|
||||
);
|
||||
} catch (error: any) {
|
||||
global.ERROR_CALLBACK?.(
|
||||
`Error Resetting SQL credentials`,
|
||||
error as Error
|
||||
);
|
||||
console.log(`Error in adding SQL user =>`, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
process.exit();
|
||||
}
|
||||
|
||||
resetSQLCredentials();
|
||||
@@ -1,8 +1,9 @@
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
import generator from "generate-password";
|
||||
import noDatabaseDbHandler from "./utils/noDatabaseDbHandler";
|
||||
import dbHandler from "./utils/dbHandler";
|
||||
import dbHandler from "../functions/backend/dbHandler";
|
||||
import encrypt from "../functions/dsql/encrypt";
|
||||
import grabSQLKeyName from "../utils/grab-sql-key-name";
|
||||
|
||||
/**
|
||||
* # Create database from Schema Function
|
||||
@@ -24,7 +25,7 @@ async function resetSQLCredentialsPasswords() {
|
||||
const defaultMariadbUserHost = process.env.DSQL_DB_HOST || "127.0.0.1";
|
||||
|
||||
try {
|
||||
const username = `dsql_user_${user.id}`;
|
||||
const username = grabSQLKeyName({ type: "user", userId: user.id });
|
||||
const password = generator.generate({
|
||||
length: 16,
|
||||
numbers: true,
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
import generator from "generate-password";
|
||||
import noDatabaseDbHandler from "./utils/noDatabaseDbHandler";
|
||||
import dbHandler from "./utils/dbHandler";
|
||||
import dbHandler from "../functions/backend/dbHandler";
|
||||
import encrypt from "../functions/dsql/encrypt";
|
||||
import grabSQLKeyName from "../utils/grab-sql-key-name";
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
@@ -32,7 +33,7 @@ async function setSQLCredentials() {
|
||||
}
|
||||
|
||||
try {
|
||||
const username = `dsql_user_${user.id}`;
|
||||
const username = grabSQLKeyName({ type: "user", userId: user.id });
|
||||
const password = generator.generate({
|
||||
length: 16,
|
||||
numbers: true,
|
||||
|
||||
@@ -1,21 +1,9 @@
|
||||
// @ts-check
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
import generator from "generate-password";
|
||||
import noDatabaseDbHandler from "./utils/noDatabaseDbHandler";
|
||||
import dbHandler from "./utils/dbHandler";
|
||||
import dbHandler from "../functions/backend/dbHandler";
|
||||
import encrypt from "../functions/dsql/encrypt";
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
import grabSQLKeyName from "../utils/grab-sql-key-name";
|
||||
|
||||
/**
|
||||
* # Test SQL Escape
|
||||
@@ -37,7 +25,7 @@ export default async function testSQLEscape() {
|
||||
const defaultMariadbUserHost = process.env.DSQL_DB_HOST || "127.0.0.1";
|
||||
|
||||
try {
|
||||
const username = `dsql_user_${user.id}`;
|
||||
const username = grabSQLKeyName({ type: "user", userId: user.id });
|
||||
const password = generator.generate({
|
||||
length: 16,
|
||||
numbers: true,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
import mysql from "serverless-mysql";
|
||||
import grabDSQLConnection from "../utils/grab-dsql-connection";
|
||||
import grabSQLKeyName from "../utils/grab-sql-key-name";
|
||||
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
@@ -28,7 +29,7 @@ import grabDSQLConnection from "../utils/grab-dsql-connection";
|
||||
if (
|
||||
user.User !== process.env.DSQL_DB_READ_ONLY_USERNAME ||
|
||||
user.User !== process.env.DSQL_DB_FULL_ACCESS_USERNAME ||
|
||||
!user.User?.match(/dsql_user_.*/i)
|
||||
!user.User?.match(new RegExp(grabSQLKeyName({ type: "user" })))
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
import varDatabaseDbHandler from "./varDatabaseDbHandler";
|
||||
import { DSQL_TableSchemaType } from "../../types";
|
||||
import {
|
||||
DSQL_DATASQUIREL_USER_DATABASE_TABLES,
|
||||
DSQL_DATASQUIREL_USER_DATABASES,
|
||||
} from "../../types/dsql";
|
||||
import numberfy from "../../utils/numberfy";
|
||||
import updateDbEntry from "../../functions/backend/db/updateDbEntry";
|
||||
import addDbEntry from "../../functions/backend/db/addDbEntry";
|
||||
import slugToNormalText from "../../utils/slug-to-normal-text";
|
||||
import debugLog from "../../utils/logging/debug-log";
|
||||
import _ from "lodash";
|
||||
|
||||
type Param = {
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
recordedDbEntry?: DSQL_DATASQUIREL_USER_DATABASES;
|
||||
update?: boolean;
|
||||
isMain?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Handle Table Record Update and Insert
|
||||
*/
|
||||
export default async function ({
|
||||
tableSchema,
|
||||
recordedDbEntry,
|
||||
update,
|
||||
isMain,
|
||||
}: Param): Promise<number | undefined> {
|
||||
if (isMain) return undefined;
|
||||
|
||||
let tableId: number | undefined;
|
||||
|
||||
const targetDatabase = "datasquirel";
|
||||
const targetTableName = "user_database_tables";
|
||||
|
||||
if (!tableSchema?.tableName) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const newTableSchema = _.cloneDeep(tableSchema);
|
||||
|
||||
try {
|
||||
if (!recordedDbEntry) {
|
||||
throw new Error("Recorded Db entry not found!");
|
||||
}
|
||||
|
||||
// const existingTableName = newTableSchema.tableNameOld
|
||||
// ? newTableSchema.tableNameOld
|
||||
// : newTableSchema.tableName;
|
||||
|
||||
const newTableEntry: DSQL_DATASQUIREL_USER_DATABASE_TABLES = {
|
||||
user_id: recordedDbEntry.user_id,
|
||||
db_id: recordedDbEntry.id,
|
||||
db_slug: recordedDbEntry.db_slug,
|
||||
table_name: slugToNormalText(newTableSchema.tableName),
|
||||
table_slug: newTableSchema.tableName,
|
||||
child_table: newTableSchema.childTable ? 1 : 0,
|
||||
child_table_parent_database_schema_id: newTableSchema.childTableDbId
|
||||
? numberfy(newTableSchema.childTableDbId)
|
||||
: 0,
|
||||
child_table_parent_table_schema_id: newTableSchema.childTableId
|
||||
? numberfy(newTableSchema.childTableId)
|
||||
: 0,
|
||||
table_schema_id: newTableSchema.id
|
||||
? numberfy(newTableSchema.id)
|
||||
: 0,
|
||||
active_data: newTableSchema.updateData ? 1 : 0,
|
||||
};
|
||||
|
||||
const existingTable = await varDatabaseDbHandler({
|
||||
queryString: `SELECT * FROM ${targetDatabase}.${targetTableName} WHERE db_id = ? AND table_slug = ?`,
|
||||
queryValuesArray: [
|
||||
String(recordedDbEntry.id),
|
||||
String(newTableSchema.tableName),
|
||||
],
|
||||
});
|
||||
|
||||
const table: DSQL_DATASQUIREL_USER_DATABASE_TABLES = existingTable?.[0];
|
||||
|
||||
if (table?.id) {
|
||||
tableId = table.id;
|
||||
if (update) {
|
||||
await updateDbEntry<DSQL_DATASQUIREL_USER_DATABASE_TABLES>({
|
||||
data: newTableEntry,
|
||||
identifierColumnName: "id",
|
||||
identifierValue: table.id,
|
||||
tableName: targetTableName,
|
||||
dbFullName: targetDatabase,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
const newTableEntryRes =
|
||||
await addDbEntry<DSQL_DATASQUIREL_USER_DATABASE_TABLES>({
|
||||
data: newTableEntry,
|
||||
tableName: targetTableName,
|
||||
dbFullName: targetDatabase,
|
||||
});
|
||||
|
||||
if (newTableEntryRes?.payload?.insertId) {
|
||||
tableId = newTableEntryRes.payload.insertId;
|
||||
}
|
||||
}
|
||||
|
||||
if (newTableSchema.tableNameOld) {
|
||||
}
|
||||
|
||||
return tableId;
|
||||
} catch (error) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,18 @@
|
||||
import varDatabaseDbHandler from "./varDatabaseDbHandler";
|
||||
import generateColumnDescription from "./generateColumnDescription";
|
||||
import supplementTable from "./supplementTable";
|
||||
import dbHandler from "./dbHandler";
|
||||
import { DSQL_DatabaseSchemaType, DSQL_TableSchemaType } from "../../types";
|
||||
import { DSQL_FieldSchemaType, DSQL_TableSchemaType } from "../../types";
|
||||
import { DSQL_DATASQUIREL_USER_DATABASES } from "../../types/dsql";
|
||||
import handleTableForeignKey from "./handle-table-foreign-key";
|
||||
import createTableHandleTableRecord from "./create-table-handle-table-record";
|
||||
|
||||
type Param = {
|
||||
dbFullName: string;
|
||||
tableName: string;
|
||||
tableInfoArray: any[];
|
||||
tableInfoArray: DSQL_FieldSchemaType[];
|
||||
tableSchema?: DSQL_TableSchemaType;
|
||||
recordedDbEntry?: any;
|
||||
recordedDbEntry?: DSQL_DATASQUIREL_USER_DATABASES;
|
||||
isMain?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -21,110 +24,28 @@ export default async function createTable({
|
||||
tableInfoArray,
|
||||
tableSchema,
|
||||
recordedDbEntry,
|
||||
isMain,
|
||||
}: Param) {
|
||||
/**
|
||||
* Format tableInfoArray
|
||||
*
|
||||
* @description Format tableInfoArray
|
||||
*/
|
||||
const finalTable = supplementTable({ tableInfoArray: tableInfoArray });
|
||||
|
||||
/**
|
||||
* Grab Schema
|
||||
*
|
||||
* @description Grab Schema
|
||||
*/
|
||||
let tableId = await createTableHandleTableRecord({
|
||||
recordedDbEntry,
|
||||
tableSchema,
|
||||
isMain,
|
||||
});
|
||||
|
||||
if (!tableId && !isMain) throw new Error(`Couldn't grab table ID`);
|
||||
|
||||
const createTableQueryArray = [];
|
||||
|
||||
createTableQueryArray.push(
|
||||
`CREATE TABLE IF NOT EXISTS \`${dbFullName}\`.\`${tableName}\` (`
|
||||
);
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
try {
|
||||
if (!recordedDbEntry) {
|
||||
throw new Error("Recorded Db entry not found!");
|
||||
}
|
||||
|
||||
const existingTable = await varDatabaseDbHandler({
|
||||
queryString: `SELECT * FROM datasquirel.user_database_tables WHERE db_id = ? AND table_slug = ?`,
|
||||
queryValuesArray: [recordedDbEntry.id, tableSchema?.tableName],
|
||||
});
|
||||
|
||||
/** @type {import("../../types").MYSQL_user_database_tables_table_def} */
|
||||
const table: import("../../types").MYSQL_user_database_tables_table_def =
|
||||
existingTable?.[0];
|
||||
|
||||
if (!table?.id) {
|
||||
const newTableEntry = await dbHandler({
|
||||
query: `INSERT INTO datasquirel.user_database_tables SET ?`,
|
||||
values: {
|
||||
user_id: recordedDbEntry.user_id,
|
||||
db_id: recordedDbEntry.id,
|
||||
db_slug: recordedDbEntry.db_slug,
|
||||
table_name: tableSchema?.tableFullName,
|
||||
table_slug: tableSchema?.tableName,
|
||||
child_table: tableSchema?.childTable ? "1" : null,
|
||||
child_table_parent_database:
|
||||
tableSchema?.childTableDbFullName || null,
|
||||
child_table_parent_table:
|
||||
tableSchema?.childTableName || null,
|
||||
date_created: Date(),
|
||||
date_created_code: Date.now(),
|
||||
date_updated: Date(),
|
||||
date_updated_code: Date.now(),
|
||||
},
|
||||
});
|
||||
}
|
||||
} catch (error) {}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
let primaryKeySet = false;
|
||||
|
||||
/** @type {import("../../types").DSQL_FieldSchemaType[]} */
|
||||
let foreignKeys: import("../../types").DSQL_FieldSchemaType[] = [];
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
for (let i = 0; i < finalTable.length; i++) {
|
||||
const column = finalTable[i];
|
||||
const {
|
||||
fieldName,
|
||||
dataType,
|
||||
nullValue,
|
||||
primaryKey,
|
||||
autoIncrement,
|
||||
defaultValue,
|
||||
defaultValueLiteral,
|
||||
foreignKey,
|
||||
updatedField,
|
||||
onUpdate,
|
||||
onUpdateLiteral,
|
||||
onDelete,
|
||||
onDeleteLiteral,
|
||||
defaultField,
|
||||
encrypted,
|
||||
json,
|
||||
newTempField,
|
||||
notNullValue,
|
||||
originName,
|
||||
plainText,
|
||||
pattern,
|
||||
patternFlags,
|
||||
richText,
|
||||
} = column;
|
||||
|
||||
if (foreignKey) {
|
||||
foreignKeys.push({
|
||||
...column,
|
||||
});
|
||||
}
|
||||
|
||||
let { fieldEntryText, newPrimaryKeySet } = generateColumnDescription({
|
||||
columnData: column,
|
||||
@@ -133,56 +54,39 @@ export default async function createTable({
|
||||
|
||||
primaryKeySet = newPrimaryKeySet;
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
const comma = (() => {
|
||||
if (foreignKeys[0]) return ",";
|
||||
if (i === finalTable.length - 1) return "";
|
||||
return ",";
|
||||
})();
|
||||
|
||||
createTableQueryArray.push(" " + fieldEntryText + comma);
|
||||
|
||||
////////////////////////////////////////
|
||||
}
|
||||
|
||||
if (foreignKeys[0]) {
|
||||
foreignKeys.forEach((foreighKey, index, array) => {
|
||||
const fieldName = foreighKey.fieldName;
|
||||
const destinationTableName =
|
||||
foreighKey.foreignKey?.destinationTableName;
|
||||
const destinationTableColumnName =
|
||||
foreighKey.foreignKey?.destinationTableColumnName;
|
||||
const cascadeDelete = foreighKey.foreignKey?.cascadeDelete;
|
||||
const cascadeUpdate = foreighKey.foreignKey?.cascadeUpdate;
|
||||
const foreignKeyName = foreighKey.foreignKey?.foreignKeyName;
|
||||
|
||||
const comma = (() => {
|
||||
if (index === foreignKeys.length - 1) return "";
|
||||
return ",";
|
||||
})();
|
||||
|
||||
createTableQueryArray.push(
|
||||
` CONSTRAINT \`${foreignKeyName}\` FOREIGN KEY (\`${fieldName}\`) REFERENCES \`${destinationTableName}\`(${destinationTableColumnName})${
|
||||
cascadeDelete ? " ON DELETE CASCADE" : ""
|
||||
}${cascadeUpdate ? " ON UPDATE CASCADE" : ""}${comma}`
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
createTableQueryArray.push(
|
||||
`) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;`
|
||||
);
|
||||
|
||||
const createTableQuery = createTableQueryArray.join("\n");
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
const newTable = await varDatabaseDbHandler({
|
||||
queryString: createTableQuery,
|
||||
});
|
||||
|
||||
return newTable;
|
||||
for (let i = 0; i < finalTable.length; i++) {
|
||||
const column = finalTable[i];
|
||||
const { foreignKey, fieldName } = column;
|
||||
|
||||
if (!fieldName) continue;
|
||||
|
||||
if (foreignKey) {
|
||||
await handleTableForeignKey({
|
||||
dbFullName,
|
||||
foreignKey,
|
||||
tableName,
|
||||
fieldName,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return tableId;
|
||||
}
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import grabDSQLConnection from "../../utils/grab-dsql-connection";
|
||||
|
||||
type Param = {
|
||||
query: string;
|
||||
values?: string[] | object;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
*/
|
||||
export default async function dbHandler({
|
||||
query,
|
||||
values,
|
||||
}: Param): Promise<any[] | object | null> {
|
||||
const CONNECTION = grabDSQLConnection();
|
||||
|
||||
let results;
|
||||
|
||||
try {
|
||||
if (query && values) {
|
||||
results = await CONNECTION.query(query, values);
|
||||
} else {
|
||||
results = await CONNECTION.query(query);
|
||||
}
|
||||
} catch (error: any) {
|
||||
global.ERROR_CALLBACK?.(`DB Handler Error...`, error as Error);
|
||||
|
||||
if (process.env.FIRST_RUN) {
|
||||
return null;
|
||||
}
|
||||
|
||||
console.log("ERROR in dbHandler =>", error.message);
|
||||
console.log(error);
|
||||
console.log(CONNECTION.config());
|
||||
|
||||
const tmpFolder = path.resolve(process.cwd(), "./.tmp");
|
||||
if (!fs.existsSync(tmpFolder))
|
||||
fs.mkdirSync(tmpFolder, { recursive: true });
|
||||
|
||||
fs.appendFileSync(
|
||||
path.resolve(tmpFolder, "./dbErrorLogs.txt"),
|
||||
JSON.stringify(error, null, 4) + "\n" + Date() + "\n\n\n",
|
||||
"utf8"
|
||||
);
|
||||
results = null;
|
||||
} finally {
|
||||
await CONNECTION?.end();
|
||||
}
|
||||
|
||||
if (results) {
|
||||
return JSON.parse(JSON.stringify(results));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import grabSQLKeyName from "../../utils/grab-sql-key-name";
|
||||
import varDatabaseDbHandler from "./varDatabaseDbHandler";
|
||||
|
||||
type Param = {
|
||||
dbFullName: string;
|
||||
tableName: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Drop All Foreign Keys
|
||||
*/
|
||||
export default async function dropAllForeignKeys({
|
||||
dbFullName,
|
||||
tableName,
|
||||
}: Param) {
|
||||
try {
|
||||
// const rows = await varDatabaseDbHandler({
|
||||
// queryString: `SELECT CONSTRAINT_NAME FROM information_schema.REFERENTIAL_CONSTRAINTS WHERE TABLE_NAME = '${tableName}' AND CONSTRAINT_SCHEMA = '${dbFullName}'`,
|
||||
// });
|
||||
|
||||
// console.log("rows", rows);
|
||||
// console.log("dbFullName", dbFullName);
|
||||
// console.log("tableName", tableName);
|
||||
|
||||
// for (const row of rows) {
|
||||
// await varDatabaseDbHandler({
|
||||
// queryString: `ALTER TABLE \`${dbFullName}\`.\`${tableName}\` DROP FOREIGN KEY \`${row.CONSTRAINT_NAME}\`
|
||||
// `,
|
||||
// });
|
||||
// }
|
||||
|
||||
const foreignKeys = await varDatabaseDbHandler({
|
||||
queryString: `SHOW INDEXES FROM \`${dbFullName}\`.\`${tableName}\` WHERE Key_name LIKE '${grabSQLKeyName(
|
||||
{ type: "foreign_key" }
|
||||
)}%'`,
|
||||
});
|
||||
|
||||
for (const fk of foreignKeys) {
|
||||
if (
|
||||
fk.Key_name.match(
|
||||
new RegExp(grabSQLKeyName({ type: "foreign_key" }))
|
||||
)
|
||||
) {
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `ALTER TABLE \`${dbFullName}\`.\`${tableName}\` DROP INDEX \`${fk.Key_name}\`
|
||||
`,
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.log(`dropAllForeignKeys ERROR => ${error.message}`);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
import { DSQL_FieldSchemaType } from "../../types";
|
||||
import dataTypeConstructor from "../../utils/db/schema/data-type-constructor";
|
||||
import dataTypeParser from "../../utils/db/schema/data-type-parser";
|
||||
|
||||
type Param = {
|
||||
columnData: import("../../types").DSQL_FieldSchemaType;
|
||||
columnData: DSQL_FieldSchemaType;
|
||||
primaryKeySet?: boolean;
|
||||
};
|
||||
|
||||
@@ -15,11 +19,6 @@ export default function generateColumnDescription({
|
||||
columnData,
|
||||
primaryKeySet,
|
||||
}: Param): Return {
|
||||
/**
|
||||
* Format tableInfoArray
|
||||
*
|
||||
* @description Format tableInfoArray
|
||||
*/
|
||||
const {
|
||||
fieldName,
|
||||
dataType,
|
||||
@@ -30,13 +29,19 @@ export default function generateColumnDescription({
|
||||
defaultValueLiteral,
|
||||
onUpdateLiteral,
|
||||
notNullValue,
|
||||
unique,
|
||||
} = columnData;
|
||||
|
||||
let fieldEntryText = "";
|
||||
|
||||
fieldEntryText += `\`${fieldName}\` ${dataType}`;
|
||||
const finalDataTypeObject = dataTypeParser(dataType);
|
||||
const finalDataType = dataTypeConstructor(
|
||||
finalDataTypeObject.type,
|
||||
finalDataTypeObject.limit,
|
||||
finalDataTypeObject.decimal
|
||||
);
|
||||
|
||||
////////////////////////////////////////
|
||||
fieldEntryText += `\`${fieldName}\` ${finalDataType}`;
|
||||
|
||||
if (nullValue) {
|
||||
fieldEntryText += " DEFAULT NULL";
|
||||
@@ -46,35 +51,32 @@ export default function generateColumnDescription({
|
||||
if (String(defaultValue).match(/uuid\(\)/i)) {
|
||||
fieldEntryText += ` DEFAULT UUID()`;
|
||||
} else {
|
||||
fieldEntryText += ` DEFAULT '${defaultValue}'`;
|
||||
fieldEntryText += ` DEFAULT '${String(defaultValue)
|
||||
.replace(/^\'|\'$/g, "")
|
||||
.replace(/\'/g, "\\'")}'`;
|
||||
}
|
||||
} else if (notNullValue) {
|
||||
fieldEntryText += ` NOT NULL`;
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
if (onUpdateLiteral) {
|
||||
fieldEntryText += ` ON UPDATE ${onUpdateLiteral}`;
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
if (primaryKey && !primaryKeySet) {
|
||||
fieldEntryText += " PRIMARY KEY";
|
||||
primaryKeySet = true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
if (autoIncrement) {
|
||||
fieldEntryText += " AUTO_INCREMENT";
|
||||
primaryKeySet = true;
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
if (unique) {
|
||||
fieldEntryText += " UNIQUE";
|
||||
primaryKeySet = true;
|
||||
}
|
||||
|
||||
return {
|
||||
fieldEntryText,
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
export default function grabDSQLSchemaIndexComment() {
|
||||
return `dsql_schema_index`;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import varDatabaseDbHandler from "./varDatabaseDbHandler";
|
||||
import { DSQL_ForeignKeyType } from "../../types";
|
||||
|
||||
type Param = {
|
||||
dbFullName: string;
|
||||
tableName: string;
|
||||
foreignKey: DSQL_ForeignKeyType;
|
||||
fieldName: string;
|
||||
errorLogs?: any[];
|
||||
};
|
||||
|
||||
/**
|
||||
* # Update table function
|
||||
*/
|
||||
export default async function handleTableForeignKey({
|
||||
dbFullName,
|
||||
tableName,
|
||||
foreignKey,
|
||||
errorLogs,
|
||||
fieldName,
|
||||
}: Param) {
|
||||
const {
|
||||
destinationTableName,
|
||||
destinationTableColumnName,
|
||||
cascadeDelete,
|
||||
cascadeUpdate,
|
||||
foreignKeyName,
|
||||
} = foreignKey;
|
||||
|
||||
let finalQueryString = `ALTER TABLE \`${dbFullName}\`.\`${tableName}\``;
|
||||
|
||||
finalQueryString += ` ADD CONSTRAINT \`${foreignKeyName}\` FOREIGN KEY (\`${fieldName}\`)`;
|
||||
finalQueryString += ` REFERENCES \`${destinationTableName}\`(\`${destinationTableColumnName}\`)`;
|
||||
|
||||
if (cascadeDelete) finalQueryString += ` ON DELETE CASCADE`;
|
||||
if (cascadeUpdate) finalQueryString += ` ON UPDATE CASCADE`;
|
||||
|
||||
// let foreinKeyText = `ADD CONSTRAINT \`${foreignKeyName}\` FOREIGN KEY (\`${destinationTableColumnType}\`) REFERENCES \`${destinationTableName}\`(\`${destinationTableColumnName}\`)${
|
||||
// cascadeDelete ? " ON DELETE CASCADE" : ""
|
||||
// }${cascadeUpdate ? " ON UPDATE CASCADE" : ""}`;
|
||||
|
||||
// let finalQueryString = `ALTER TABLE \`${dbFullName}\`.\`${tableName}\` ${foreinKeyText}`;
|
||||
|
||||
const addForeignKey = await varDatabaseDbHandler({
|
||||
queryString: finalQueryString,
|
||||
});
|
||||
|
||||
if (!addForeignKey?.serverStatus) {
|
||||
errorLogs?.push(addForeignKey);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import dbHandler from "./dbHandler";
|
||||
import dbHandler from "../../functions/backend/dbHandler";
|
||||
|
||||
export default async function noDatabaseDbHandler(
|
||||
queryString: string
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,4 @@
|
||||
import dbHandler from "./dbHandler";
|
||||
import { DSQL_TableSchemaType } from "../../types";
|
||||
import dbHandler from "../../functions/backend/dbHandler";
|
||||
|
||||
type Param = {
|
||||
queryString: string;
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
SELECT
|
||||
kcu.TABLE_NAME,
|
||||
kcu.COLUMN_NAME,
|
||||
kcu.CONSTRAINT_NAME,
|
||||
kcu.REFERENCED_TABLE_NAME,
|
||||
kcu.REFERENCED_COLUMN_NAME,
|
||||
rc.UPDATE_RULE,
|
||||
rc.DELETE_RULE
|
||||
FROM
|
||||
INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
|
||||
JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc ON kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
|
||||
AND kcu.TABLE_SCHEMA = rc.CONSTRAINT_SCHEMA
|
||||
WHERE
|
||||
kcu.TABLE_SCHEMA = 'datasquirel'
|
||||
AND kcu.TABLE_NAME = '{{TABLE_NAME}}'
|
||||
AND kcu.REFERENCED_TABLE_NAME IS NOT NULL;
|
||||
+143
-93
@@ -1,21 +1,24 @@
|
||||
export const DsqlTables = [
|
||||
"users",
|
||||
"mariadb_users",
|
||||
"mariadb_user_databases",
|
||||
"mariadb_user_tables",
|
||||
"mariadb_user_privileges",
|
||||
"api_keys",
|
||||
"api_keys_scoped_resources",
|
||||
"invitations",
|
||||
"user_users",
|
||||
"delegated_user_tables",
|
||||
"delegated_resources",
|
||||
"user_databases",
|
||||
"user_database_tables",
|
||||
"user_media",
|
||||
"user_private_folders",
|
||||
"delegated_users",
|
||||
"unsubscribes",
|
||||
"notifications",
|
||||
"docs_pages",
|
||||
"docs_page_extra_links",
|
||||
"deleted_api_keys",
|
||||
"servers",
|
||||
"process_queue",
|
||||
"backups",
|
||||
] as const
|
||||
|
||||
export type DSQL_DATASQUIREL_USERS = {
|
||||
@@ -39,6 +42,7 @@ export type DSQL_DATASQUIREL_USERS = {
|
||||
mariadb_pass?: string;
|
||||
disk_usage_in_mb?: number;
|
||||
verification_status?: number;
|
||||
temp_login_code?: string;
|
||||
date_created?: string;
|
||||
date_created_code?: number;
|
||||
date_created_timestamp?: string;
|
||||
@@ -55,7 +59,63 @@ export type DSQL_DATASQUIREL_MARIADB_USERS = {
|
||||
host?: string;
|
||||
password?: string;
|
||||
primary?: number;
|
||||
grants?: string;
|
||||
all_databases?: 0 | 1;
|
||||
all_grants?: 0 | 1;
|
||||
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_USER_DATABASES = {
|
||||
id?: number;
|
||||
uuid?: string;
|
||||
user_id?: number;
|
||||
mariadb_user_id?: number;
|
||||
db_id?: number;
|
||||
db_schema_id?: number;
|
||||
db_slug?: string;
|
||||
all_tables?: 0 | 1;
|
||||
all_privileges?: 0 | 1;
|
||||
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_USER_TABLES = {
|
||||
id?: number;
|
||||
uuid?: string;
|
||||
user_id?: number;
|
||||
mariadb_user_id?: number;
|
||||
db_id?: number;
|
||||
db_schema_id?: number;
|
||||
db_slug?: string;
|
||||
table_schema_id?: number;
|
||||
table_slug?: string;
|
||||
all_fields?: 0 | 1;
|
||||
all_privileges?: 0 | 1;
|
||||
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_USER_PRIVILEGES = {
|
||||
id?: number;
|
||||
uuid?: string;
|
||||
user_id?: number;
|
||||
mariadb_user_id?: number;
|
||||
db_id?: number;
|
||||
db_schema_id?: number;
|
||||
db_slug?: string;
|
||||
privilege?: "ALTER" | "ALTER ROUTINE" | "CREATE" | "CREATE ROUTINE" | "CREATE TEMPORARY TABLES" | "CREATE VIEW" | "DELETE" | "DROP" | "EVENT" | "EXECUTE" | "FILE" | "INDEX" | "INSERT" | "LOCK TABLES" | "PROCESS" | "REFERENCES" | "RELOAD" | "SELECT" | "SHOW VIEW" | "SUPER" | "TRIGGER" | "UPDATE" | "USAGE";
|
||||
date_created?: string;
|
||||
date_created_code?: number;
|
||||
date_created_timestamp?: string;
|
||||
@@ -71,7 +131,9 @@ export type DSQL_DATASQUIREL_API_KEYS = {
|
||||
name?: string;
|
||||
slug?: string;
|
||||
key?: string;
|
||||
scope?: string;
|
||||
scope?: "readOnly" | "fullAccess";
|
||||
all_dbs?: 0 | 1;
|
||||
media_only?: 0 | 1;
|
||||
csrf?: string;
|
||||
date_created?: string;
|
||||
date_created_code?: number;
|
||||
@@ -81,50 +143,35 @@ export type DSQL_DATASQUIREL_API_KEYS = {
|
||||
date_updated_timestamp?: string;
|
||||
}
|
||||
|
||||
export type DSQL_DATASQUIREL_API_KEYS_SCOPED_RESOURCES = {
|
||||
id?: number;
|
||||
uuid?: string;
|
||||
user_id?: number;
|
||||
api_key_id?: number;
|
||||
db_id?: number;
|
||||
db_schema_id?: number;
|
||||
db_slug?: string;
|
||||
table_schema_id?: number;
|
||||
table_slug?: string;
|
||||
all_tables?: 0 | 1;
|
||||
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;
|
||||
invited_user_email?: string;
|
||||
invitation_status?: "pending" | "accepted" | "rejected" | "cancelled";
|
||||
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;
|
||||
db_tables_data?: string;
|
||||
email_sent?: 0 | 1;
|
||||
date_created?: string;
|
||||
date_created_code?: number;
|
||||
date_created_timestamp?: string;
|
||||
@@ -133,14 +180,19 @@ export type DSQL_DATASQUIREL_USER_USERS = {
|
||||
date_updated_timestamp?: string;
|
||||
}
|
||||
|
||||
export type DSQL_DATASQUIREL_DELEGATED_USER_TABLES = {
|
||||
export type DSQL_DATASQUIREL_DELEGATED_RESOURCES = {
|
||||
id?: number;
|
||||
uuid?: string;
|
||||
delegated_users_id?: number;
|
||||
user_id?: number;
|
||||
delegated_user_id?: number;
|
||||
root_user_id?: number;
|
||||
database?: string;
|
||||
table?: string;
|
||||
priviledge?: string;
|
||||
db_id?: number;
|
||||
db_schema_id?: number;
|
||||
db_slug?: string;
|
||||
table_schema_id?: number;
|
||||
table_slug?: string;
|
||||
permission?: "read" | "write" | "edit" | "delete";
|
||||
all_tables?: 0 | 1;
|
||||
date_created?: string;
|
||||
date_created_code?: number;
|
||||
date_created_timestamp?: string;
|
||||
@@ -153,6 +205,7 @@ export type DSQL_DATASQUIREL_USER_DATABASES = {
|
||||
id?: number;
|
||||
uuid?: string;
|
||||
user_id?: number;
|
||||
db_schema_id?: number;
|
||||
db_name?: string;
|
||||
db_slug?: string;
|
||||
db_full_name?: string;
|
||||
@@ -163,9 +216,11 @@ export type DSQL_DATASQUIREL_USER_DATABASES = {
|
||||
remote_db_full_name?: string;
|
||||
remote_connection_host?: string;
|
||||
remote_connection_key?: string;
|
||||
active_clone?: number;
|
||||
active_clone?: 0 | 1;
|
||||
active_clone_parent_db?: string;
|
||||
active_clone_parent_db_id?: number;
|
||||
active_data?: number;
|
||||
last_checked_date_code?: number;
|
||||
date_created?: string;
|
||||
date_created_code?: number;
|
||||
date_created_timestamp?: string;
|
||||
@@ -179,14 +234,16 @@ export type DSQL_DATASQUIREL_USER_DATABASE_TABLES = {
|
||||
uuid?: string;
|
||||
user_id?: number;
|
||||
db_id?: number;
|
||||
table_schema_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;
|
||||
child_table_parent_database_schema_id?: number;
|
||||
child_table_parent_table_schema_id?: number;
|
||||
active_data?: 0 | 1;
|
||||
last_checked_date_code?: number;
|
||||
date_created?: string;
|
||||
date_created_code?: number;
|
||||
date_created_timestamp?: string;
|
||||
@@ -203,13 +260,30 @@ export type DSQL_DATASQUIREL_USER_MEDIA = {
|
||||
folder?: string;
|
||||
media_url?: string;
|
||||
media_thumbnail_url?: string;
|
||||
media_path?: string;
|
||||
media_thumbnail_path?: string;
|
||||
media_type?: string;
|
||||
media_base64?: string;
|
||||
media_thumbnail_base64?: string;
|
||||
media_type?: "file" | "image" | "video";
|
||||
media_stats?: string;
|
||||
mime_type?: string;
|
||||
width?: number;
|
||||
height?: number;
|
||||
size?: number;
|
||||
private?: number;
|
||||
private?: 0 | 1;
|
||||
private_folder?: 0 | 1;
|
||||
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_PRIVATE_FOLDERS = {
|
||||
id?: number;
|
||||
uuid?: string;
|
||||
user_id?: number;
|
||||
folder_path?: string;
|
||||
child_folder?: 0 | 1;
|
||||
date_created?: string;
|
||||
date_created_code?: number;
|
||||
date_created_timestamp?: string;
|
||||
@@ -223,8 +297,6 @@ export type DSQL_DATASQUIREL_DELEGATED_USERS = {
|
||||
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;
|
||||
@@ -261,40 +333,6 @@ export type DSQL_DATASQUIREL_NOTIFICATIONS = {
|
||||
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;
|
||||
@@ -341,4 +379,16 @@ export type DSQL_DATASQUIREL_PROCESS_QUEUE = {
|
||||
date_updated?: string;
|
||||
date_updated_code?: number;
|
||||
date_updated_timestamp?: string;
|
||||
}
|
||||
|
||||
export type DSQL_DATASQUIREL_BACKUPS = {
|
||||
id?: number;
|
||||
uuid?: string;
|
||||
user_id?: number;
|
||||
date_created?: string;
|
||||
date_created_code?: number;
|
||||
date_created_timestamp?: string;
|
||||
date_updated?: string;
|
||||
date_updated_code?: number;
|
||||
date_updated_timestamp?: string;
|
||||
}
|
||||
+766
-306
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,47 @@
|
||||
import fs from "fs";
|
||||
import { SiteConfig } from "../../../types";
|
||||
import grabDirNames from "../names/grab-dir-names";
|
||||
import EJSON from "../../ejson";
|
||||
import envsub from "../../envsub";
|
||||
|
||||
type Params = {
|
||||
userId?: string | number;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
appConfig: SiteConfig;
|
||||
userConfig: SiteConfig | null;
|
||||
};
|
||||
|
||||
export default function grabConfig(params?: Params): Return {
|
||||
const { appConfigJSONFile, userConfigJSONFilePath } = grabDirNames({
|
||||
userId: params?.userId,
|
||||
});
|
||||
|
||||
const appConfigJSON = envsub(fs.readFileSync(appConfigJSONFile, "utf-8"));
|
||||
const appConfig = EJSON.parse(appConfigJSON) as SiteConfig;
|
||||
|
||||
if (!userConfigJSONFilePath) {
|
||||
return { appConfig, userConfig: null };
|
||||
}
|
||||
|
||||
if (!fs.existsSync(userConfigJSONFilePath)) {
|
||||
fs.writeFileSync(
|
||||
userConfigJSONFilePath,
|
||||
JSON.stringify({
|
||||
main: {},
|
||||
}),
|
||||
"utf-8"
|
||||
);
|
||||
}
|
||||
|
||||
const userConfigJSON = envsub(
|
||||
fs.readFileSync(userConfigJSONFilePath, "utf-8")
|
||||
);
|
||||
|
||||
const userConfig = (EJSON.parse(userConfigJSON) || {
|
||||
main: {},
|
||||
}) as SiteConfig;
|
||||
|
||||
return { appConfig, userConfig };
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user