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