Updates
This commit is contained in:
@@ -9,12 +9,16 @@ interface SQLDeleteGenReturn {
|
||||
export default function sqlDeleteGenerator({
|
||||
tableName,
|
||||
data,
|
||||
dbFullName,
|
||||
}: {
|
||||
data: any;
|
||||
tableName: string;
|
||||
dbFullName?: string;
|
||||
}): SQLDeleteGenReturn | undefined {
|
||||
const finalDbName = dbFullName ? `${dbFullName}.` : "";
|
||||
|
||||
try {
|
||||
let queryStr = `DELETE FROM ${tableName}`;
|
||||
let queryStr = `DELETE FROM ${finalDbName}${tableName}`;
|
||||
|
||||
let deleteBatch: string[] = [];
|
||||
let queryArr: string[] = [];
|
||||
|
||||
@@ -4,9 +4,10 @@ import {
|
||||
ServerQueryQueryObject,
|
||||
} from "../../../types";
|
||||
|
||||
type Param = {
|
||||
genObject?: ServerQueryParam;
|
||||
type Param<T extends { [key: string]: any } = { [key: string]: any }> = {
|
||||
genObject?: ServerQueryParam<T>;
|
||||
tableName: string;
|
||||
dbFullName?: string;
|
||||
};
|
||||
|
||||
type Return =
|
||||
@@ -20,7 +21,9 @@ type Return =
|
||||
* # SQL Query Generator
|
||||
* @description Generates an SQL Query for node module `mysql` or `serverless-mysql`
|
||||
*/
|
||||
export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
export default function sqlGenerator<
|
||||
T extends { [key: string]: any } = { [key: string]: any }
|
||||
>({ tableName, genObject, dbFullName }: Param<T>): Return {
|
||||
if (!genObject) return undefined;
|
||||
|
||||
const finalQuery = genObject.query ? genObject.query : undefined;
|
||||
@@ -29,6 +32,8 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
|
||||
const sqlSearhValues: string[] = [];
|
||||
|
||||
const finalDbName = dbFullName ? `${dbFullName}.` : "";
|
||||
|
||||
/**
|
||||
* # Generate Query
|
||||
*/
|
||||
@@ -43,10 +48,10 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
}) {
|
||||
const finalFieldName = (() => {
|
||||
if (queryObj?.tableName) {
|
||||
return `${queryObj.tableName}.${field}`;
|
||||
return `${finalDbName}${queryObj.tableName}.${field}`;
|
||||
}
|
||||
if (join) {
|
||||
return `${tableName}.${field}`;
|
||||
return `${finalDbName}${tableName}.${field}`;
|
||||
}
|
||||
return field;
|
||||
})();
|
||||
@@ -112,7 +117,6 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
join: genObject.join,
|
||||
});
|
||||
});
|
||||
console.log("queryObj.operator", queryObj.operator);
|
||||
|
||||
return (
|
||||
"(" +
|
||||
@@ -128,7 +132,7 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
/** @type {import("../../../types").ServerQueryParamsJoinMatchObject} */ mtch: import("../../../types").ServerQueryParamsJoinMatchObject,
|
||||
/** @type {import("../../../types").ServerQueryParamsJoin} */ join: import("../../../types").ServerQueryParamsJoin
|
||||
) {
|
||||
return `${
|
||||
return `${finalDbName}${
|
||||
typeof mtch.source == "object" ? mtch.source.tableName : tableName
|
||||
}.${
|
||||
typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source
|
||||
@@ -138,7 +142,7 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
}
|
||||
|
||||
if (join.alias) {
|
||||
return `${
|
||||
return `${finalDbName}${
|
||||
typeof mtch.target == "object"
|
||||
? mtch.target.tableName
|
||||
: join.alias
|
||||
@@ -149,7 +153,7 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
}`;
|
||||
}
|
||||
|
||||
return `${
|
||||
return `${finalDbName}${
|
||||
typeof mtch.target == "object"
|
||||
? mtch.target.tableName
|
||||
: join.tableName
|
||||
@@ -166,14 +170,14 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
if (genObject.selectFields?.[0]) {
|
||||
if (genObject.join) {
|
||||
str += ` ${genObject.selectFields
|
||||
?.map((fld) => `${tableName}.${fld}`)
|
||||
?.map((fld) => `${finalDbName}${tableName}.${fld}`)
|
||||
.join(",")}`;
|
||||
} else {
|
||||
str += ` ${genObject.selectFields?.join(",")}`;
|
||||
}
|
||||
} else {
|
||||
if (genObject.join) {
|
||||
str += ` ${tableName}.*`;
|
||||
str += ` ${finalDbName}${tableName}.*`;
|
||||
} else {
|
||||
str += " *";
|
||||
}
|
||||
@@ -199,11 +203,11 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
return joinObj.selectFields
|
||||
.map((selectField) => {
|
||||
if (typeof selectField == "string") {
|
||||
return `${joinTableName}.${selectField}`;
|
||||
return `${finalDbName}${joinTableName}.${selectField}`;
|
||||
} else if (typeof selectField == "object") {
|
||||
let aliasSelectField = selectField.count
|
||||
? `COUNT(${joinTableName}.${selectField.field})`
|
||||
: `${joinTableName}.${selectField.field}`;
|
||||
? `COUNT(${finalDbName}${joinTableName}.${selectField.field})`
|
||||
: `${finalDbName}${joinTableName}.${selectField.field}`;
|
||||
if (selectField.alias)
|
||||
aliasSelectField += ` AS ${selectField.alias}`;
|
||||
return aliasSelectField;
|
||||
@@ -211,14 +215,14 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
})
|
||||
.join(",");
|
||||
} else {
|
||||
return `${joinTableName}.*`;
|
||||
return `${finalDbName}${joinTableName}.*`;
|
||||
}
|
||||
})
|
||||
.filter((_) => Boolean(_))
|
||||
.join(",");
|
||||
}
|
||||
|
||||
str += ` FROM ${tableName}`;
|
||||
str += ` FROM ${finalDbName}${tableName}`;
|
||||
|
||||
if (genObject.join) {
|
||||
str +=
|
||||
@@ -229,8 +233,10 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
join.joinType +
|
||||
" " +
|
||||
(join.alias
|
||||
? join.tableName + " " + join.alias
|
||||
: join.tableName) +
|
||||
? `${finalDbName}${join.tableName}` +
|
||||
" " +
|
||||
join.alias
|
||||
: `${finalDbName}${join.tableName}`) +
|
||||
" ON " +
|
||||
(() => {
|
||||
if (Array.isArray(join.match)) {
|
||||
@@ -267,8 +273,8 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
if (genObject.order)
|
||||
queryString += ` ORDER BY ${
|
||||
genObject.join
|
||||
? `${tableName}.${genObject.order.field}`
|
||||
: genObject.order.field
|
||||
? `${finalDbName}${tableName}.${String(genObject.order.field)}`
|
||||
: String(genObject.order.field)
|
||||
} ${genObject.order.strategy}`;
|
||||
|
||||
if (genObject.limit) queryString += ` LIMIT ${genObject.limit}`;
|
||||
|
||||
@@ -11,13 +11,16 @@ interface SQLInsertGenReturn {
|
||||
export default function sqlInsertGenerator({
|
||||
tableName,
|
||||
data,
|
||||
dbFullName,
|
||||
}: {
|
||||
data: any[];
|
||||
tableName: string;
|
||||
dbFullName?: string;
|
||||
}): SQLInsertGenReturn | undefined {
|
||||
const finalDbName = dbFullName ? `${dbFullName}.` : "";
|
||||
|
||||
try {
|
||||
if (Array.isArray(data) && data?.[0]) {
|
||||
/** @type {string[]} */
|
||||
let insertKeys: string[] = [];
|
||||
|
||||
data.forEach((dt) => {
|
||||
@@ -48,7 +51,7 @@ export default function sqlInsertGenerator({
|
||||
.join(",")})`
|
||||
);
|
||||
});
|
||||
let query = `INSERT INTO ${tableName} (${insertKeys.join(
|
||||
let query = `INSERT INTO ${finalDbName}${tableName} (${insertKeys.join(
|
||||
","
|
||||
)}) VALUES ${queryBatches.join(",")}`;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user