Updates
This commit is contained in:
parent
3597b11342
commit
b38ddc9f21
17
dist/index.d.ts
vendored
17
dist/index.d.ts
vendored
@ -1,8 +1,8 @@
|
||||
import { ServerlessMysql } from "serverless-mysql";
|
||||
import type { Pool } from "mariadb";
|
||||
declare global {
|
||||
var DSQL_DB_CONN: ServerlessMysql | undefined;
|
||||
var DSQL_READ_ONLY_DB_CONN: ServerlessMysql | undefined;
|
||||
var DSQL_FULL_ACCESS_DB_CONN: ServerlessMysql | undefined;
|
||||
var DSQL_DB_CONN: Pool | undefined;
|
||||
var DSQL_READ_ONLY_DB_CONN: Pool | undefined;
|
||||
var DSQL_FULL_ACCESS_DB_CONN: Pool | undefined;
|
||||
var DSQL_USE_LOCAL: boolean | undefined;
|
||||
var ERROR_CALLBACK: ErrorCallback | undefined;
|
||||
}
|
||||
@ -37,6 +37,7 @@ import dsqlMethodCrud from "./package-shared/utils/data-fetching/method-crud";
|
||||
import debugLog from "./package-shared/utils/logging/debug-log";
|
||||
import { ErrorCallback } from "./package-shared/types";
|
||||
import parseEnv from "./package-shared/utils/parse-env";
|
||||
import setup from "./package-shared/utils/setup-db";
|
||||
/**
|
||||
* Main Export
|
||||
*/
|
||||
@ -141,13 +142,17 @@ declare const datasquirel: {
|
||||
};
|
||||
/**
|
||||
* Run Crud actions `get`, `insert`, `update`, `delete`
|
||||
* @note *Requires global variables `DSQL_USE_LOCAL` and `DSQL_DB_CONN`
|
||||
* @description *Requires global variables `DSQL_USE_LOCAL` and `DSQL_DB_CONN`
|
||||
*/
|
||||
crud: typeof dsqlCrud;
|
||||
/**
|
||||
* Run Crud based on request Methods `GET`, `POST`, `PUT`, `PATCH`
|
||||
* @note *Requires global variables `DSQL_USE_LOCAL` and `DSQL_DB_CONN`
|
||||
* @description *Requires global variables `DSQL_USE_LOCAL` and `DSQL_DB_CONN`
|
||||
*/
|
||||
methodCrud: typeof dsqlMethodCrud;
|
||||
/**
|
||||
* Setup Database
|
||||
*/
|
||||
setup: typeof setup;
|
||||
};
|
||||
export default datasquirel;
|
||||
|
9
dist/index.js
vendored
9
dist/index.js
vendored
@ -36,6 +36,7 @@ const debug_log_1 = __importDefault(require("./package-shared/utils/logging/debu
|
||||
const parse_env_1 = __importDefault(require("./package-shared/utils/parse-env"));
|
||||
const crud_2 = __importDefault(require("./package-shared/api/crud"));
|
||||
const media_1 = __importDefault(require("./package-shared/api/media"));
|
||||
const setup_db_1 = __importDefault(require("./package-shared/utils/setup-db"));
|
||||
/**
|
||||
* User Functions Object
|
||||
*/
|
||||
@ -106,13 +107,17 @@ const datasquirel = {
|
||||
},
|
||||
/**
|
||||
* Run Crud actions `get`, `insert`, `update`, `delete`
|
||||
* @note *Requires global variables `DSQL_USE_LOCAL` and `DSQL_DB_CONN`
|
||||
* @description *Requires global variables `DSQL_USE_LOCAL` and `DSQL_DB_CONN`
|
||||
*/
|
||||
crud: crud_1.default,
|
||||
/**
|
||||
* Run Crud based on request Methods `GET`, `POST`, `PUT`, `PATCH`
|
||||
* @note *Requires global variables `DSQL_USE_LOCAL` and `DSQL_DB_CONN`
|
||||
* @description *Requires global variables `DSQL_USE_LOCAL` and `DSQL_DB_CONN`
|
||||
*/
|
||||
methodCrud: method_crud_1.default,
|
||||
/**
|
||||
* Setup Database
|
||||
*/
|
||||
setup: setup_db_1.default,
|
||||
};
|
||||
exports.default = datasquirel;
|
||||
|
@ -43,7 +43,6 @@ function dbHandler(_a) {
|
||||
if (!noErrorLogs) {
|
||||
console.log("ERROR in dbHandler =>", error.message);
|
||||
console.log(error);
|
||||
console.log(CONNECTION.config());
|
||||
const tmpFolder = path_1.default.resolve(process.cwd(), "./.tmp");
|
||||
if (!fs_1.default.existsSync(tmpFolder))
|
||||
fs_1.default.mkdirSync(tmpFolder, { recursive: true });
|
||||
|
@ -13,18 +13,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = suDbHandler;
|
||||
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
|
||||
const grabDbSSL_1 = __importDefault(require("../../utils/backend/grabDbSSL"));
|
||||
const conn_db_handler_1 = __importDefault(require("../../utils/db/conn-db-handler"));
|
||||
const mariadb_1 = __importDefault(require("mariadb"));
|
||||
function suDbHandler(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ query, database, user, values, }) {
|
||||
const connection = (0, serverless_mysql_1.default)({
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: database,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
const connection = mariadb_1.default.createPool({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: database,
|
||||
charset: "utf8mb4",
|
||||
ssl: (0, grabDbSSL_1.default)(),
|
||||
});
|
||||
const results = yield (0, conn_db_handler_1.default)(connection, query);
|
||||
return results;
|
||||
|
@ -13,20 +13,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = userDbHandler;
|
||||
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
|
||||
const mariadb_1 = __importDefault(require("mariadb"));
|
||||
const grab_mariadb_main_user_for_user_1 = __importDefault(require("./grab-mariadb-main-user-for-user"));
|
||||
const conn_db_handler_1 = __importDefault(require("../../utils/db/conn-db-handler"));
|
||||
const grabDbSSL_1 = __importDefault(require("../../utils/backend/grabDbSSL"));
|
||||
function userDbHandler(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ query, user, database, debug, tableSchema, values, }) {
|
||||
const { fullName, host, username, password } = yield (0, grab_mariadb_main_user_for_user_1.default)({ user });
|
||||
const connection = (0, serverless_mysql_1.default)({
|
||||
config: {
|
||||
host,
|
||||
user: username,
|
||||
password: password,
|
||||
database: database,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
const connection = mariadb_1.default.createPool({
|
||||
host,
|
||||
user: username,
|
||||
password: password,
|
||||
database: database,
|
||||
charset: "utf8mb4",
|
||||
ssl: (0, grabDbSSL_1.default)(),
|
||||
});
|
||||
const results = yield (0, conn_db_handler_1.default)(connection, query);
|
||||
return results;
|
||||
|
@ -31,7 +31,6 @@ function varDatabaseDbHandler(_a) {
|
||||
if (debug) {
|
||||
console.log(`varDatabaseDbHandler:query:`, queryString);
|
||||
console.log(`varDatabaseDbHandler:values:`, queryValuesArray);
|
||||
console.log(`varDatabaseDbHandler:CONNECTION:`, CONNECTION.getConfig());
|
||||
}
|
||||
let results = yield (0, conn_db_handler_1.default)(CONNECTION, queryString, queryValuesArray);
|
||||
if (debug) {
|
||||
|
1
dist/package-shared/types/index.d.ts
vendored
1
dist/package-shared/types/index.d.ts
vendored
@ -1714,6 +1714,7 @@ export type SiteConfigMariadbServer = {
|
||||
root: SiteConfigMariadbServerUser;
|
||||
replication: SiteConfigMariadbServerUser;
|
||||
};
|
||||
ssl_dir?: string;
|
||||
};
|
||||
export type SiteConfigMariadbServerUser = {
|
||||
user: string;
|
||||
|
@ -2,4 +2,4 @@
|
||||
* # DSQL user read-only DB handler
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
*/
|
||||
export default function DB_HANDLER(...args: any[]): Promise<any>;
|
||||
export default function DB_HANDLER(query: string, values?: any[]): Promise<any>;
|
||||
|
@ -18,14 +18,14 @@ const grab_dsql_connection_1 = __importDefault(require("../../grab-dsql-connecti
|
||||
* # DSQL user read-only DB handler
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
*/
|
||||
function DB_HANDLER(...args) {
|
||||
function DB_HANDLER(query, values) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
var _a;
|
||||
const CONNECTION = (0, grab_dsql_connection_1.default)();
|
||||
try {
|
||||
if (!CONNECTION)
|
||||
throw new Error("No Connection provided to DB_HANDLER function!");
|
||||
const results = yield CONNECTION.query(...args);
|
||||
const results = yield CONNECTION.query(query, values);
|
||||
return JSON.parse(JSON.stringify(results));
|
||||
}
|
||||
catch (error) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
/**
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
export default function LOCAL_DB_HANDLER(...args: any[]): Promise<any>;
|
||||
export default function LOCAL_DB_HANDLER(query: string, values?: any[]): Promise<any>;
|
||||
|
@ -17,12 +17,12 @@ const grab_dsql_connection_1 = __importDefault(require("../../grab-dsql-connecti
|
||||
/**
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
function LOCAL_DB_HANDLER(...args) {
|
||||
function LOCAL_DB_HANDLER(query, values) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
var _a;
|
||||
const MASTER = (0, grab_dsql_connection_1.default)();
|
||||
try {
|
||||
const results = yield MASTER.query(...args);
|
||||
const results = yield MASTER.query(query, values);
|
||||
return JSON.parse(JSON.stringify(results));
|
||||
}
|
||||
catch (error) {
|
||||
|
@ -1,7 +1,7 @@
|
||||
/**
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
export default function NO_DB_HANDLER(...args: any[]): Promise<unknown> | {
|
||||
export default function NO_DB_HANDLER(query: string, values?: any[]): Promise<unknown> | {
|
||||
success: boolean;
|
||||
error: any;
|
||||
};
|
||||
|
@ -8,12 +8,12 @@ const grab_dsql_connection_1 = __importDefault(require("../../grab-dsql-connecti
|
||||
/**
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
function NO_DB_HANDLER(...args) {
|
||||
function NO_DB_HANDLER(query, values) {
|
||||
var _a;
|
||||
const CONNECTION = (0, grab_dsql_connection_1.default)();
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
CONNECTION.query(...args)
|
||||
CONNECTION.query(query, values)
|
||||
.then((results) => {
|
||||
CONNECTION.end();
|
||||
resolve(JSON.parse(JSON.stringify(results)));
|
||||
|
@ -1,7 +1,7 @@
|
||||
/**
|
||||
* # Root DB handler
|
||||
*/
|
||||
export default function ROOT_DB_HANDLER(...args: any[]): Promise<unknown> | {
|
||||
export default function ROOT_DB_HANDLER(query: string, values?: any[]): Promise<unknown> | {
|
||||
success: boolean;
|
||||
error: any;
|
||||
};
|
||||
|
@ -8,12 +8,12 @@ const grab_dsql_connection_1 = __importDefault(require("../../grab-dsql-connecti
|
||||
/**
|
||||
* # Root DB handler
|
||||
*/
|
||||
function ROOT_DB_HANDLER(...args) {
|
||||
function ROOT_DB_HANDLER(query, values) {
|
||||
var _a;
|
||||
const CONNECTION = (0, grab_dsql_connection_1.default)();
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
CONNECTION.query(...args)
|
||||
CONNECTION.query(query, values)
|
||||
.then((results) => {
|
||||
CONNECTION.end();
|
||||
resolve(JSON.parse(JSON.stringify(results)));
|
||||
|
@ -1,8 +1,7 @@
|
||||
type Return = string | (import("tls").SecureContextOptions & {
|
||||
rejectUnauthorized?: boolean | undefined;
|
||||
}) | undefined;
|
||||
import type { ConnectionConfig } from "mariadb";
|
||||
type Return = ConnectionConfig["ssl"] | undefined;
|
||||
/**
|
||||
* # Grall SSL
|
||||
* # Grab SSL
|
||||
*/
|
||||
export default function grabDbSSL(): Return;
|
||||
export {};
|
||||
|
13
dist/package-shared/utils/backend/grabDbSSL.js
vendored
13
dist/package-shared/utils/backend/grabDbSSL.js
vendored
@ -5,23 +5,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = grabDbSSL;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const grab_dir_names_1 = __importDefault(require("./names/grab-dir-names"));
|
||||
/**
|
||||
* # Grall SSL
|
||||
* # Grab SSL
|
||||
*/
|
||||
function grabDbSSL() {
|
||||
const SSL_DIR = process.env.DSQL_SSL_DIR;
|
||||
if (!(SSL_DIR === null || SSL_DIR === void 0 ? void 0 : SSL_DIR.match(/./))) {
|
||||
const { maxscaleSSLDir } = (0, grab_dir_names_1.default)();
|
||||
if (!(maxscaleSSLDir === null || maxscaleSSLDir === void 0 ? void 0 : maxscaleSSLDir.match(/./))) {
|
||||
return undefined;
|
||||
}
|
||||
const caFilePath = `${SSL_DIR}/ca-cert.pem`;
|
||||
const caFilePath = `${maxscaleSSLDir}/ca-cert.pem`;
|
||||
if (!fs_1.default.existsSync(caFilePath)) {
|
||||
console.log(`${caFilePath} does not exist`);
|
||||
return undefined;
|
||||
}
|
||||
return {
|
||||
ca: fs_1.default.readFileSync(`${SSL_DIR}/ca-cert.pem`),
|
||||
// key: fs.readFileSync(`${SSL_DIR}/client-key.pem`),
|
||||
// cert: fs.readFileSync(`${SSL_DIR}/client-cert.pem`),
|
||||
ca: fs_1.default.readFileSync(`${maxscaleSSLDir}/ca-cert.pem`),
|
||||
rejectUnauthorized: false,
|
||||
};
|
||||
}
|
||||
|
@ -65,5 +65,9 @@ export default function grabDirNames(param?: Param): {
|
||||
sqlBackupDirName: string;
|
||||
schemasBackupDirName: string;
|
||||
userMainShemaJSONFilePath: string | undefined;
|
||||
maxscaleSSLDir: string;
|
||||
mainDBSSLDir: string;
|
||||
replica1DBSSLDir: string;
|
||||
replica2DBSSLDir: string;
|
||||
};
|
||||
export {};
|
||||
|
@ -20,6 +20,10 @@ function grabDirNames(param) {
|
||||
const publicSSLDir = path_1.default.join(publicDir, "documents", "ssl");
|
||||
const appSSLDir = path_1.default.join(appDir, "ssl");
|
||||
const mainSSLDir = path_1.default.join(DATA_DIR, "ssl");
|
||||
const maxscaleSSLDir = path_1.default.join(mainSSLDir, "maxscale");
|
||||
const mainDBSSLDir = path_1.default.join(mainSSLDir, "main");
|
||||
const replica1DBSSLDir = path_1.default.join(mainSSLDir, "replica-1");
|
||||
const replica2DBSSLDir = path_1.default.join(mainSSLDir, "replica-2");
|
||||
const privateDataDir = path_1.default.join(DATA_DIR, "private");
|
||||
/**
|
||||
* # DB Dir names
|
||||
@ -175,5 +179,9 @@ function grabDirNames(param) {
|
||||
sqlBackupDirName,
|
||||
schemasBackupDirName,
|
||||
userMainShemaJSONFilePath,
|
||||
maxscaleSSLDir,
|
||||
mainDBSSLDir,
|
||||
replica1DBSSLDir,
|
||||
replica2DBSSLDir,
|
||||
};
|
||||
}
|
||||
|
12
dist/package-shared/utils/backend/names/grab-docker-stack-services-names.d.ts
vendored
Normal file
12
dist/package-shared/utils/backend/names/grab-docker-stack-services-names.d.ts
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
export default function grabDockerStackServicesNames(): {
|
||||
deploymentName: string;
|
||||
maxScaleServiceName: string;
|
||||
dbServiceName: string;
|
||||
dbCronServiceName: string;
|
||||
postDbSetupServiceName: string;
|
||||
webAppServiceName: string;
|
||||
webAppCronServiceName: string;
|
||||
webAppPostDbSetupServiceName: string;
|
||||
dbReplica1ServiceName: string;
|
||||
dbReplica2ServiceName: string;
|
||||
};
|
27
dist/package-shared/utils/backend/names/grab-docker-stack-services-names.js
vendored
Normal file
27
dist/package-shared/utils/backend/names/grab-docker-stack-services-names.js
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = grabDockerStackServicesNames;
|
||||
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,
|
||||
};
|
||||
}
|
@ -2,5 +2,7 @@ export default function grabIPAddresses(): {
|
||||
webAppIP: string;
|
||||
appCronIP: string;
|
||||
maxScaleIP: string;
|
||||
mainDBIP: string;
|
||||
localHostIP: string;
|
||||
globalIPPrefix: string;
|
||||
};
|
||||
|
@ -11,5 +11,14 @@ function grabIPAddresses() {
|
||||
const webAppIP = `${globalIPPrefix}.${web}`;
|
||||
const appCronIP = `${globalIPPrefix}.${cron}`;
|
||||
const maxScaleIP = `${globalIPPrefix}.${maxscale}`;
|
||||
return { webAppIP, appCronIP, maxScaleIP, globalIPPrefix };
|
||||
const mainDBIP = `${globalIPPrefix}.${db}`;
|
||||
const localHostIP = `${globalIPPrefix}.1`;
|
||||
return {
|
||||
webAppIP,
|
||||
appCronIP,
|
||||
maxScaleIP,
|
||||
mainDBIP,
|
||||
localHostIP,
|
||||
globalIPPrefix,
|
||||
};
|
||||
}
|
||||
|
@ -1,12 +1,13 @@
|
||||
import { ServerlessMysql } from "serverless-mysql";
|
||||
import { DSQLErrorObject } from "../../types";
|
||||
import type { ConnectionConfig, Pool } from "mariadb";
|
||||
export type ConnDBHandlerQueryObject = {
|
||||
query: string;
|
||||
values?: (string | number | undefined)[];
|
||||
};
|
||||
type Return<ReturnType = any> = ReturnType | null | {
|
||||
type Return<ReturnType = any> = ReturnType | ReturnType[] | null | {
|
||||
error?: string;
|
||||
errors?: DSQLErrorObject[];
|
||||
config?: ConnectionConfig;
|
||||
};
|
||||
/**
|
||||
* # Run Query From MySQL Connection
|
||||
@ -15,9 +16,9 @@ type Return<ReturnType = any> = ReturnType | null | {
|
||||
*/
|
||||
export default function connDbHandler<ReturnType = any>(
|
||||
/**
|
||||
* ServerlessMySQL Connection Object
|
||||
* MariaDB Connection Pool Object
|
||||
*/
|
||||
conn?: ServerlessMysql,
|
||||
connPool?: Pool,
|
||||
/**
|
||||
* String Or `ConnDBHandlerQueryObject` Array
|
||||
*/
|
||||
|
27
dist/package-shared/utils/db/conn-db-handler.js
vendored
27
dist/package-shared/utils/db/conn-db-handler.js
vendored
@ -21,9 +21,9 @@ const debug_log_1 = __importDefault(require("../logging/debug-log"));
|
||||
*/
|
||||
function connDbHandler(
|
||||
/**
|
||||
* ServerlessMySQL Connection Object
|
||||
* MariaDB Connection Pool Object
|
||||
*/
|
||||
conn,
|
||||
connPool,
|
||||
/**
|
||||
* String Or `ConnDBHandlerQueryObject` Array
|
||||
*/
|
||||
@ -35,13 +35,13 @@ values, debug) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
var _a, _b;
|
||||
try {
|
||||
if (!conn)
|
||||
if (!connPool)
|
||||
throw new Error("No Connection Found!");
|
||||
if (!query)
|
||||
throw new Error("Query String Required!");
|
||||
let queryErrorArray = [];
|
||||
if (typeof query == "string") {
|
||||
const res = yield conn.query(trimQuery(query), values);
|
||||
const res = yield connPool.query(trimQuery(query), values);
|
||||
if (debug) {
|
||||
(0, debug_log_1.default)({
|
||||
log: res,
|
||||
@ -49,7 +49,10 @@ values, debug) {
|
||||
label: "res",
|
||||
});
|
||||
}
|
||||
return JSON.parse(JSON.stringify(res));
|
||||
if (Array.isArray(res)) {
|
||||
return Array.from(res);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
else if (typeof query == "object") {
|
||||
const resArray = [];
|
||||
@ -59,7 +62,7 @@ values, debug) {
|
||||
const queryObj = query[i];
|
||||
currentQueryError.sql = queryObj.query;
|
||||
currentQueryError.sqlValues = queryObj.values;
|
||||
const queryObjRes = yield conn.query(trimQuery(queryObj.query), queryObj.values);
|
||||
const queryObjRes = yield connPool.query(trimQuery(queryObj.query), queryObj.values);
|
||||
if (debug) {
|
||||
(0, debug_log_1.default)({
|
||||
log: queryObjRes,
|
||||
@ -67,7 +70,12 @@ values, debug) {
|
||||
label: "queryObjRes",
|
||||
});
|
||||
}
|
||||
resArray.push(JSON.parse(JSON.stringify(queryObjRes)));
|
||||
if (Array.isArray(queryObjRes)) {
|
||||
resArray.push(Array.from(queryObjRes));
|
||||
}
|
||||
else {
|
||||
resArray.push(queryObjRes);
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
(_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `Connection DB Handler Query Error`, error);
|
||||
@ -105,13 +113,14 @@ values, debug) {
|
||||
}
|
||||
return {
|
||||
error: `Connection DB Handler Error: ${error.message}`,
|
||||
// config: conn,
|
||||
};
|
||||
}
|
||||
finally {
|
||||
conn === null || conn === void 0 ? void 0 : conn.end();
|
||||
connPool === null || connPool === void 0 ? void 0 : connPool.end();
|
||||
}
|
||||
});
|
||||
}
|
||||
function trimQuery(query) {
|
||||
return query.replace(/\n/gm, "").replace(/ {2,}/g, "").trim();
|
||||
return query.replace(/\n/gm, " ").replace(/ {2,}/g, " ").trim();
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { ServerlessMysql } from "serverless-mysql";
|
||||
import { Pool } from "mariadb";
|
||||
type Param = {
|
||||
/**
|
||||
* Read Only?
|
||||
@ -20,5 +20,5 @@ type Param = {
|
||||
/**
|
||||
* # Grab General CONNECTION for DSQL
|
||||
*/
|
||||
export default function grabDSQLConnection(param?: Param): ServerlessMysql;
|
||||
export default function grabDSQLConnection(param?: Param): Pool;
|
||||
export {};
|
||||
|
@ -4,59 +4,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = grabDSQLConnection;
|
||||
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
|
||||
const mariadb_1 = __importDefault(require("mariadb"));
|
||||
const grabDbSSL_1 = __importDefault(require("./backend/grabDbSSL"));
|
||||
/**
|
||||
* # Grab General CONNECTION for DSQL
|
||||
*/
|
||||
function grabDSQLConnection(param) {
|
||||
if (global.DSQL_USE_LOCAL || (param === null || param === void 0 ? void 0 : param.local)) {
|
||||
return (global.DSQL_DB_CONN ||
|
||||
(0, serverless_mysql_1.default)({
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: (param === null || param === void 0 ? void 0 : 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 === null || param === void 0 ? void 0 : param.ro) {
|
||||
return (global.DSQL_READ_ONLY_DB_CONN ||
|
||||
(0, serverless_mysql_1.default)({
|
||||
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 === null || param === void 0 ? void 0 : param.fa) {
|
||||
return (global.DSQL_FULL_ACCESS_DB_CONN ||
|
||||
(0, serverless_mysql_1.default)({
|
||||
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 ||
|
||||
(0, serverless_mysql_1.default)({
|
||||
config: {
|
||||
mariadb_1.default.createPool({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
@ -65,6 +21,45 @@ function grabDSQLConnection(param) {
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
ssl: (0, grabDbSSL_1.default)(),
|
||||
}));
|
||||
}
|
||||
if (param === null || param === void 0 ? void 0 : param.ro) {
|
||||
return (global.DSQL_READ_ONLY_DB_CONN ||
|
||||
mariadb_1.default.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: (0, grabDbSSL_1.default)(),
|
||||
}));
|
||||
}
|
||||
if (param === null || param === void 0 ? void 0 : param.fa) {
|
||||
return (global.DSQL_FULL_ACCESS_DB_CONN ||
|
||||
mariadb_1.default.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: (0, grabDbSSL_1.default)(),
|
||||
}));
|
||||
}
|
||||
return (global.DSQL_DB_CONN ||
|
||||
mariadb_1.default.createPool({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: (param === null || param === void 0 ? void 0 : param.noDb) ? undefined : process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: (0, grabDbSSL_1.default)(),
|
||||
}));
|
||||
}
|
||||
|
11
dist/package-shared/utils/setup-db.d.ts
vendored
Normal file
11
dist/package-shared/utils/setup-db.d.ts
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
import mariadb, { ConnectionConfig } from "mariadb";
|
||||
type Params = {
|
||||
useLocal?: boolean;
|
||||
dbConfig?: ConnectionConfig;
|
||||
ssl?: boolean;
|
||||
connectionLimit?: number;
|
||||
};
|
||||
export default function setupDSQLDb({ useLocal, dbConfig, ssl, connectionLimit, }: Params): {
|
||||
pool: mariadb.Pool;
|
||||
};
|
||||
export {};
|
31
dist/package-shared/utils/setup-db.js
vendored
Normal file
31
dist/package-shared/utils/setup-db.js
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = setupDSQLDb;
|
||||
const grabDbSSL_1 = __importDefault(require("./backend/grabDbSSL"));
|
||||
const mariadb_1 = __importDefault(require("mariadb"));
|
||||
function setupDSQLDb({ useLocal, dbConfig, ssl, connectionLimit = 100, }) {
|
||||
global.DSQL_USE_LOCAL = useLocal || true;
|
||||
const pool = mariadb_1.default.createPool(Object.assign(Object.assign({ 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 ? (0, grabDbSSL_1.default)() : 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,
|
||||
};
|
||||
}
|
21
index.ts
21
index.ts
@ -1,9 +1,9 @@
|
||||
import { ServerlessMysql } from "serverless-mysql";
|
||||
import type { Pool } from "mariadb";
|
||||
|
||||
declare global {
|
||||
var DSQL_DB_CONN: ServerlessMysql | undefined;
|
||||
var DSQL_READ_ONLY_DB_CONN: ServerlessMysql | undefined;
|
||||
var DSQL_FULL_ACCESS_DB_CONN: ServerlessMysql | undefined;
|
||||
var DSQL_DB_CONN: Pool | undefined;
|
||||
var DSQL_READ_ONLY_DB_CONN: Pool | undefined;
|
||||
var DSQL_FULL_ACCESS_DB_CONN: Pool | undefined;
|
||||
var DSQL_USE_LOCAL: boolean | undefined;
|
||||
var ERROR_CALLBACK: ErrorCallback | undefined;
|
||||
}
|
||||
@ -12,10 +12,6 @@ import get from "./package-shared/actions/get";
|
||||
import post from "./package-shared/actions/post";
|
||||
import getSchema from "./package-shared/actions/get-schema";
|
||||
|
||||
import uploadImage from "./package-shared/actions/upload-image";
|
||||
import uploadFile from "./package-shared/actions/upload-file";
|
||||
import deleteFile from "./package-shared/actions/delete-file";
|
||||
|
||||
import createUser from "./package-shared/actions/users/add-user";
|
||||
import updateUser from "./package-shared/actions/users/update-user";
|
||||
import loginUser from "./package-shared/actions/users/login-user";
|
||||
@ -50,6 +46,7 @@ import { ErrorCallback } from "./package-shared/types";
|
||||
import parseEnv from "./package-shared/utils/parse-env";
|
||||
import crud from "./package-shared/api/crud";
|
||||
import media from "./package-shared/api/media";
|
||||
import setup from "./package-shared/utils/setup-db";
|
||||
|
||||
/**
|
||||
* User Functions Object
|
||||
@ -124,14 +121,18 @@ const datasquirel = {
|
||||
},
|
||||
/**
|
||||
* Run Crud actions `get`, `insert`, `update`, `delete`
|
||||
* @note *Requires global variables `DSQL_USE_LOCAL` and `DSQL_DB_CONN`
|
||||
* @description *Requires global variables `DSQL_USE_LOCAL` and `DSQL_DB_CONN`
|
||||
*/
|
||||
crud: dsqlCrud,
|
||||
/**
|
||||
* Run Crud based on request Methods `GET`, `POST`, `PUT`, `PATCH`
|
||||
* @note *Requires global variables `DSQL_USE_LOCAL` and `DSQL_DB_CONN`
|
||||
* @description *Requires global variables `DSQL_USE_LOCAL` and `DSQL_DB_CONN`
|
||||
*/
|
||||
methodCrud: dsqlMethodCrud,
|
||||
/**
|
||||
* Setup Database
|
||||
*/
|
||||
setup,
|
||||
};
|
||||
|
||||
export default datasquirel;
|
||||
|
@ -39,7 +39,6 @@ export default async function dbHandler({
|
||||
if (!noErrorLogs) {
|
||||
console.log("ERROR in dbHandler =>", error.message);
|
||||
console.log(error);
|
||||
console.log(CONNECTION.config());
|
||||
|
||||
const tmpFolder = path.resolve(process.cwd(), "./.tmp");
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
import mysql from "serverless-mysql";
|
||||
import { UserType } from "../../types";
|
||||
import grabDbSSL from "../../utils/backend/grabDbSSL";
|
||||
import connDbHandler from "../../utils/db/conn-db-handler";
|
||||
import mariadb from "mariadb";
|
||||
|
||||
type Params = {
|
||||
query?: string;
|
||||
@ -15,14 +16,13 @@ export default async function suDbHandler({
|
||||
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 connection = mariadb.createPool({
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: database,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
});
|
||||
|
||||
const results = await connDbHandler(connection, query);
|
||||
|
@ -1,7 +1,8 @@
|
||||
import mysql from "serverless-mysql";
|
||||
import mariadb from "mariadb";
|
||||
import { DSQL_TableSchemaType, UserType } from "../../types";
|
||||
import grabMariadbMainUserForUser from "./grab-mariadb-main-user-for-user";
|
||||
import connDbHandler from "../../utils/db/conn-db-handler";
|
||||
import grabDbSSL from "../../utils/backend/grabDbSSL";
|
||||
|
||||
type Params = {
|
||||
query?: string;
|
||||
@ -23,14 +24,13 @@ export default async function userDbHandler({
|
||||
const { fullName, host, username, password } =
|
||||
await grabMariadbMainUserForUser({ user });
|
||||
|
||||
const connection = mysql({
|
||||
config: {
|
||||
host,
|
||||
user: username,
|
||||
password: password,
|
||||
database: database,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
const connection = mariadb.createPool({
|
||||
host,
|
||||
user: username,
|
||||
password: password,
|
||||
database: database,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
});
|
||||
|
||||
const results = await connDbHandler(connection, query);
|
||||
|
@ -29,7 +29,6 @@ export default async function varDatabaseDbHandler({
|
||||
if (debug) {
|
||||
console.log(`varDatabaseDbHandler:query:`, queryString);
|
||||
console.log(`varDatabaseDbHandler:values:`, queryValuesArray);
|
||||
console.log(`varDatabaseDbHandler:CONNECTION:`, CONNECTION.getConfig());
|
||||
}
|
||||
|
||||
let results = await connDbHandler(
|
||||
|
@ -1,5 +1,4 @@
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
import mysql from "serverless-mysql";
|
||||
import grabDSQLConnection from "../utils/grab-dsql-connection";
|
||||
|
||||
/**
|
||||
|
@ -1,5 +1,4 @@
|
||||
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";
|
||||
|
||||
|
@ -2135,6 +2135,7 @@ export type SiteConfigMariadbServer = {
|
||||
root: SiteConfigMariadbServerUser;
|
||||
replication: SiteConfigMariadbServerUser;
|
||||
};
|
||||
ssl_dir?: string;
|
||||
};
|
||||
|
||||
export type SiteConfigMariadbServerUser = {
|
||||
|
@ -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(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
53
package-shared/utils/setup-db.ts
Normal file
53
package-shared/utils/setup-db.ts
Normal file
@ -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,
|
||||
};
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@moduletrace/datasquirel",
|
||||
"version": "4.8.6",
|
||||
"version": "4.8.7",
|
||||
"description": "Cloud-based SQL data management tool",
|
||||
"main": "dist/index.js",
|
||||
"bin": {
|
||||
@ -44,9 +44,8 @@
|
||||
"google-auth-library": "^9.15.0",
|
||||
"inquirer": "^12.5.2",
|
||||
"lodash": "^4.17.21",
|
||||
"mysql": "^2.18.1",
|
||||
"mariadb": "^3.4.4",
|
||||
"nodemailer": "^6.9.14",
|
||||
"sanitize-html": "^2.13.1",
|
||||
"serverless-mysql": "^1.5.5"
|
||||
"sanitize-html": "^2.13.1"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user