Updates
This commit is contained in:
parent
9cd2707157
commit
82152082f3
@ -2,6 +2,7 @@ import { ServerQueryParam } from "../../../types";
|
||||
type Param = {
|
||||
genObject?: ServerQueryParam;
|
||||
tableName: string;
|
||||
dbFullName: string;
|
||||
};
|
||||
type Return = {
|
||||
string: string;
|
||||
@ -11,5 +12,5 @@ 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({ tableName, genObject, dbFullName, }: Param): Return;
|
||||
export {};
|
||||
|
@ -5,7 +5,7 @@ exports.default = sqlGenerator;
|
||||
* # SQL Query Generator
|
||||
* @description Generates an SQL Query for node module `mysql` or `serverless-mysql`
|
||||
*/
|
||||
function sqlGenerator({ tableName, genObject }) {
|
||||
function sqlGenerator({ tableName, genObject, dbFullName, }) {
|
||||
if (!genObject)
|
||||
return undefined;
|
||||
const finalQuery = genObject.query ? genObject.query : undefined;
|
||||
@ -17,10 +17,10 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
function genSqlSrchStr({ queryObj, join, field, }) {
|
||||
const finalFieldName = (() => {
|
||||
if (queryObj === null || queryObj === void 0 ? void 0 : queryObj.tableName) {
|
||||
return `${queryObj.tableName}.${field}`;
|
||||
return `${dbFullName}.${queryObj.tableName}.${field}`;
|
||||
}
|
||||
if (join) {
|
||||
return `${tableName}.${field}`;
|
||||
return `${dbFullName}.${tableName}.${field}`;
|
||||
}
|
||||
return field;
|
||||
})();
|
||||
@ -87,18 +87,18 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
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}=${(() => {
|
||||
return `${dbFullName}.${typeof mtch.source == "object" ? mtch.source.tableName : tableName}.${typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source}=${(() => {
|
||||
if (mtch.targetLiteral) {
|
||||
return `'${mtch.targetLiteral}'`;
|
||||
}
|
||||
if (join.alias) {
|
||||
return `${typeof mtch.target == "object"
|
||||
return `${dbFullName}.${typeof mtch.target == "object"
|
||||
? mtch.target.tableName
|
||||
: join.alias}.${typeof mtch.target == "object"
|
||||
? mtch.target.fieldName
|
||||
: mtch.target}`;
|
||||
}
|
||||
return `${typeof mtch.target == "object"
|
||||
return `${dbFullName}.${typeof mtch.target == "object"
|
||||
? mtch.target.tableName
|
||||
: join.tableName}.${typeof mtch.target == "object"
|
||||
? mtch.target.fieldName
|
||||
@ -110,7 +110,7 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
let str = "SELECT";
|
||||
if ((_a = genObject.selectFields) === null || _a === void 0 ? void 0 : _a[0]) {
|
||||
if (genObject.join) {
|
||||
str += ` ${(_b = genObject.selectFields) === null || _b === void 0 ? void 0 : _b.map((fld) => `${tableName}.${fld}`).join(",")}`;
|
||||
str += ` ${(_b = genObject.selectFields) === null || _b === void 0 ? void 0 : _b.map((fld) => `${dbFullName}.${tableName}.${fld}`).join(",")}`;
|
||||
}
|
||||
else {
|
||||
str += ` ${(_c = genObject.selectFields) === null || _c === void 0 ? void 0 : _c.join(",")}`;
|
||||
@ -118,7 +118,7 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
}
|
||||
else {
|
||||
if (genObject.join) {
|
||||
str += ` ${tableName}.*`;
|
||||
str += ` ${dbFullName}.${tableName}.*`;
|
||||
}
|
||||
else {
|
||||
str += " *";
|
||||
@ -141,12 +141,12 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
return joinObj.selectFields
|
||||
.map((selectField) => {
|
||||
if (typeof selectField == "string") {
|
||||
return `${joinTableName}.${selectField}`;
|
||||
return `${dbFullName}.${joinTableName}.${selectField}`;
|
||||
}
|
||||
else if (typeof selectField == "object") {
|
||||
let aliasSelectField = selectField.count
|
||||
? `COUNT(${joinTableName}.${selectField.field})`
|
||||
: `${joinTableName}.${selectField.field}`;
|
||||
? `COUNT(${dbFullName}.${joinTableName}.${selectField.field})`
|
||||
: `${dbFullName}.${joinTableName}.${selectField.field}`;
|
||||
if (selectField.alias)
|
||||
aliasSelectField += ` AS ${selectField.alias}`;
|
||||
return aliasSelectField;
|
||||
@ -155,13 +155,13 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
.join(",");
|
||||
}
|
||||
else {
|
||||
return `${joinTableName}.*`;
|
||||
return `${dbFullName}.${joinTableName}.*`;
|
||||
}
|
||||
})
|
||||
.filter((_) => Boolean(_))
|
||||
.join(",");
|
||||
}
|
||||
str += ` FROM ${tableName}`;
|
||||
str += ` FROM ${dbFullName}.${tableName}`;
|
||||
if (genObject.join) {
|
||||
str +=
|
||||
" " +
|
||||
@ -170,8 +170,10 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
return (join.joinType +
|
||||
" " +
|
||||
(join.alias
|
||||
? join.tableName + " " + join.alias
|
||||
: join.tableName) +
|
||||
? `${dbFullName}.${join.tableName}` +
|
||||
" " +
|
||||
join.alias
|
||||
: `${dbFullName}.${join.tableName}`) +
|
||||
" ON " +
|
||||
(() => {
|
||||
if (Array.isArray(join.match)) {
|
||||
@ -198,7 +200,7 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
}
|
||||
if (genObject.order)
|
||||
queryString += ` ORDER BY ${genObject.join
|
||||
? `${tableName}.${genObject.order.field}`
|
||||
? `${dbFullName}.${tableName}.${genObject.order.field}`
|
||||
: genObject.order.field} ${genObject.order.strategy}`;
|
||||
if (genObject.limit)
|
||||
queryString += ` LIMIT ${genObject.limit}`;
|
||||
|
2
dist/utils/post.d.ts
vendored
2
dist/utils/post.d.ts
vendored
@ -2,7 +2,7 @@ import { PostDataPayload, PostReturn } from "../package-shared/types";
|
||||
type Param = {
|
||||
key?: string;
|
||||
database?: string;
|
||||
query: PostDataPayload;
|
||||
query: string | PostDataPayload;
|
||||
queryValues?: any[];
|
||||
tableName?: string;
|
||||
useLocal?: boolean;
|
||||
|
@ -7,6 +7,7 @@ import {
|
||||
type Param = {
|
||||
genObject?: ServerQueryParam;
|
||||
tableName: string;
|
||||
dbFullName: string;
|
||||
};
|
||||
|
||||
type Return =
|
||||
@ -20,7 +21,11 @@ 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({
|
||||
tableName,
|
||||
genObject,
|
||||
dbFullName,
|
||||
}: Param): Return {
|
||||
if (!genObject) return undefined;
|
||||
|
||||
const finalQuery = genObject.query ? genObject.query : undefined;
|
||||
@ -43,10 +48,10 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
}) {
|
||||
const finalFieldName = (() => {
|
||||
if (queryObj?.tableName) {
|
||||
return `${queryObj.tableName}.${field}`;
|
||||
return `${dbFullName}.${queryObj.tableName}.${field}`;
|
||||
}
|
||||
if (join) {
|
||||
return `${tableName}.${field}`;
|
||||
return `${dbFullName}.${tableName}.${field}`;
|
||||
}
|
||||
return field;
|
||||
})();
|
||||
@ -128,7 +133,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 `${dbFullName}.${
|
||||
typeof mtch.source == "object" ? mtch.source.tableName : tableName
|
||||
}.${
|
||||
typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source
|
||||
@ -138,7 +143,7 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
}
|
||||
|
||||
if (join.alias) {
|
||||
return `${
|
||||
return `${dbFullName}.${
|
||||
typeof mtch.target == "object"
|
||||
? mtch.target.tableName
|
||||
: join.alias
|
||||
@ -149,7 +154,7 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
}`;
|
||||
}
|
||||
|
||||
return `${
|
||||
return `${dbFullName}.${
|
||||
typeof mtch.target == "object"
|
||||
? mtch.target.tableName
|
||||
: join.tableName
|
||||
@ -166,14 +171,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) => `${dbFullName}.${tableName}.${fld}`)
|
||||
.join(",")}`;
|
||||
} else {
|
||||
str += ` ${genObject.selectFields?.join(",")}`;
|
||||
}
|
||||
} else {
|
||||
if (genObject.join) {
|
||||
str += ` ${tableName}.*`;
|
||||
str += ` ${dbFullName}.${tableName}.*`;
|
||||
} else {
|
||||
str += " *";
|
||||
}
|
||||
@ -199,11 +204,11 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
return joinObj.selectFields
|
||||
.map((selectField) => {
|
||||
if (typeof selectField == "string") {
|
||||
return `${joinTableName}.${selectField}`;
|
||||
return `${dbFullName}.${joinTableName}.${selectField}`;
|
||||
} else if (typeof selectField == "object") {
|
||||
let aliasSelectField = selectField.count
|
||||
? `COUNT(${joinTableName}.${selectField.field})`
|
||||
: `${joinTableName}.${selectField.field}`;
|
||||
? `COUNT(${dbFullName}.${joinTableName}.${selectField.field})`
|
||||
: `${dbFullName}.${joinTableName}.${selectField.field}`;
|
||||
if (selectField.alias)
|
||||
aliasSelectField += ` AS ${selectField.alias}`;
|
||||
return aliasSelectField;
|
||||
@ -211,14 +216,14 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
})
|
||||
.join(",");
|
||||
} else {
|
||||
return `${joinTableName}.*`;
|
||||
return `${dbFullName}.${joinTableName}.*`;
|
||||
}
|
||||
})
|
||||
.filter((_) => Boolean(_))
|
||||
.join(",");
|
||||
}
|
||||
|
||||
str += ` FROM ${tableName}`;
|
||||
str += ` FROM ${dbFullName}.${tableName}`;
|
||||
|
||||
if (genObject.join) {
|
||||
str +=
|
||||
@ -229,8 +234,10 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
join.joinType +
|
||||
" " +
|
||||
(join.alias
|
||||
? join.tableName + " " + join.alias
|
||||
: join.tableName) +
|
||||
? `${dbFullName}.${join.tableName}` +
|
||||
" " +
|
||||
join.alias
|
||||
: `${dbFullName}.${join.tableName}`) +
|
||||
" ON " +
|
||||
(() => {
|
||||
if (Array.isArray(join.match)) {
|
||||
@ -267,7 +274,7 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
|
||||
if (genObject.order)
|
||||
queryString += ` ORDER BY ${
|
||||
genObject.join
|
||||
? `${tableName}.${genObject.order.field}`
|
||||
? `${dbFullName}.${tableName}.${genObject.order.field}`
|
||||
: genObject.order.field
|
||||
} ${genObject.order.strategy}`;
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@moduletrace/datasquirel",
|
||||
"version": "3.5.9",
|
||||
"version": "3.6.0",
|
||||
"description": "Cloud-based SQL data management tool",
|
||||
"main": "dist/index.js",
|
||||
"bin": {
|
||||
|
@ -9,7 +9,7 @@ import { PostDataPayload, PostReturn } from "../package-shared/types";
|
||||
type Param = {
|
||||
key?: string;
|
||||
database?: string;
|
||||
query: PostDataPayload;
|
||||
query: string | PostDataPayload;
|
||||
queryValues?: any[];
|
||||
tableName?: string;
|
||||
useLocal?: boolean;
|
||||
|
Loading…
Reference in New Issue
Block a user