Updates
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
// @ts-check
|
||||
|
||||
const { scryptSync, createDecipheriv } = require("crypto");
|
||||
const { Buffer } = require("buffer");
|
||||
|
||||
/**
|
||||
* @param {object} param0
|
||||
* @param {string} param0.encryptedString
|
||||
* @param {string} [param0.encryptionKey]
|
||||
* @param {string} [param0.encryptionSalt]
|
||||
* @returns
|
||||
*/
|
||||
const decrypt = ({ encryptedString, encryptionKey, encryptionSalt }) => {
|
||||
if (!encryptedString?.match(/./)) {
|
||||
console.log("Encrypted string is invalid");
|
||||
return encryptedString;
|
||||
}
|
||||
|
||||
const finalEncryptionKey =
|
||||
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
|
||||
const finalEncryptionSalt =
|
||||
encryptionSalt || process.env.DSQL_ENCRYPTION_SALT;
|
||||
const finalKeyLen = process.env.DSQL_ENCRYPTION_KEY_LENGTH
|
||||
? Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH)
|
||||
: 24;
|
||||
|
||||
if (!finalEncryptionKey?.match(/.{8,}/)) {
|
||||
console.log("Decrption key is invalid");
|
||||
return encryptedString;
|
||||
}
|
||||
|
||||
if (!finalEncryptionSalt?.match(/.{8,}/)) {
|
||||
console.log("Decrption salt is invalid");
|
||||
return encryptedString;
|
||||
}
|
||||
|
||||
const algorithm = "aes-192-cbc";
|
||||
|
||||
let key = scryptSync(finalEncryptionKey, finalEncryptionSalt, finalKeyLen);
|
||||
let iv = Buffer.alloc(16, 0);
|
||||
// @ts-ignore
|
||||
const decipher = createDecipheriv(algorithm, key, iv);
|
||||
|
||||
try {
|
||||
let decrypted = decipher.update(encryptedString, "hex", "utf8");
|
||||
decrypted += decipher.final("utf8");
|
||||
return decrypted;
|
||||
} catch (/** @type {*} */ error) {
|
||||
console.log("Error in decrypting =>", error.message);
|
||||
return encryptedString;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = decrypt;
|
||||
@@ -0,0 +1,55 @@
|
||||
// @ts-check
|
||||
|
||||
const { scryptSync, createCipheriv } = require("crypto");
|
||||
const { Buffer } = require("buffer");
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {object} param0
|
||||
* @param {string} param0.data
|
||||
* @param {string} [param0.encryptionKey]
|
||||
* @param {string} [param0.encryptionSalt]
|
||||
* @returns {string | null}
|
||||
*/
|
||||
const encrypt = ({ data, encryptionKey, encryptionSalt }) => {
|
||||
if (!data?.match(/./)) {
|
||||
console.log("Encryption string is invalid");
|
||||
return data;
|
||||
}
|
||||
|
||||
const finalEncryptionKey =
|
||||
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
|
||||
const finalEncryptionSalt =
|
||||
encryptionSalt || process.env.DSQL_ENCRYPTION_SALT;
|
||||
const finalKeyLen = process.env.DSQL_ENCRYPTION_KEY_LENGTH
|
||||
? Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH)
|
||||
: 24;
|
||||
|
||||
if (!finalEncryptionKey?.match(/.{8,}/)) {
|
||||
console.log("Encryption key is invalid");
|
||||
return data;
|
||||
}
|
||||
if (!finalEncryptionSalt?.match(/.{8,}/)) {
|
||||
console.log("Encryption salt is invalid");
|
||||
return data;
|
||||
}
|
||||
|
||||
const algorithm = "aes-192-cbc";
|
||||
const password = finalEncryptionKey;
|
||||
|
||||
let key = scryptSync(password, finalEncryptionSalt, finalKeyLen);
|
||||
let iv = Buffer.alloc(16, 0);
|
||||
// @ts-ignore
|
||||
const cipher = createCipheriv(algorithm, key, iv);
|
||||
|
||||
try {
|
||||
let encrypted = cipher.update(data, "utf8", "hex");
|
||||
encrypted += cipher.final("hex");
|
||||
return encrypted;
|
||||
} catch (/** @type {*} */ error) {
|
||||
console.log("Error in encrypting =>", error.message);
|
||||
return data;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = encrypt;
|
||||
@@ -0,0 +1,5 @@
|
||||
declare function _exports({ password, encryptionKey }: {
|
||||
password: string;
|
||||
encryptionKey: string;
|
||||
}): string;
|
||||
export = _exports;
|
||||
@@ -0,0 +1,27 @@
|
||||
/** # MODULE TRACE
|
||||
======================================================================
|
||||
* Detected 4 files that call this module. The files are listed below:
|
||||
======================================================================
|
||||
* `require` Statement Found in [add-user.js] => file:///d:\GitHub\dsql\engine\user\add-user.js
|
||||
* `require` Statement Found in [login-user.js] => file:///d:\GitHub\dsql\engine\user\login-user.js
|
||||
* `require` Statement Found in [googleLogin.js] => file:///d:\GitHub\dsql\engine\user\social\utils\googleLogin.js
|
||||
* `require` Statement Found in [update-user.js] => file:///d:\GitHub\dsql\engine\user\update-user.js
|
||||
==== MODULE TRACE END ==== */
|
||||
|
||||
// @ts-check
|
||||
|
||||
const { createHmac } = require("crypto");
|
||||
|
||||
/**
|
||||
* # Hash password Function
|
||||
* @param {object} param0
|
||||
* @param {string} param0.password - Password to hash
|
||||
* @param {string} param0.encryptionKey - Encryption key
|
||||
* @returns {string}
|
||||
*/
|
||||
module.exports = function hashPassword({ password, encryptionKey }) {
|
||||
const hmac = createHmac("sha512", encryptionKey);
|
||||
hmac.update(password);
|
||||
let hashed = hmac.digest("base64");
|
||||
return hashed;
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
export = sqlDeleteGenerator;
|
||||
/**
|
||||
* @typedef {object} SQLDeleteGenReturn
|
||||
* @property {string} query
|
||||
* @property {string[]} values
|
||||
*/
|
||||
/**
|
||||
* @param {object} param0
|
||||
* @param {any} param0.data
|
||||
* @param {string} param0.tableName
|
||||
*
|
||||
* @return {SQLDeleteGenReturn | undefined}
|
||||
*/
|
||||
declare function sqlDeleteGenerator({ tableName, data }: {
|
||||
data: any;
|
||||
tableName: string;
|
||||
}): SQLDeleteGenReturn | undefined;
|
||||
declare namespace sqlDeleteGenerator {
|
||||
export { SQLDeleteGenReturn };
|
||||
}
|
||||
type SQLDeleteGenReturn = {
|
||||
query: string;
|
||||
values: string[];
|
||||
};
|
||||
@@ -0,0 +1,41 @@
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* @typedef {object} SQLDeleteGenReturn
|
||||
* @property {string} query
|
||||
* @property {string[]} values
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {object} param0
|
||||
* @param {any} param0.data
|
||||
* @param {string} param0.tableName
|
||||
*
|
||||
* @return {SQLDeleteGenReturn | undefined}
|
||||
*/
|
||||
function sqlDeleteGenerator({ tableName, data }) {
|
||||
try {
|
||||
let queryStr = `DELETE FROM ${tableName}`;
|
||||
|
||||
/** @type {string[]} */
|
||||
let deleteBatch = [];
|
||||
/** @type {string[]} */
|
||||
let queryArr = [];
|
||||
|
||||
Object.keys(data).forEach((ky) => {
|
||||
deleteBatch.push(`${ky}=?`);
|
||||
queryArr.push(data[ky]);
|
||||
});
|
||||
queryStr += ` WHERE ${deleteBatch.join(" AND ")}`;
|
||||
|
||||
return {
|
||||
query: queryStr,
|
||||
values: queryArr,
|
||||
};
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log(`SQL delete gen ERROR: ${error.message}`);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = sqlDeleteGenerator;
|
||||
@@ -0,0 +1,10 @@
|
||||
export = sqlGenerator;
|
||||
declare function sqlGenerator(Param0: {
|
||||
genObject?: import("../../../types").ServerQueryParam;
|
||||
tableName: string;
|
||||
}):
|
||||
| {
|
||||
string: string;
|
||||
values: string[];
|
||||
}
|
||||
| undefined;
|
||||
@@ -0,0 +1,194 @@
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* # SQL Query Generator
|
||||
* @description Generates an SQL Query for node module `mysql` or `serverless-mysql`
|
||||
* @type {import("../../../types").SqlGeneratorFn}
|
||||
*/
|
||||
function sqlGenerator({ tableName, genObject }) {
|
||||
if (!genObject) return undefined;
|
||||
|
||||
const finalQuery = genObject.query ? genObject.query : undefined;
|
||||
|
||||
const queryKeys = finalQuery ? Object.keys(finalQuery) : undefined;
|
||||
|
||||
/** @type {string[]} */
|
||||
const sqlSearhValues = [];
|
||||
const sqlSearhString = queryKeys?.map((field) => {
|
||||
const queryObj = finalQuery?.[field];
|
||||
if (!queryObj) return;
|
||||
|
||||
const finalFieldName = (() => {
|
||||
if (queryObj?.tableName) {
|
||||
return `${queryObj.tableName}.${field}`;
|
||||
}
|
||||
if (genObject.join) {
|
||||
return `${tableName}.${field}`;
|
||||
}
|
||||
return field;
|
||||
})();
|
||||
|
||||
let str = `${finalFieldName}=?`;
|
||||
|
||||
if (
|
||||
typeof queryObj.value == "string" ||
|
||||
typeof queryObj.value == "number"
|
||||
) {
|
||||
const valueParsed = String(queryObj.value);
|
||||
if (queryObj.equality == "LIKE") {
|
||||
str = `LOWER(${finalFieldName}) LIKE LOWER('%${valueParsed}%')`;
|
||||
} else {
|
||||
sqlSearhValues.push(valueParsed);
|
||||
}
|
||||
} else if (Array.isArray(queryObj.value)) {
|
||||
/** @type {string[]} */
|
||||
const strArray = [];
|
||||
queryObj.value.forEach((val) => {
|
||||
const valueParsed = val;
|
||||
if (queryObj.equality == "LIKE") {
|
||||
strArray.push(
|
||||
`LOWER(${finalFieldName}) LIKE LOWER('%${valueParsed}%')`
|
||||
);
|
||||
} else {
|
||||
strArray.push(`${finalFieldName} = ?`);
|
||||
sqlSearhValues.push(valueParsed);
|
||||
}
|
||||
});
|
||||
|
||||
str = "(" + strArray.join(` ${queryObj.operator || "AND"} `) + ")";
|
||||
}
|
||||
|
||||
return str;
|
||||
});
|
||||
|
||||
function generateJoinStr(
|
||||
/** @type {import("../../../types").ServerQueryParamsJoinMatchObject} */ mtch,
|
||||
/** @type {import("../../../types").ServerQueryParamsJoin} */ join
|
||||
) {
|
||||
return `${
|
||||
typeof mtch.source == "object" ? mtch.source.tableName : tableName
|
||||
}.${
|
||||
typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source
|
||||
}=${(() => {
|
||||
if (mtch.targetLiteral) {
|
||||
return `'${mtch.targetLiteral}'`;
|
||||
}
|
||||
|
||||
return `${
|
||||
typeof mtch.target == "object"
|
||||
? mtch.target.tableName
|
||||
: join.tableName
|
||||
}.${
|
||||
typeof mtch.target == "object"
|
||||
? mtch.target.fieldName
|
||||
: mtch.target
|
||||
}`;
|
||||
})()}`;
|
||||
}
|
||||
|
||||
let queryString = (() => {
|
||||
let str = "SELECT";
|
||||
if (genObject.selectFields?.[0]) {
|
||||
if (genObject.join) {
|
||||
str += ` ${genObject.selectFields
|
||||
?.map((fld) => `${tableName}.${fld}`)
|
||||
.join(",")}`;
|
||||
} else {
|
||||
str += ` ${genObject.selectFields?.join(",")}`;
|
||||
}
|
||||
} else {
|
||||
if (genObject.join) {
|
||||
str += ` ${tableName}.*`;
|
||||
} else {
|
||||
str += " *";
|
||||
}
|
||||
}
|
||||
|
||||
if (genObject.join) {
|
||||
/** @type {string[]} */
|
||||
const existingJoinTableNames = [tableName];
|
||||
|
||||
str +=
|
||||
"," +
|
||||
genObject.join
|
||||
.map((joinObj) => {
|
||||
if (existingJoinTableNames.includes(joinObj.tableName))
|
||||
return null;
|
||||
existingJoinTableNames.push(joinObj.tableName);
|
||||
|
||||
if (joinObj.selectFields) {
|
||||
return joinObj.selectFields
|
||||
.map((slFld) => {
|
||||
if (typeof slFld == "string") {
|
||||
return `${joinObj.tableName}.${slFld}`;
|
||||
} else if (typeof slFld == "object") {
|
||||
let aliasSlctFld = `${joinObj.tableName}.${slFld.field}`;
|
||||
if (slFld.alias)
|
||||
aliasSlctFld += ` as ${slFld.alias}`;
|
||||
return aliasSlctFld;
|
||||
}
|
||||
})
|
||||
.join(",");
|
||||
} else {
|
||||
return `${joinObj.tableName}.*`;
|
||||
}
|
||||
})
|
||||
.filter((_) => Boolean(_))
|
||||
.join(",");
|
||||
}
|
||||
|
||||
str += ` FROM ${tableName}`;
|
||||
|
||||
if (genObject.join) {
|
||||
str +=
|
||||
" " +
|
||||
genObject.join
|
||||
.map((join) => {
|
||||
return (
|
||||
join.joinType +
|
||||
" " +
|
||||
join.tableName +
|
||||
" ON " +
|
||||
(() => {
|
||||
if (Array.isArray(join.match)) {
|
||||
return (
|
||||
"(" +
|
||||
join.match
|
||||
.map((mtch) =>
|
||||
generateJoinStr(mtch, join)
|
||||
)
|
||||
.join(" AND ") +
|
||||
")"
|
||||
);
|
||||
} else if (typeof join.match == "object") {
|
||||
return generateJoinStr(join.match, join);
|
||||
}
|
||||
})()
|
||||
);
|
||||
})
|
||||
.join(" ");
|
||||
}
|
||||
|
||||
return str;
|
||||
})();
|
||||
|
||||
if (sqlSearhString) {
|
||||
const stringOperator = genObject?.searchOperator || "AND";
|
||||
queryString += ` WHERE ${sqlSearhString.join(` ${stringOperator} `)} `;
|
||||
}
|
||||
|
||||
if (genObject.order)
|
||||
queryString += ` ORDER BY ${
|
||||
genObject.join
|
||||
? `${tableName}.${genObject.order.field}`
|
||||
: genObject.order.field
|
||||
} ${genObject.order.strategy}`;
|
||||
if (genObject.limit) queryString += ` LIMIT ${genObject.limit}`;
|
||||
|
||||
return {
|
||||
string: queryString,
|
||||
values: sqlSearhValues,
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = sqlGenerator;
|
||||
@@ -0,0 +1,24 @@
|
||||
export = sqlInsertGenerator;
|
||||
/**
|
||||
* @typedef {object} SQLINsertGenReturn
|
||||
* @property {string} query
|
||||
* @property {string[]} values
|
||||
*/
|
||||
/**
|
||||
* @param {object} param0
|
||||
* @param {any[]} param0.data
|
||||
* @param {string} param0.tableName
|
||||
*
|
||||
* @return {SQLINsertGenReturn | undefined}
|
||||
*/
|
||||
declare function sqlInsertGenerator({ tableName, data }: {
|
||||
data: any[];
|
||||
tableName: string;
|
||||
}): SQLINsertGenReturn | undefined;
|
||||
declare namespace sqlInsertGenerator {
|
||||
export { SQLINsertGenReturn };
|
||||
}
|
||||
type SQLINsertGenReturn = {
|
||||
query: string;
|
||||
values: string[];
|
||||
};
|
||||
@@ -0,0 +1,67 @@
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* @typedef {object} SQLInsertGenReturn
|
||||
* @property {string} query
|
||||
* @property {string[]} values
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {object} param0
|
||||
* @param {any[]} param0.data
|
||||
* @param {string} param0.tableName
|
||||
*
|
||||
* @return {SQLInsertGenReturn | undefined}
|
||||
*/
|
||||
function sqlInsertGenerator({ tableName, data }) {
|
||||
try {
|
||||
if (Array.isArray(data) && data?.[0]) {
|
||||
/** @type {string[]} */
|
||||
let insertKeys = [];
|
||||
|
||||
data.forEach((dt) => {
|
||||
const kys = Object.keys(dt);
|
||||
kys.forEach((ky) => {
|
||||
if (!insertKeys.includes(ky)) {
|
||||
insertKeys.push(ky);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
/** @type {string[]} */
|
||||
let queryBatches = [];
|
||||
/** @type {string[]} */
|
||||
let queryValues = [];
|
||||
|
||||
data.forEach((item) => {
|
||||
queryBatches.push(
|
||||
`(${insertKeys
|
||||
.map((ky) => {
|
||||
queryValues.push(
|
||||
item[ky]?.toString()?.match(/./)
|
||||
? item[ky]
|
||||
: null
|
||||
);
|
||||
return "?";
|
||||
})
|
||||
.join(",")})`
|
||||
);
|
||||
});
|
||||
let query = `INSERT INTO ${tableName} (${insertKeys.join(
|
||||
","
|
||||
)}) VALUES ${queryBatches.join(",")}`;
|
||||
|
||||
return {
|
||||
query: query,
|
||||
values: queryValues,
|
||||
};
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log(`SQL insert gen ERROR: ${error.message}`);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = sqlInsertGenerator;
|
||||
Reference in New Issue
Block a user