Updates
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
import mysql from "serverless-mysql";
|
||||
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[]) {
|
||||
const CONNECTION = global.DSQL_DB_CONN;
|
||||
const CONNECTION = grabDSQLConnection();
|
||||
|
||||
try {
|
||||
if (!CONNECTION)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import connDbHandler from "../../db/conn-db-handler";
|
||||
import grabDSQLConnection from "../../grab-dsql-connection";
|
||||
|
||||
type Param = {
|
||||
paradigm: "Full Access" | "FA" | "Read Only";
|
||||
@@ -14,122 +15,17 @@ export default async function DSQL_USER_DB_HANDLER({
|
||||
queryString,
|
||||
queryValues,
|
||||
}: Param) {
|
||||
const CONNECTION =
|
||||
paradigm == "Read Only"
|
||||
? grabDSQLConnection({ ro: true })
|
||||
: grabDSQLConnection({ fa: true });
|
||||
|
||||
try {
|
||||
switch (paradigm) {
|
||||
case "Read Only":
|
||||
return await connDbHandler(
|
||||
global.DSQL_READ_ONLY_DB_CONN,
|
||||
queryString,
|
||||
queryValues
|
||||
);
|
||||
|
||||
case "Full Access":
|
||||
return await connDbHandler(
|
||||
global.DSQL_FULL_ACCESS_DB_CONN,
|
||||
queryString,
|
||||
queryValues
|
||||
);
|
||||
|
||||
case "FA":
|
||||
return await connDbHandler(
|
||||
global.DSQL_FULL_ACCESS_DB_CONN,
|
||||
queryString,
|
||||
queryValues
|
||||
);
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
return await connDbHandler(CONNECTION, queryString, queryValues);
|
||||
} catch (error: any) {
|
||||
console.log(`DSQL_USER_DB_HANDLER Error: ${error.message}`);
|
||||
return null;
|
||||
} finally {
|
||||
CONNECTION?.end();
|
||||
}
|
||||
|
||||
// try {
|
||||
// return new Promise((resolve, reject) => {
|
||||
// const fullAccess = paradigm?.match(/full.access|^fa$/i)
|
||||
// ? true
|
||||
// : false;
|
||||
|
||||
// try {
|
||||
// if (fullAccess) {
|
||||
// DSQL_USER = mysql({
|
||||
// config: {
|
||||
// host: process.env.DSQL_DB_HOST,
|
||||
// user: process.env.DSQL_DB_FULL_ACCESS_USERNAME,
|
||||
// password: process.env.DSQL_DB_FULL_ACCESS_PASSWORD,
|
||||
// database: database,
|
||||
// ssl: grabDbSSL(),
|
||||
// },
|
||||
// });
|
||||
// } else {
|
||||
// DSQL_USER = mysql({
|
||||
// config: {
|
||||
// host: process.env.DSQL_DB_HOST,
|
||||
// user: process.env.DSQL_DB_READ_ONLY_USERNAME,
|
||||
// password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
|
||||
// database: database,
|
||||
// ssl: grabDbSSL(),
|
||||
// },
|
||||
// });
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * ### Run query Function
|
||||
// * @param {any} results
|
||||
// */
|
||||
// function runQuery(results: any) {
|
||||
// DSQL_USER.end();
|
||||
// resolve(JSON.parse(JSON.stringify(results)));
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * ### Query Error
|
||||
// * @param {any} err
|
||||
// */
|
||||
// function queryError(err: any) {
|
||||
// DSQL_USER.end();
|
||||
// resolve({
|
||||
// error: err.message,
|
||||
// queryStringGenerated: queryString,
|
||||
// queryValuesGenerated: queryValues,
|
||||
// sql: err.sql,
|
||||
// });
|
||||
// }
|
||||
|
||||
// if (
|
||||
// queryValues &&
|
||||
// Array.isArray(queryValues) &&
|
||||
// queryValues[0]
|
||||
// ) {
|
||||
// DSQL_USER.query(queryString, queryValues)
|
||||
// .then(runQuery)
|
||||
// .catch(queryError);
|
||||
// } else {
|
||||
// DSQL_USER.query(queryString)
|
||||
// .then(runQuery)
|
||||
// .catch(queryError);
|
||||
// }
|
||||
|
||||
// ////////////////////////////////////////
|
||||
// } catch (/** @type {any} */ error: any) {
|
||||
// ////////////////////////////////////////
|
||||
|
||||
// fs.appendFileSync(
|
||||
// "./.tmp/dbErrorLogs.txt",
|
||||
// error.message + "\n" + Date() + "\n\n\n",
|
||||
// "utf8"
|
||||
// );
|
||||
|
||||
// resolve({
|
||||
// error: error.message,
|
||||
// });
|
||||
// }
|
||||
// });
|
||||
// } catch (/** @type {any} */ error: any) {
|
||||
// return {
|
||||
// success: false,
|
||||
// error: error.message,
|
||||
// };
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -1,45 +1,24 @@
|
||||
import mysql from "serverless-mysql";
|
||||
import grabDbSSL from "../grabDbSSL";
|
||||
import grabDSQLConnection from "../../grab-dsql-connection";
|
||||
|
||||
/**
|
||||
* # DSQL user read-only DB handler
|
||||
*/
|
||||
export default async function LOCAL_DB_HANDLER(...args: any[]) {
|
||||
const MASTER = 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",
|
||||
ssl: grabDbSSL(),
|
||||
},
|
||||
onConnect: () => {
|
||||
console.log("Connection Successful!");
|
||||
},
|
||||
onConnectError: (/** @type {any} */ err: any) => {
|
||||
console.log("Connection Error", err.message);
|
||||
},
|
||||
onError: (/** @type {any} */ err: any) => {
|
||||
console.log("Client Error", err.message);
|
||||
},
|
||||
});
|
||||
const MASTER = grabDSQLConnection();
|
||||
|
||||
console.log("Querying ...");
|
||||
|
||||
try {
|
||||
const results = await MASTER.query(...args);
|
||||
await MASTER.end();
|
||||
|
||||
return JSON.parse(JSON.stringify(results));
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
} catch (error: any) {
|
||||
console.log("DB Error =>", error.message);
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
} finally {
|
||||
await MASTER?.end();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,22 @@
|
||||
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[]) {
|
||||
const NO_DB = global.DSQL_DB_CONN;
|
||||
const CONNECTION = grabDSQLConnection();
|
||||
|
||||
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 +28,7 @@ export default function NO_DB_HANDLER(...args: any[]) {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
} finally {
|
||||
CONNECTION?.end();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
import grabDSQLConnection from "../../grab-dsql-connection";
|
||||
|
||||
/**
|
||||
* # Root DB handler
|
||||
*/
|
||||
export default function ROOT_DB_HANDLER(...args: any[]) {
|
||||
const NO_DB = global.DSQL_DB_CONN;
|
||||
const CONNECTION = grabDSQLConnection();
|
||||
|
||||
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 +26,7 @@ export default function ROOT_DB_HANDLER(...args: any[]) {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
} finally {
|
||||
CONNECTION?.end();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
type Param = {
|
||||
dbName: string;
|
||||
userId?: string | number;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Grab Database Full Name
|
||||
*/
|
||||
export default function grabDbFullName({ dbName, userId }: Param): string {
|
||||
const sanitizedName = dbName.replace(/[^a-z0-9\_]/g, "");
|
||||
const cleanedDbName = sanitizedName.replace(/datasquirel_user_\d+_/, "");
|
||||
|
||||
if (!userId) return cleanedDbName;
|
||||
|
||||
const dbNamePrefix = `datasquirel_user_${userId}_`;
|
||||
|
||||
return dbNamePrefix + cleanedDbName;
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import mysql, { ServerlessMysql } from "serverless-mysql";
|
||||
|
||||
type Param = {
|
||||
/**
|
||||
* Read Only?
|
||||
*/
|
||||
ro?: boolean;
|
||||
/**
|
||||
* Full Access?
|
||||
*/
|
||||
fa?: boolean;
|
||||
/**
|
||||
* No Database Connection
|
||||
*/
|
||||
noDb?: boolean;
|
||||
/**
|
||||
* Is this a local connection?
|
||||
*/
|
||||
local?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Grab General CONNECTION for DSQL
|
||||
*/
|
||||
export default function grabDSQLConnection(param?: Param): ServerlessMysql {
|
||||
if (param?.ro) {
|
||||
return (
|
||||
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: {
|
||||
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",
|
||||
},
|
||||
})
|
||||
);
|
||||
}
|
||||
@@ -10,10 +10,14 @@ type GrabHostNamesReturn = {
|
||||
user_id: string | number;
|
||||
};
|
||||
|
||||
type Param = {
|
||||
userId?: string | number;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Grab Names For Query
|
||||
*/
|
||||
export default function grabHostNames(): GrabHostNamesReturn {
|
||||
export default function grabHostNames(param?: Param): GrabHostNamesReturn {
|
||||
const scheme = process.env.DSQL_HTTP_SCHEME;
|
||||
const localHost = process.env.DSQL_LOCAL_HOST;
|
||||
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
|
||||
@@ -28,6 +32,6 @@ export default function grabHostNames(): GrabHostNamesReturn {
|
||||
host: remoteHost || localHost || "datasquirel.com",
|
||||
port: remoteHostPort || localHostPort || 443,
|
||||
scheme: scheme?.match(/^http$/i) ? http : https,
|
||||
user_id: String(process.env.DSQL_API_USER_ID || 0),
|
||||
user_id: param?.userId || String(process.env.DSQL_API_USER_ID || 0),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import numberfy from "./numberfy";
|
||||
|
||||
export type GrabEncryptionKeysParam = {
|
||||
encryptionKey?: string;
|
||||
encryptionSalt?: string;
|
||||
apiKey?: string;
|
||||
algorithm?: string;
|
||||
bufferAllocSize?: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* # Grab Encryption Keys
|
||||
* @description Grab Required Encryption Keys
|
||||
*/
|
||||
export default function grabKeys(param?: GrabEncryptionKeysParam) {
|
||||
return {
|
||||
key: param?.encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD,
|
||||
keyLen: process.env.DSQL_ENCRYPTION_KEY_LENGTH
|
||||
? Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH)
|
||||
: 24,
|
||||
salt: param?.encryptionSalt || process.env.DSQL_ENCRYPTION_SALT,
|
||||
apiKey: param?.apiKey || process.env.DSQL_API_KEY,
|
||||
algorithm:
|
||||
param?.algorithm ||
|
||||
process.env.DSQL_ENCRYPTION_ALGORITHM ||
|
||||
"aes-192-cbc",
|
||||
bufferAllocSize:
|
||||
param?.bufferAllocSize ||
|
||||
(process.env.DSQL_ENCRYPTION_BUFFER_ALLOCATION_SIZE
|
||||
? numberfy(process.env.DSQL_ENCRYPTION_BUFFER_ALLOCATION_SIZE)
|
||||
: undefined) ||
|
||||
16,
|
||||
};
|
||||
}
|
||||
@@ -7,7 +7,7 @@
|
||||
* numberfy("123.456", 0) // 123
|
||||
* numberfy("123.456", 3) // 123.456
|
||||
*/
|
||||
export default function numberfy(num: any, decimals: number): number {
|
||||
export default function numberfy(num: any, decimals?: number): number {
|
||||
try {
|
||||
const numberfiedNum = Number(num);
|
||||
if (typeof numberfiedNum !== "number") return 0;
|
||||
|
||||
Reference in New Issue
Block a user