Updates
This commit is contained in:
@@ -4,14 +4,14 @@ import grabDSQLConnection from "../../grab-dsql-connection";
|
||||
* # DSQL user read-only DB handler
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
*/
|
||||
export default async function DB_HANDLER(...args: any[]) {
|
||||
export default async function DB_HANDLER(query: string, values?: any[]) {
|
||||
const CONNECTION = grabDSQLConnection();
|
||||
|
||||
try {
|
||||
if (!CONNECTION)
|
||||
throw new Error("No Connection provided to DB_HANDLER function!");
|
||||
|
||||
const results = await CONNECTION.query(...args);
|
||||
const results = await CONNECTION.query(query, values);
|
||||
|
||||
return JSON.parse(JSON.stringify(results));
|
||||
} catch (error: any) {
|
||||
|
||||
@@ -3,11 +3,11 @@ import grabDSQLConnection from "../../grab-dsql-connection";
|
||||
/**
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
export default async function LOCAL_DB_HANDLER(...args: any[]) {
|
||||
export default async function LOCAL_DB_HANDLER(query: string, values?: any[]) {
|
||||
const MASTER = grabDSQLConnection();
|
||||
|
||||
try {
|
||||
const results = await MASTER.query(...args);
|
||||
const results = await MASTER.query(query, values);
|
||||
|
||||
return JSON.parse(JSON.stringify(results));
|
||||
} catch (error: any) {
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
import mysql from "serverless-mysql";
|
||||
import grabDbSSL from "../grabDbSSL";
|
||||
import grabDSQLConnection from "../../grab-dsql-connection";
|
||||
|
||||
/**
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
export default function NO_DB_HANDLER(...args: any[]) {
|
||||
export default function NO_DB_HANDLER(query: string, values?: any[]) {
|
||||
const CONNECTION = grabDSQLConnection();
|
||||
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
CONNECTION.query(...args)
|
||||
CONNECTION.query(query, values)
|
||||
.then((results) => {
|
||||
CONNECTION.end();
|
||||
resolve(JSON.parse(JSON.stringify(results)));
|
||||
|
||||
@@ -3,12 +3,12 @@ import grabDSQLConnection from "../../grab-dsql-connection";
|
||||
/**
|
||||
* # Root DB handler
|
||||
*/
|
||||
export default function ROOT_DB_HANDLER(...args: any[]) {
|
||||
export default function ROOT_DB_HANDLER(query: string, values?: any[]) {
|
||||
const CONNECTION = grabDSQLConnection();
|
||||
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
CONNECTION.query(...args)
|
||||
CONNECTION.query(query, values)
|
||||
.then((results) => {
|
||||
CONNECTION.end();
|
||||
resolve(JSON.parse(JSON.stringify(results)));
|
||||
|
||||
@@ -1,22 +1,19 @@
|
||||
import fs from "fs";
|
||||
import grabDirNames from "./names/grab-dir-names";
|
||||
import type { ConnectionConfig } from "mariadb";
|
||||
|
||||
type Return =
|
||||
| string
|
||||
| (import("tls").SecureContextOptions & {
|
||||
rejectUnauthorized?: boolean | undefined;
|
||||
})
|
||||
| undefined;
|
||||
type Return = ConnectionConfig["ssl"] | undefined;
|
||||
|
||||
/**
|
||||
* # Grall SSL
|
||||
* # Grab SSL
|
||||
*/
|
||||
export default function grabDbSSL(): Return {
|
||||
const SSL_DIR = process.env.DSQL_SSL_DIR;
|
||||
if (!SSL_DIR?.match(/./)) {
|
||||
const { maxscaleSSLDir } = grabDirNames();
|
||||
if (!maxscaleSSLDir?.match(/./)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const caFilePath = `${SSL_DIR}/ca-cert.pem`;
|
||||
const caFilePath = `${maxscaleSSLDir}/ca-cert.pem`;
|
||||
|
||||
if (!fs.existsSync(caFilePath)) {
|
||||
console.log(`${caFilePath} does not exist`);
|
||||
@@ -24,9 +21,7 @@ export default function grabDbSSL(): Return {
|
||||
}
|
||||
|
||||
return {
|
||||
ca: fs.readFileSync(`${SSL_DIR}/ca-cert.pem`),
|
||||
// key: fs.readFileSync(`${SSL_DIR}/client-key.pem`),
|
||||
// cert: fs.readFileSync(`${SSL_DIR}/client-cert.pem`),
|
||||
ca: fs.readFileSync(`${maxscaleSSLDir}/ca-cert.pem`),
|
||||
rejectUnauthorized: false,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -27,6 +27,11 @@ export default function grabDirNames(param?: Param) {
|
||||
const appSSLDir = path.join(appDir, "ssl");
|
||||
const mainSSLDir = path.join(DATA_DIR, "ssl");
|
||||
|
||||
const maxscaleSSLDir = path.join(mainSSLDir, "maxscale");
|
||||
const mainDBSSLDir = path.join(mainSSLDir, "main");
|
||||
const replica1DBSSLDir = path.join(mainSSLDir, "replica-1");
|
||||
const replica2DBSSLDir = path.join(mainSSLDir, "replica-2");
|
||||
|
||||
const privateDataDir = path.join(DATA_DIR, "private");
|
||||
|
||||
/**
|
||||
@@ -228,5 +233,9 @@ export default function grabDirNames(param?: Param) {
|
||||
sqlBackupDirName,
|
||||
schemasBackupDirName,
|
||||
userMainShemaJSONFilePath,
|
||||
maxscaleSSLDir,
|
||||
mainDBSSLDir,
|
||||
replica1DBSSLDir,
|
||||
replica2DBSSLDir,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
export default function grabDockerStackServicesNames() {
|
||||
const deploymentName = process.env.DSQL_DEPLOYMENT_NAME || "dsql";
|
||||
|
||||
const maxScaleServiceName = `${deploymentName}-dsql-maxscale`;
|
||||
const dbServiceName = `${deploymentName}-dsql-db`;
|
||||
const dbCronServiceName = `${deploymentName}-dsql-db-cron`;
|
||||
const postDbSetupServiceName = `${deploymentName}-dsql-post-db-setup`;
|
||||
const webAppServiceName = `${deploymentName}-dsql-web-app`;
|
||||
const webAppCronServiceName = `${deploymentName}-dsql-web-app-cron`;
|
||||
const webAppPostDbSetupServiceName = `${deploymentName}-dsql-web-app-post-db-setup`;
|
||||
const dbReplica1ServiceName = `${deploymentName}-dsql-db-replica-1`;
|
||||
const dbReplica2ServiceName = `${deploymentName}-dsql-db-replica-2`;
|
||||
|
||||
return {
|
||||
deploymentName,
|
||||
maxScaleServiceName,
|
||||
dbServiceName,
|
||||
dbCronServiceName,
|
||||
postDbSetupServiceName,
|
||||
webAppServiceName,
|
||||
webAppCronServiceName,
|
||||
webAppPostDbSetupServiceName,
|
||||
dbReplica1ServiceName,
|
||||
dbReplica2ServiceName,
|
||||
};
|
||||
}
|
||||
@@ -8,6 +8,15 @@ export default function grabIPAddresses() {
|
||||
const webAppIP = `${globalIPPrefix}.${web}`;
|
||||
const appCronIP = `${globalIPPrefix}.${cron}`;
|
||||
const maxScaleIP = `${globalIPPrefix}.${maxscale}`;
|
||||
const mainDBIP = `${globalIPPrefix}.${db}`;
|
||||
const localHostIP = `${globalIPPrefix}.1`;
|
||||
|
||||
return { webAppIP, appCronIP, maxScaleIP, globalIPPrefix };
|
||||
return {
|
||||
webAppIP,
|
||||
appCronIP,
|
||||
maxScaleIP,
|
||||
mainDBIP,
|
||||
localHostIP,
|
||||
globalIPPrefix,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ServerlessMysql } from "serverless-mysql";
|
||||
import debugLog from "../logging/debug-log";
|
||||
import { DSQLErrorObject } from "../../types";
|
||||
import type { ConnectionConfig, Pool } from "mariadb";
|
||||
|
||||
export type ConnDBHandlerQueryObject = {
|
||||
query: string;
|
||||
@@ -9,8 +9,9 @@ export type ConnDBHandlerQueryObject = {
|
||||
|
||||
type Return<ReturnType = any> =
|
||||
| ReturnType
|
||||
| ReturnType[]
|
||||
| null
|
||||
| { error?: string; errors?: DSQLErrorObject[] };
|
||||
| { error?: string; errors?: DSQLErrorObject[]; config?: ConnectionConfig };
|
||||
|
||||
/**
|
||||
* # Run Query From MySQL Connection
|
||||
@@ -19,9 +20,9 @@ type Return<ReturnType = any> =
|
||||
*/
|
||||
export default async function connDbHandler<ReturnType = any>(
|
||||
/**
|
||||
* ServerlessMySQL Connection Object
|
||||
* MariaDB Connection Pool Object
|
||||
*/
|
||||
conn?: ServerlessMysql,
|
||||
connPool?: Pool,
|
||||
/**
|
||||
* String Or `ConnDBHandlerQueryObject` Array
|
||||
*/
|
||||
@@ -33,13 +34,13 @@ export default async function connDbHandler<ReturnType = any>(
|
||||
debug?: boolean
|
||||
): Promise<Return<ReturnType>> {
|
||||
try {
|
||||
if (!conn) throw new Error("No Connection Found!");
|
||||
if (!connPool) throw new Error("No Connection Found!");
|
||||
if (!query) throw new Error("Query String Required!");
|
||||
|
||||
let queryErrorArray: DSQLErrorObject[] = [];
|
||||
|
||||
if (typeof query == "string") {
|
||||
const res = await conn.query(trimQuery(query), values);
|
||||
const res = await connPool.query(trimQuery(query), values);
|
||||
|
||||
if (debug) {
|
||||
debugLog({
|
||||
@@ -49,7 +50,11 @@ export default async function connDbHandler<ReturnType = any>(
|
||||
});
|
||||
}
|
||||
|
||||
return JSON.parse(JSON.stringify(res));
|
||||
if (Array.isArray(res)) {
|
||||
return Array.from(res);
|
||||
}
|
||||
|
||||
return res;
|
||||
} else if (typeof query == "object") {
|
||||
const resArray = [];
|
||||
|
||||
@@ -62,7 +67,7 @@ export default async function connDbHandler<ReturnType = any>(
|
||||
currentQueryError.sql = queryObj.query;
|
||||
currentQueryError.sqlValues = queryObj.values;
|
||||
|
||||
const queryObjRes = await conn.query(
|
||||
const queryObjRes = await connPool.query(
|
||||
trimQuery(queryObj.query),
|
||||
queryObj.values
|
||||
);
|
||||
@@ -75,7 +80,11 @@ export default async function connDbHandler<ReturnType = any>(
|
||||
});
|
||||
}
|
||||
|
||||
resArray.push(JSON.parse(JSON.stringify(queryObjRes)));
|
||||
if (Array.isArray(queryObjRes)) {
|
||||
resArray.push(Array.from(queryObjRes));
|
||||
} else {
|
||||
resArray.push(queryObjRes);
|
||||
}
|
||||
} catch (error: any) {
|
||||
global.ERROR_CALLBACK?.(
|
||||
`Connection DB Handler Query Error`,
|
||||
@@ -101,7 +110,7 @@ export default async function connDbHandler<ReturnType = any>(
|
||||
};
|
||||
}
|
||||
|
||||
return resArray as any;
|
||||
return resArray;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
@@ -118,12 +127,13 @@ export default async function connDbHandler<ReturnType = any>(
|
||||
|
||||
return {
|
||||
error: `Connection DB Handler Error: ${error.message}`,
|
||||
// config: conn,
|
||||
};
|
||||
} finally {
|
||||
conn?.end();
|
||||
connPool?.end();
|
||||
}
|
||||
}
|
||||
|
||||
function trimQuery(query: string) {
|
||||
return query.replace(/\n/gm, "").replace(/ {2,}/g, "").trim();
|
||||
return query.replace(/\n/gm, " ").replace(/ {2,}/g, " ").trim();
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import mysql, { ServerlessMysql } from "serverless-mysql";
|
||||
import mariadb, { Pool } from "mariadb";
|
||||
import grabDbSSL from "./backend/grabDbSSL";
|
||||
|
||||
type Param = {
|
||||
/**
|
||||
@@ -22,65 +23,11 @@ type Param = {
|
||||
/**
|
||||
* # Grab General CONNECTION for DSQL
|
||||
*/
|
||||
export default function grabDSQLConnection(param?: Param): ServerlessMysql {
|
||||
export default function grabDSQLConnection(param?: Param): Pool {
|
||||
if (global.DSQL_USE_LOCAL || param?.local) {
|
||||
return (
|
||||
global.DSQL_DB_CONN ||
|
||||
mysql({
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: param?.noDb
|
||||
? undefined
|
||||
: process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
if (param?.ro) {
|
||||
return (
|
||||
global.DSQL_READ_ONLY_DB_CONN ||
|
||||
mysql({
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_READ_ONLY_USERNAME,
|
||||
password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
if (param?.fa) {
|
||||
return (
|
||||
global.DSQL_FULL_ACCESS_DB_CONN ||
|
||||
mysql({
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_FULL_ACCESS_USERNAME,
|
||||
password: process.env.DSQL_DB_FULL_ACCESS_PASSWORD,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
global.DSQL_DB_CONN ||
|
||||
mysql({
|
||||
config: {
|
||||
mariadb.createPool({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
@@ -89,7 +36,55 @@ export default function grabDSQLConnection(param?: Param): ServerlessMysql {
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
ssl: grabDbSSL(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
if (param?.ro) {
|
||||
return (
|
||||
global.DSQL_READ_ONLY_DB_CONN ||
|
||||
mariadb.createPool({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_READ_ONLY_USERNAME,
|
||||
password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
if (param?.fa) {
|
||||
return (
|
||||
global.DSQL_FULL_ACCESS_DB_CONN ||
|
||||
mariadb.createPool({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_FULL_ACCESS_USERNAME,
|
||||
password: process.env.DSQL_DB_FULL_ACCESS_PASSWORD,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
global.DSQL_DB_CONN ||
|
||||
mariadb.createPool({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: param?.noDb ? undefined : process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import grabDbSSL from "./backend/grabDbSSL";
|
||||
import mariadb, { ConnectionConfig } from "mariadb";
|
||||
|
||||
type Params = {
|
||||
useLocal?: boolean;
|
||||
dbConfig?: ConnectionConfig;
|
||||
ssl?: boolean;
|
||||
connectionLimit?: number;
|
||||
};
|
||||
|
||||
export default function setupDSQLDb({
|
||||
useLocal,
|
||||
dbConfig,
|
||||
ssl,
|
||||
connectionLimit = 100,
|
||||
}: Params) {
|
||||
global.DSQL_USE_LOCAL = useLocal || true;
|
||||
|
||||
const pool = mariadb.createPool({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: process.env.DSQL_DB_NAME,
|
||||
charset: "utf8mb4",
|
||||
...dbConfig,
|
||||
ssl: ssl ? grabDbSSL() : undefined,
|
||||
connectionLimit,
|
||||
});
|
||||
|
||||
global.DSQL_DB_CONN = pool;
|
||||
|
||||
// let readOnlyConnection;
|
||||
|
||||
// if (addReadOnlyConn) {
|
||||
// readOnlyConnection = mariadb.createPool({
|
||||
// host: process.env.DSQL_DB_HOST,
|
||||
// user: process.env.DSQL_DB_READ_ONLY_USERNAME,
|
||||
// password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
|
||||
// database: process.env.DSQL_DB_NAME,
|
||||
// charset: "utf8mb4",
|
||||
// ...readOnlyDbConfig,
|
||||
// ssl: ssl ? grabDbSSL() : undefined,
|
||||
// connectionLimit,
|
||||
// });
|
||||
|
||||
// global.DSQL_READ_ONLY_DB_CONN = readOnlyConnection;
|
||||
// }
|
||||
|
||||
return {
|
||||
pool,
|
||||
// readOnlyConnection,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user