Updates
This commit is contained in:
parent
2118a55ab6
commit
0cd3eb418c
6
dist/index.d.ts
vendored
6
dist/index.d.ts
vendored
@ -1,8 +1,8 @@
|
||||
import { ServerlessMysql } from "serverless-mysql";
|
||||
declare global {
|
||||
var DSQL_DB_CONN: ServerlessMysql;
|
||||
var DSQL_READ_ONLY_DB_CONN: ServerlessMysql;
|
||||
var DSQL_FULL_ACCESS_DB_CONN: ServerlessMysql;
|
||||
var DSQL_DB_CONN: ServerlessMysql | undefined;
|
||||
var DSQL_READ_ONLY_DB_CONN: ServerlessMysql | undefined;
|
||||
var DSQL_FULL_ACCESS_DB_CONN: ServerlessMysql | undefined;
|
||||
}
|
||||
/**
|
||||
* Imports
|
||||
|
@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = dbHandler;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const serverError_1 = __importDefault(require("./serverError"));
|
||||
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
*/
|
||||
@ -23,6 +24,19 @@ function dbHandler(...args) {
|
||||
var _a;
|
||||
((_a = process.env.NODE_ENV) === null || _a === void 0 ? void 0 : _a.match(/dev/)) &&
|
||||
fs_1.default.appendFileSync("./.tmp/sqlQuery.sql", args[0] + "\n" + Date() + "\n\n\n", "utf8");
|
||||
const CONNECTION = 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: process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
let results;
|
||||
/**
|
||||
* Fetch from db
|
||||
@ -30,9 +44,8 @@ function dbHandler(...args) {
|
||||
* @description Fetch data from db if no cache
|
||||
*/
|
||||
try {
|
||||
const connection = global.DSQL_DB_CONN;
|
||||
results = yield new Promise((resolve, reject) => {
|
||||
connection.query(...args, (error, result, fields) => {
|
||||
CONNECTION.query(...args, (error, result, fields) => {
|
||||
if (error) {
|
||||
resolve({ error: error.message });
|
||||
}
|
||||
@ -41,7 +54,6 @@ function dbHandler(...args) {
|
||||
}
|
||||
});
|
||||
});
|
||||
yield connection.end();
|
||||
}
|
||||
catch (error) {
|
||||
fs_1.default.appendFileSync("./.tmp/dbErrorLogs.txt", JSON.stringify(error, null, 4) + "\n" + Date() + "\n\n\n", "utf8");
|
||||
@ -51,6 +63,9 @@ function dbHandler(...args) {
|
||||
message: error.message,
|
||||
});
|
||||
}
|
||||
finally {
|
||||
yield (CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end());
|
||||
}
|
||||
/**
|
||||
* Return results
|
||||
*
|
||||
|
22
dist/package-shared/shell/checkDb.js
vendored
22
dist/package-shared/shell/checkDb.js
vendored
@ -8,8 +8,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
* @async
|
||||
@ -22,16 +26,28 @@ require("dotenv").config({ path: "./../.env" });
|
||||
* @returns {Promise<object|null>}
|
||||
*/
|
||||
(() => __awaiter(void 0, void 0, void 0, function* () {
|
||||
const connection = global.DSQL_DB_CONN;
|
||||
const CONNECTION = 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: process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
try {
|
||||
const result = yield connection.query("SELECT id,first_name,last_name FROM users LIMIT 3");
|
||||
const result = yield CONNECTION.query("SELECT id,first_name,last_name FROM users LIMIT 3");
|
||||
console.log("Connection Query Success =>", result);
|
||||
}
|
||||
catch (error) {
|
||||
console.log("Connection query ERROR =>", error.message);
|
||||
}
|
||||
finally {
|
||||
connection.end();
|
||||
CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end();
|
||||
process.exit();
|
||||
}
|
||||
}))();
|
||||
|
24
dist/package-shared/shell/updateSSLUsers.js
vendored
24
dist/package-shared/shell/updateSSLUsers.js
vendored
@ -8,8 +8,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
* @async
|
||||
@ -23,9 +27,21 @@ require("dotenv").config({ path: "./../.env" });
|
||||
*/
|
||||
(() => __awaiter(void 0, void 0, void 0, function* () {
|
||||
var _a;
|
||||
const connection = global.DSQL_DB_CONN;
|
||||
const CONNECTION = 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: process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
try {
|
||||
const result = yield connection.query("SELECT user,host,ssl_type FROM mysql.user");
|
||||
const result = yield CONNECTION.query("SELECT user,host,ssl_type FROM mysql.user");
|
||||
const parsedResults = JSON.parse(JSON.stringify(result));
|
||||
for (let i = 0; i < parsedResults.length; i++) {
|
||||
const user = parsedResults[i];
|
||||
@ -38,7 +54,7 @@ require("dotenv").config({ path: "./../.env" });
|
||||
if (ssl_type === "ANY") {
|
||||
continue;
|
||||
}
|
||||
const addUserSSL = yield connection.query(`ALTER USER '${User}'@'${Host}'`);
|
||||
const addUserSSL = yield CONNECTION.query(`ALTER USER '${User}'@'${Host}'`);
|
||||
console.log(`addUserSSL => ${User}@${Host}`, addUserSSL);
|
||||
}
|
||||
}
|
||||
@ -46,7 +62,7 @@ require("dotenv").config({ path: "./../.env" });
|
||||
console.log("Connection query ERROR =>", error.message);
|
||||
}
|
||||
finally {
|
||||
connection.end();
|
||||
CONNECTION.end();
|
||||
process.exit();
|
||||
}
|
||||
}))();
|
||||
|
23
dist/package-shared/shell/utils/dbHandler.js
vendored
23
dist/package-shared/shell/utils/dbHandler.js
vendored
@ -15,20 +15,33 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = dbHandler;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
*/
|
||||
function dbHandler(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ query, values, }) {
|
||||
let connection = global.DSQL_DB_CONN;
|
||||
const CONNECTION = 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: process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
let results;
|
||||
try {
|
||||
if (query && values) {
|
||||
results = yield connection.query(query, values);
|
||||
results = yield CONNECTION.query(query, values);
|
||||
}
|
||||
else {
|
||||
results = yield connection.query(query);
|
||||
results = yield CONNECTION.query(query);
|
||||
}
|
||||
}
|
||||
catch ( /** @type {any} */error) {
|
||||
@ -37,12 +50,12 @@ function dbHandler(_a) {
|
||||
}
|
||||
console.log("ERROR in dbHandler =>", error.message);
|
||||
console.log(error);
|
||||
console.log(connection.config());
|
||||
console.log(CONNECTION.config());
|
||||
fs_1.default.appendFileSync(path_1.default.resolve(__dirname, "../.tmp/dbErrorLogs.txt"), JSON.stringify(error, null, 4) + "\n" + Date() + "\n\n\n", "utf8");
|
||||
results = null;
|
||||
}
|
||||
finally {
|
||||
yield (connection === null || connection === void 0 ? void 0 : connection.end());
|
||||
yield (CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end());
|
||||
}
|
||||
if (results) {
|
||||
return JSON.parse(JSON.stringify(results));
|
||||
|
@ -8,15 +8,31 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = DB_HANDLER;
|
||||
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
|
||||
/**
|
||||
* # DSQL user read-only DB handler
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
*/
|
||||
function DB_HANDLER(...args) {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const CONNECTION = global.DSQL_DB_CONN;
|
||||
const CONNECTION = 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: process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
try {
|
||||
if (!CONNECTION)
|
||||
throw new Error("No Connection provided to DB_HANDLER function!");
|
||||
|
@ -13,20 +13,34 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = DSQL_USER_DB_HANDLER;
|
||||
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
|
||||
const conn_db_handler_1 = __importDefault(require("../../db/conn-db-handler"));
|
||||
/**
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
function DSQL_USER_DB_HANDLER(_a) {
|
||||
return __awaiter(this, arguments, void 0, function* ({ paradigm, queryString, queryValues, }) {
|
||||
const CONNECTION = 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: process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
try {
|
||||
switch (paradigm) {
|
||||
case "Read Only":
|
||||
return yield (0, conn_db_handler_1.default)(global.DSQL_READ_ONLY_DB_CONN, queryString, queryValues);
|
||||
return yield (0, conn_db_handler_1.default)(global.DSQL_READ_ONLY_DB_CONN || CONNECTION, queryString, queryValues);
|
||||
case "Full Access":
|
||||
return yield (0, conn_db_handler_1.default)(global.DSQL_FULL_ACCESS_DB_CONN, queryString, queryValues);
|
||||
return yield (0, conn_db_handler_1.default)(global.DSQL_FULL_ACCESS_DB_CONN || CONNECTION, queryString, queryValues);
|
||||
case "FA":
|
||||
return yield (0, conn_db_handler_1.default)(global.DSQL_FULL_ACCESS_DB_CONN, queryString, queryValues);
|
||||
return yield (0, conn_db_handler_1.default)(global.DSQL_FULL_ACCESS_DB_CONN || CONNECTION, queryString, queryValues);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
@ -1,20 +1,36 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = NO_DB_HANDLER;
|
||||
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
|
||||
/**
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
function NO_DB_HANDLER(...args) {
|
||||
const NO_DB = global.DSQL_DB_CONN;
|
||||
const CONNECTION = 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: process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
NO_DB.query(...args)
|
||||
CONNECTION.query(...args)
|
||||
.then((results) => {
|
||||
NO_DB.end();
|
||||
CONNECTION.end();
|
||||
resolve(JSON.parse(JSON.stringify(results)));
|
||||
})
|
||||
.catch((err) => {
|
||||
NO_DB.end();
|
||||
CONNECTION.end();
|
||||
resolve({
|
||||
error: err.message,
|
||||
sql: err.sql,
|
||||
@ -28,4 +44,7 @@ function NO_DB_HANDLER(...args) {
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
finally {
|
||||
CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end();
|
||||
}
|
||||
}
|
||||
|
@ -1,20 +1,36 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.default = ROOT_DB_HANDLER;
|
||||
const serverless_mysql_1 = __importDefault(require("serverless-mysql"));
|
||||
/**
|
||||
* # Root DB handler
|
||||
*/
|
||||
function ROOT_DB_HANDLER(...args) {
|
||||
const NO_DB = global.DSQL_DB_CONN;
|
||||
const CONNECTION = 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: process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
NO_DB.query(...args)
|
||||
CONNECTION.query(...args)
|
||||
.then((results) => {
|
||||
NO_DB.end();
|
||||
CONNECTION.end();
|
||||
resolve(JSON.parse(JSON.stringify(results)));
|
||||
})
|
||||
.catch((err) => {
|
||||
NO_DB.end();
|
||||
CONNECTION.end();
|
||||
resolve({
|
||||
error: err.message,
|
||||
sql: err.sql,
|
||||
@ -28,4 +44,7 @@ function ROOT_DB_HANDLER(...args) {
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
finally {
|
||||
CONNECTION === null || CONNECTION === void 0 ? void 0 : CONNECTION.end();
|
||||
}
|
||||
}
|
||||
|
6
index.ts
6
index.ts
@ -1,8 +1,8 @@
|
||||
import { ServerlessMysql } from "serverless-mysql";
|
||||
declare global {
|
||||
var DSQL_DB_CONN: ServerlessMysql;
|
||||
var DSQL_READ_ONLY_DB_CONN: ServerlessMysql;
|
||||
var DSQL_FULL_ACCESS_DB_CONN: ServerlessMysql;
|
||||
var DSQL_DB_CONN: ServerlessMysql | undefined;
|
||||
var DSQL_READ_ONLY_DB_CONN: ServerlessMysql | undefined;
|
||||
var DSQL_FULL_ACCESS_DB_CONN: ServerlessMysql | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,5 +1,6 @@
|
||||
import fs from "fs";
|
||||
import serverError from "./serverError";
|
||||
import mysql from "serverless-mysql";
|
||||
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
@ -12,6 +13,21 @@ export default async function dbHandler(...args: any[]) {
|
||||
"utf8"
|
||||
);
|
||||
|
||||
const CONNECTION =
|
||||
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: process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
|
||||
let results;
|
||||
|
||||
/**
|
||||
@ -20,10 +36,8 @@ export default async function dbHandler(...args: any[]) {
|
||||
* @description Fetch data from db if no cache
|
||||
*/
|
||||
try {
|
||||
const connection = global.DSQL_DB_CONN;
|
||||
|
||||
results = await new Promise((resolve, reject) => {
|
||||
connection.query(
|
||||
CONNECTION.query(
|
||||
...args,
|
||||
(error: any, result: any, fields: any) => {
|
||||
if (error) {
|
||||
@ -34,8 +48,6 @@ export default async function dbHandler(...args: any[]) {
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
await connection.end();
|
||||
} catch (error: any) {
|
||||
fs.appendFileSync(
|
||||
"./.tmp/dbErrorLogs.txt",
|
||||
@ -49,6 +61,8 @@ export default async function dbHandler(...args: any[]) {
|
||||
component: "dbHandler",
|
||||
message: error.message,
|
||||
});
|
||||
} finally {
|
||||
await CONNECTION?.end();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,6 +1,5 @@
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
import mysql from "serverless-mysql";
|
||||
import grabDbSSL from "../utils/backend/grabDbSSL";
|
||||
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
@ -14,17 +13,30 @@ import grabDbSSL from "../utils/backend/grabDbSSL";
|
||||
* @returns {Promise<object|null>}
|
||||
*/
|
||||
(async () => {
|
||||
const connection = global.DSQL_DB_CONN;
|
||||
const CONNECTION =
|
||||
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: process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await connection.query(
|
||||
const result = await CONNECTION.query(
|
||||
"SELECT id,first_name,last_name FROM users LIMIT 3"
|
||||
);
|
||||
console.log("Connection Query Success =>", result);
|
||||
} catch (error: any) {
|
||||
console.log("Connection query ERROR =>", error.message);
|
||||
} finally {
|
||||
connection.end();
|
||||
CONNECTION?.end();
|
||||
process.exit();
|
||||
}
|
||||
})();
|
||||
|
@ -1,5 +1,4 @@
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
import grabDbSSL from "../utils/backend/grabDbSSL";
|
||||
import mysql from "serverless-mysql";
|
||||
|
||||
/**
|
||||
@ -14,10 +13,23 @@ import mysql from "serverless-mysql";
|
||||
* @returns {Promise<object|null>}
|
||||
*/
|
||||
(async () => {
|
||||
const connection = global.DSQL_DB_CONN;
|
||||
const CONNECTION =
|
||||
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: process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await connection.query(
|
||||
const result = await CONNECTION.query(
|
||||
"SELECT user,host,ssl_type FROM mysql.user"
|
||||
);
|
||||
const parsedResults = JSON.parse(JSON.stringify(result));
|
||||
@ -39,7 +51,7 @@ import mysql from "serverless-mysql";
|
||||
continue;
|
||||
}
|
||||
|
||||
const addUserSSL = await connection.query(
|
||||
const addUserSSL = await CONNECTION.query(
|
||||
`ALTER USER '${User}'@'${Host}'`
|
||||
);
|
||||
|
||||
@ -48,7 +60,7 @@ import mysql from "serverless-mysql";
|
||||
} catch (error: any) {
|
||||
console.log("Connection query ERROR =>", error.message);
|
||||
} finally {
|
||||
connection.end();
|
||||
CONNECTION.end();
|
||||
process.exit();
|
||||
}
|
||||
})();
|
||||
|
@ -17,15 +17,28 @@ export default async function dbHandler({
|
||||
query,
|
||||
values,
|
||||
}: Param): Promise<any[] | object | null> {
|
||||
let connection = global.DSQL_DB_CONN;
|
||||
const CONNECTION =
|
||||
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: process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
|
||||
let results;
|
||||
|
||||
try {
|
||||
if (query && values) {
|
||||
results = await connection.query(query, values);
|
||||
results = await CONNECTION.query(query, values);
|
||||
} else {
|
||||
results = await connection.query(query);
|
||||
results = await CONNECTION.query(query);
|
||||
}
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
if (process.env.FIRST_RUN) {
|
||||
@ -34,7 +47,7 @@ export default async function dbHandler({
|
||||
|
||||
console.log("ERROR in dbHandler =>", error.message);
|
||||
console.log(error);
|
||||
console.log(connection.config());
|
||||
console.log(CONNECTION.config());
|
||||
|
||||
fs.appendFileSync(
|
||||
path.resolve(__dirname, "../.tmp/dbErrorLogs.txt"),
|
||||
@ -43,7 +56,7 @@ export default async function dbHandler({
|
||||
);
|
||||
results = null;
|
||||
} finally {
|
||||
await connection?.end();
|
||||
await CONNECTION?.end();
|
||||
}
|
||||
|
||||
if (results) {
|
||||
|
@ -1,9 +1,24 @@
|
||||
import mysql from "serverless-mysql";
|
||||
|
||||
/**
|
||||
* # DSQL user read-only DB handler
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
*/
|
||||
export default async function DB_HANDLER(...args: any[]) {
|
||||
const CONNECTION = global.DSQL_DB_CONN;
|
||||
const CONNECTION =
|
||||
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: process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
if (!CONNECTION)
|
||||
|
@ -1,3 +1,4 @@
|
||||
import mysql from "serverless-mysql";
|
||||
import connDbHandler from "../../db/conn-db-handler";
|
||||
|
||||
type Param = {
|
||||
@ -14,25 +15,40 @@ export default async function DSQL_USER_DB_HANDLER({
|
||||
queryString,
|
||||
queryValues,
|
||||
}: Param) {
|
||||
const CONNECTION =
|
||||
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: process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
switch (paradigm) {
|
||||
case "Read Only":
|
||||
return await connDbHandler(
|
||||
global.DSQL_READ_ONLY_DB_CONN,
|
||||
global.DSQL_READ_ONLY_DB_CONN || CONNECTION,
|
||||
queryString,
|
||||
queryValues
|
||||
);
|
||||
|
||||
case "Full Access":
|
||||
return await connDbHandler(
|
||||
global.DSQL_FULL_ACCESS_DB_CONN,
|
||||
global.DSQL_FULL_ACCESS_DB_CONN || CONNECTION,
|
||||
queryString,
|
||||
queryValues
|
||||
);
|
||||
|
||||
case "FA":
|
||||
return await connDbHandler(
|
||||
global.DSQL_FULL_ACCESS_DB_CONN,
|
||||
global.DSQL_FULL_ACCESS_DB_CONN || CONNECTION,
|
||||
queryString,
|
||||
queryValues
|
||||
);
|
||||
|
@ -5,17 +5,30 @@ import grabDbSSL from "../grabDbSSL";
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
export default function NO_DB_HANDLER(...args: any[]) {
|
||||
const NO_DB = global.DSQL_DB_CONN;
|
||||
const CONNECTION =
|
||||
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: process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
NO_DB.query(...args)
|
||||
CONNECTION.query(...args)
|
||||
.then((results) => {
|
||||
NO_DB.end();
|
||||
CONNECTION.end();
|
||||
resolve(JSON.parse(JSON.stringify(results)));
|
||||
})
|
||||
.catch((err) => {
|
||||
NO_DB.end();
|
||||
CONNECTION.end();
|
||||
resolve({
|
||||
error: err.message,
|
||||
sql: err.sql,
|
||||
@ -27,5 +40,7 @@ export default function NO_DB_HANDLER(...args: any[]) {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
} finally {
|
||||
CONNECTION?.end();
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,33 @@
|
||||
import mysql from "serverless-mysql";
|
||||
|
||||
/**
|
||||
* # Root DB handler
|
||||
*/
|
||||
export default function ROOT_DB_HANDLER(...args: any[]) {
|
||||
const NO_DB = global.DSQL_DB_CONN;
|
||||
const CONNECTION =
|
||||
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: process.env.DSQL_DB_NAME,
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
return new Promise((resolve, reject) => {
|
||||
NO_DB.query(...args)
|
||||
CONNECTION.query(...args)
|
||||
.then((results) => {
|
||||
NO_DB.end();
|
||||
CONNECTION.end();
|
||||
resolve(JSON.parse(JSON.stringify(results)));
|
||||
})
|
||||
.catch((err) => {
|
||||
NO_DB.end();
|
||||
CONNECTION.end();
|
||||
resolve({
|
||||
error: err.message,
|
||||
sql: err.sql,
|
||||
@ -24,5 +39,7 @@ export default function ROOT_DB_HANDLER(...args: any[]) {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
} finally {
|
||||
CONNECTION?.end();
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@moduletrace/datasquirel",
|
||||
"version": "3.4.7",
|
||||
"version": "3.4.8",
|
||||
"description": "Cloud-based SQL data management tool",
|
||||
"main": "dist/index.js",
|
||||
"bin": {
|
||||
|
Loading…
Reference in New Issue
Block a user