Refactor Code to typescript
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
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[];
|
||||
};
|
||||
@@ -1,41 +0,0 @@
|
||||
// @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,36 @@
|
||||
interface SQLDeleteGenReturn {
|
||||
query: string;
|
||||
values: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* # SQL Delete Generator
|
||||
*/
|
||||
export default function sqlDeleteGenerator({
|
||||
tableName,
|
||||
data,
|
||||
}: {
|
||||
data: any;
|
||||
tableName: string;
|
||||
}): SQLDeleteGenReturn | undefined {
|
||||
try {
|
||||
let queryStr = `DELETE FROM ${tableName}`;
|
||||
|
||||
let deleteBatch: string[] = [];
|
||||
let queryArr: string[] = [];
|
||||
|
||||
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: any) {
|
||||
console.log(`SQL delete gen ERROR: ${error.message}`);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
export = sqlGenerator;
|
||||
declare function sqlGenerator(Param0: {
|
||||
genObject?: import("../../../types").ServerQueryParam;
|
||||
tableName: string;
|
||||
}): {
|
||||
string: string;
|
||||
values: string[];
|
||||
} | undefined;
|
||||
+36
-22
@@ -1,28 +1,46 @@
|
||||
// @ts-check
|
||||
import {
|
||||
ServerQueryParam,
|
||||
ServerQueryParamsJoin,
|
||||
ServerQueryQueryObject,
|
||||
} from "../../../types";
|
||||
|
||||
type Param = {
|
||||
genObject?: ServerQueryParam;
|
||||
tableName: string;
|
||||
};
|
||||
|
||||
type Return =
|
||||
| {
|
||||
string: string;
|
||||
values: string[];
|
||||
}
|
||||
| undefined;
|
||||
|
||||
/**
|
||||
* # SQL Query Generator
|
||||
* @description Generates an SQL Query for node module `mysql` or `serverless-mysql`
|
||||
* @type {import("../../../types").SqlGeneratorFn}
|
||||
*/
|
||||
function sqlGenerator({ tableName, genObject }) {
|
||||
export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
if (!genObject) return undefined;
|
||||
|
||||
const finalQuery = genObject.query ? genObject.query : undefined;
|
||||
|
||||
const queryKeys = finalQuery ? Object.keys(finalQuery) : undefined;
|
||||
|
||||
/** @type {string[]} */
|
||||
const sqlSearhValues = [];
|
||||
const sqlSearhValues: string[] = [];
|
||||
|
||||
/**
|
||||
* # Generate Query
|
||||
* @param {object} param
|
||||
* @param {import("../../../types").ServerQueryQueryObject[string]} param.queryObj
|
||||
* @param {import("../../../types").ServerQueryParamsJoin[]} [param.join]
|
||||
* @param {string} [param.field]
|
||||
*/
|
||||
function genSqlSrchStr({ queryObj, join, field }) {
|
||||
function genSqlSrchStr({
|
||||
queryObj,
|
||||
join,
|
||||
field,
|
||||
}: {
|
||||
queryObj: ServerQueryQueryObject[string];
|
||||
join?: ServerQueryParamsJoin[];
|
||||
field?: string;
|
||||
}) {
|
||||
const finalFieldName = (() => {
|
||||
if (queryObj?.tableName) {
|
||||
return `${queryObj.tableName}.${field}`;
|
||||
@@ -51,7 +69,7 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
}
|
||||
} else if (Array.isArray(queryObj.value)) {
|
||||
/** @type {string[]} */
|
||||
const strArray = [];
|
||||
const strArray: string[] = [];
|
||||
queryObj.value.forEach((val) => {
|
||||
const valueParsed = val;
|
||||
if (queryObj.equality == "LIKE") {
|
||||
@@ -75,16 +93,14 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
|
||||
const sqlSearhString = queryKeys?.map((field) => {
|
||||
const queryObj =
|
||||
/** @type {import("../../../types").ServerQueryQueryObject} */ (
|
||||
finalQuery?.[field]
|
||||
);
|
||||
/** @type {import("../../../types").ServerQueryQueryObject} */ finalQuery?.[
|
||||
field
|
||||
];
|
||||
if (!queryObj) return;
|
||||
|
||||
if (queryObj.__query) {
|
||||
const subQueryGroup =
|
||||
/** @type {import("../../../types").ServerQueryQueryObject}} */ (
|
||||
queryObj.__query
|
||||
);
|
||||
/** @type {import("../../../types").ServerQueryQueryObject}} */ queryObj.__query;
|
||||
|
||||
const subSearchKeys = Object.keys(subQueryGroup);
|
||||
const subSearchString = subSearchKeys.map((_field) => {
|
||||
@@ -109,8 +125,8 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
});
|
||||
|
||||
function generateJoinStr(
|
||||
/** @type {import("../../../types").ServerQueryParamsJoinMatchObject} */ mtch,
|
||||
/** @type {import("../../../types").ServerQueryParamsJoin} */ join
|
||||
/** @type {import("../../../types").ServerQueryParamsJoinMatchObject} */ mtch: import("../../../types").ServerQueryParamsJoinMatchObject,
|
||||
/** @type {import("../../../types").ServerQueryParamsJoin} */ join: import("../../../types").ServerQueryParamsJoin
|
||||
) {
|
||||
return `${
|
||||
typeof mtch.source == "object" ? mtch.source.tableName : tableName
|
||||
@@ -165,7 +181,7 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
|
||||
if (genObject.join) {
|
||||
/** @type {string[]} */
|
||||
const existingJoinTableNames = [tableName];
|
||||
const existingJoinTableNames: string[] = [tableName];
|
||||
|
||||
str +=
|
||||
"," +
|
||||
@@ -263,5 +279,3 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
values: sqlSearhValues,
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = sqlGenerator;
|
||||
@@ -1,24 +0,0 @@
|
||||
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[];
|
||||
};
|
||||
+16
-17
@@ -1,23 +1,24 @@
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* @typedef {object} SQLInsertGenReturn
|
||||
* @property {string} query
|
||||
* @property {string[]} values
|
||||
*/
|
||||
interface SQLInsertGenReturn {
|
||||
query: string;
|
||||
values: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object} param0
|
||||
* @param {any[]} param0.data
|
||||
* @param {string} param0.tableName
|
||||
*
|
||||
* @return {SQLInsertGenReturn | undefined}
|
||||
* # SQL Insert Generator
|
||||
*/
|
||||
function sqlInsertGenerator({ tableName, data }) {
|
||||
export default function sqlInsertGenerator({
|
||||
tableName,
|
||||
data,
|
||||
}: {
|
||||
data: any[];
|
||||
tableName: string;
|
||||
}): SQLInsertGenReturn | undefined {
|
||||
try {
|
||||
if (Array.isArray(data) && data?.[0]) {
|
||||
/** @type {string[]} */
|
||||
let insertKeys = [];
|
||||
let insertKeys: string[] = [];
|
||||
|
||||
data.forEach((dt) => {
|
||||
const kys = Object.keys(dt);
|
||||
@@ -29,9 +30,9 @@ function sqlInsertGenerator({ tableName, data }) {
|
||||
});
|
||||
|
||||
/** @type {string[]} */
|
||||
let queryBatches = [];
|
||||
let queryBatches: string[] = [];
|
||||
/** @type {string[]} */
|
||||
let queryValues = [];
|
||||
let queryValues: string[] = [];
|
||||
|
||||
data.forEach((item) => {
|
||||
queryBatches.push(
|
||||
@@ -58,10 +59,8 @@ function sqlInsertGenerator({ tableName, data }) {
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
} catch (/** @type {any} */ error) {
|
||||
} catch (/** @type {any} */ error: any) {
|
||||
console.log(`SQL insert gen ERROR: ${error.message}`);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = sqlInsertGenerator;
|
||||
Reference in New Issue
Block a user