This commit is contained in:
Benjamin Toby 2025-01-14 16:50:40 +01:00
parent 9cd2707157
commit 82152082f3
6 changed files with 46 additions and 36 deletions

View File

@ -2,6 +2,7 @@ import { ServerQueryParam } from "../../../types";
type Param = { type Param = {
genObject?: ServerQueryParam; genObject?: ServerQueryParam;
tableName: string; tableName: string;
dbFullName: string;
}; };
type Return = { type Return = {
string: string; string: string;
@ -11,5 +12,5 @@ type Return = {
* # SQL Query Generator * # SQL Query Generator
* @description Generates an SQL Query for node module `mysql` or `serverless-mysql` * @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 {}; export {};

View File

@ -5,7 +5,7 @@ exports.default = sqlGenerator;
* # SQL Query Generator * # SQL Query Generator
* @description Generates an SQL Query for node module `mysql` or `serverless-mysql` * @description Generates an SQL Query for node module `mysql` or `serverless-mysql`
*/ */
function sqlGenerator({ tableName, genObject }) { function sqlGenerator({ tableName, genObject, dbFullName, }) {
if (!genObject) if (!genObject)
return undefined; return undefined;
const finalQuery = genObject.query ? genObject.query : undefined; const finalQuery = genObject.query ? genObject.query : undefined;
@ -17,10 +17,10 @@ function sqlGenerator({ tableName, genObject }) {
function genSqlSrchStr({ queryObj, join, field, }) { function genSqlSrchStr({ queryObj, join, field, }) {
const finalFieldName = (() => { const finalFieldName = (() => {
if (queryObj === null || queryObj === void 0 ? void 0 : queryObj.tableName) { if (queryObj === null || queryObj === void 0 ? void 0 : queryObj.tableName) {
return `${queryObj.tableName}.${field}`; return `${dbFullName}.${queryObj.tableName}.${field}`;
} }
if (join) { if (join) {
return `${tableName}.${field}`; return `${dbFullName}.${tableName}.${field}`;
} }
return field; return field;
})(); })();
@ -87,18 +87,18 @@ function sqlGenerator({ tableName, genObject }) {
function generateJoinStr( function generateJoinStr(
/** @type {import("../../../types").ServerQueryParamsJoinMatchObject} */ mtch, /** @type {import("../../../types").ServerQueryParamsJoinMatchObject} */ mtch,
/** @type {import("../../../types").ServerQueryParamsJoin} */ join) { /** @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) { if (mtch.targetLiteral) {
return `'${mtch.targetLiteral}'`; return `'${mtch.targetLiteral}'`;
} }
if (join.alias) { if (join.alias) {
return `${typeof mtch.target == "object" return `${dbFullName}.${typeof mtch.target == "object"
? mtch.target.tableName ? mtch.target.tableName
: join.alias}.${typeof mtch.target == "object" : join.alias}.${typeof mtch.target == "object"
? mtch.target.fieldName ? mtch.target.fieldName
: mtch.target}`; : mtch.target}`;
} }
return `${typeof mtch.target == "object" return `${dbFullName}.${typeof mtch.target == "object"
? mtch.target.tableName ? mtch.target.tableName
: join.tableName}.${typeof mtch.target == "object" : join.tableName}.${typeof mtch.target == "object"
? mtch.target.fieldName ? mtch.target.fieldName
@ -110,7 +110,7 @@ function sqlGenerator({ tableName, genObject }) {
let str = "SELECT"; let str = "SELECT";
if ((_a = genObject.selectFields) === null || _a === void 0 ? void 0 : _a[0]) { if ((_a = genObject.selectFields) === null || _a === void 0 ? void 0 : _a[0]) {
if (genObject.join) { 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 { else {
str += ` ${(_c = genObject.selectFields) === null || _c === void 0 ? void 0 : _c.join(",")}`; str += ` ${(_c = genObject.selectFields) === null || _c === void 0 ? void 0 : _c.join(",")}`;
@ -118,7 +118,7 @@ function sqlGenerator({ tableName, genObject }) {
} }
else { else {
if (genObject.join) { if (genObject.join) {
str += ` ${tableName}.*`; str += ` ${dbFullName}.${tableName}.*`;
} }
else { else {
str += " *"; str += " *";
@ -141,12 +141,12 @@ function sqlGenerator({ tableName, genObject }) {
return joinObj.selectFields return joinObj.selectFields
.map((selectField) => { .map((selectField) => {
if (typeof selectField == "string") { if (typeof selectField == "string") {
return `${joinTableName}.${selectField}`; return `${dbFullName}.${joinTableName}.${selectField}`;
} }
else if (typeof selectField == "object") { else if (typeof selectField == "object") {
let aliasSelectField = selectField.count let aliasSelectField = selectField.count
? `COUNT(${joinTableName}.${selectField.field})` ? `COUNT(${dbFullName}.${joinTableName}.${selectField.field})`
: `${joinTableName}.${selectField.field}`; : `${dbFullName}.${joinTableName}.${selectField.field}`;
if (selectField.alias) if (selectField.alias)
aliasSelectField += ` AS ${selectField.alias}`; aliasSelectField += ` AS ${selectField.alias}`;
return aliasSelectField; return aliasSelectField;
@ -155,13 +155,13 @@ function sqlGenerator({ tableName, genObject }) {
.join(","); .join(",");
} }
else { else {
return `${joinTableName}.*`; return `${dbFullName}.${joinTableName}.*`;
} }
}) })
.filter((_) => Boolean(_)) .filter((_) => Boolean(_))
.join(","); .join(",");
} }
str += ` FROM ${tableName}`; str += ` FROM ${dbFullName}.${tableName}`;
if (genObject.join) { if (genObject.join) {
str += str +=
" " + " " +
@ -170,8 +170,10 @@ function sqlGenerator({ tableName, genObject }) {
return (join.joinType + return (join.joinType +
" " + " " +
(join.alias (join.alias
? join.tableName + " " + join.alias ? `${dbFullName}.${join.tableName}` +
: join.tableName) + " " +
join.alias
: `${dbFullName}.${join.tableName}`) +
" ON " + " ON " +
(() => { (() => {
if (Array.isArray(join.match)) { if (Array.isArray(join.match)) {
@ -198,7 +200,7 @@ function sqlGenerator({ tableName, genObject }) {
} }
if (genObject.order) if (genObject.order)
queryString += ` ORDER BY ${genObject.join queryString += ` ORDER BY ${genObject.join
? `${tableName}.${genObject.order.field}` ? `${dbFullName}.${tableName}.${genObject.order.field}`
: genObject.order.field} ${genObject.order.strategy}`; : genObject.order.field} ${genObject.order.strategy}`;
if (genObject.limit) if (genObject.limit)
queryString += ` LIMIT ${genObject.limit}`; queryString += ` LIMIT ${genObject.limit}`;

View File

@ -2,7 +2,7 @@ import { PostDataPayload, PostReturn } from "../package-shared/types";
type Param = { type Param = {
key?: string; key?: string;
database?: string; database?: string;
query: PostDataPayload; query: string | PostDataPayload;
queryValues?: any[]; queryValues?: any[];
tableName?: string; tableName?: string;
useLocal?: boolean; useLocal?: boolean;

View File

@ -7,6 +7,7 @@ import {
type Param = { type Param = {
genObject?: ServerQueryParam; genObject?: ServerQueryParam;
tableName: string; tableName: string;
dbFullName: string;
}; };
type Return = type Return =
@ -20,7 +21,11 @@ type Return =
* # SQL Query Generator * # SQL Query Generator
* @description Generates an SQL Query for node module `mysql` or `serverless-mysql` * @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; if (!genObject) return undefined;
const finalQuery = genObject.query ? genObject.query : undefined; const finalQuery = genObject.query ? genObject.query : undefined;
@ -43,10 +48,10 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
}) { }) {
const finalFieldName = (() => { const finalFieldName = (() => {
if (queryObj?.tableName) { if (queryObj?.tableName) {
return `${queryObj.tableName}.${field}`; return `${dbFullName}.${queryObj.tableName}.${field}`;
} }
if (join) { if (join) {
return `${tableName}.${field}`; return `${dbFullName}.${tableName}.${field}`;
} }
return 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").ServerQueryParamsJoinMatchObject} */ mtch: import("../../../types").ServerQueryParamsJoinMatchObject,
/** @type {import("../../../types").ServerQueryParamsJoin} */ join: import("../../../types").ServerQueryParamsJoin /** @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.tableName : tableName
}.${ }.${
typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source
@ -138,7 +143,7 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
} }
if (join.alias) { if (join.alias) {
return `${ return `${dbFullName}.${
typeof mtch.target == "object" typeof mtch.target == "object"
? mtch.target.tableName ? mtch.target.tableName
: join.alias : join.alias
@ -149,7 +154,7 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
}`; }`;
} }
return `${ return `${dbFullName}.${
typeof mtch.target == "object" typeof mtch.target == "object"
? mtch.target.tableName ? mtch.target.tableName
: join.tableName : join.tableName
@ -166,14 +171,14 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
if (genObject.selectFields?.[0]) { if (genObject.selectFields?.[0]) {
if (genObject.join) { if (genObject.join) {
str += ` ${genObject.selectFields str += ` ${genObject.selectFields
?.map((fld) => `${tableName}.${fld}`) ?.map((fld) => `${dbFullName}.${tableName}.${fld}`)
.join(",")}`; .join(",")}`;
} else { } else {
str += ` ${genObject.selectFields?.join(",")}`; str += ` ${genObject.selectFields?.join(",")}`;
} }
} else { } else {
if (genObject.join) { if (genObject.join) {
str += ` ${tableName}.*`; str += ` ${dbFullName}.${tableName}.*`;
} else { } else {
str += " *"; str += " *";
} }
@ -199,11 +204,11 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
return joinObj.selectFields return joinObj.selectFields
.map((selectField) => { .map((selectField) => {
if (typeof selectField == "string") { if (typeof selectField == "string") {
return `${joinTableName}.${selectField}`; return `${dbFullName}.${joinTableName}.${selectField}`;
} else if (typeof selectField == "object") { } else if (typeof selectField == "object") {
let aliasSelectField = selectField.count let aliasSelectField = selectField.count
? `COUNT(${joinTableName}.${selectField.field})` ? `COUNT(${dbFullName}.${joinTableName}.${selectField.field})`
: `${joinTableName}.${selectField.field}`; : `${dbFullName}.${joinTableName}.${selectField.field}`;
if (selectField.alias) if (selectField.alias)
aliasSelectField += ` AS ${selectField.alias}`; aliasSelectField += ` AS ${selectField.alias}`;
return aliasSelectField; return aliasSelectField;
@ -211,14 +216,14 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
}) })
.join(","); .join(",");
} else { } else {
return `${joinTableName}.*`; return `${dbFullName}.${joinTableName}.*`;
} }
}) })
.filter((_) => Boolean(_)) .filter((_) => Boolean(_))
.join(","); .join(",");
} }
str += ` FROM ${tableName}`; str += ` FROM ${dbFullName}.${tableName}`;
if (genObject.join) { if (genObject.join) {
str += str +=
@ -229,8 +234,10 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
join.joinType + join.joinType +
" " + " " +
(join.alias (join.alias
? join.tableName + " " + join.alias ? `${dbFullName}.${join.tableName}` +
: join.tableName) + " " +
join.alias
: `${dbFullName}.${join.tableName}`) +
" ON " + " ON " +
(() => { (() => {
if (Array.isArray(join.match)) { if (Array.isArray(join.match)) {
@ -267,7 +274,7 @@ export default function sqlGenerator({ tableName, genObject }: Param): Return {
if (genObject.order) if (genObject.order)
queryString += ` ORDER BY ${ queryString += ` ORDER BY ${
genObject.join genObject.join
? `${tableName}.${genObject.order.field}` ? `${dbFullName}.${tableName}.${genObject.order.field}`
: genObject.order.field : genObject.order.field
} ${genObject.order.strategy}`; } ${genObject.order.strategy}`;

View File

@ -1,6 +1,6 @@
{ {
"name": "@moduletrace/datasquirel", "name": "@moduletrace/datasquirel",
"version": "3.5.9", "version": "3.6.0",
"description": "Cloud-based SQL data management tool", "description": "Cloud-based SQL data management tool",
"main": "dist/index.js", "main": "dist/index.js",
"bin": { "bin": {

View File

@ -9,7 +9,7 @@ import { PostDataPayload, PostReturn } from "../package-shared/types";
type Param = { type Param = {
key?: string; key?: string;
database?: string; database?: string;
query: PostDataPayload; query: string | PostDataPayload;
queryValues?: any[]; queryValues?: any[];
tableName?: string; tableName?: string;
useLocal?: boolean; useLocal?: boolean;