This commit is contained in:
Benjamin Toby 2025-01-14 16:58:05 +01:00
parent 82152082f3
commit 60cccc9d9f
10 changed files with 56 additions and 43 deletions

View File

@ -5,8 +5,9 @@ interface SQLDeleteGenReturn {
/** /**
* # SQL Delete Generator * # SQL Delete Generator
*/ */
export default function sqlDeleteGenerator({ tableName, data, }: { export default function sqlDeleteGenerator({ tableName, data, dbFullName, }: {
data: any; data: any;
tableName: string; tableName: string;
dbFullName?: string;
}): SQLDeleteGenReturn | undefined; }): SQLDeleteGenReturn | undefined;
export {}; export {};

View File

@ -4,9 +4,10 @@ exports.default = sqlDeleteGenerator;
/** /**
* # SQL Delete Generator * # SQL Delete Generator
*/ */
function sqlDeleteGenerator({ tableName, data, }) { function sqlDeleteGenerator({ tableName, data, dbFullName, }) {
const finalDbName = dbFullName ? `${dbFullName}.` : "";
try { try {
let queryStr = `DELETE FROM ${tableName}`; let queryStr = `DELETE FROM ${finalDbName}${tableName}`;
let deleteBatch = []; let deleteBatch = [];
let queryArr = []; let queryArr = [];
Object.keys(data).forEach((ky) => { Object.keys(data).forEach((ky) => {

View File

@ -2,7 +2,7 @@ import { ServerQueryParam } from "../../../types";
type Param = { type Param = {
genObject?: ServerQueryParam; genObject?: ServerQueryParam;
tableName: string; tableName: string;
dbFullName: string; dbFullName?: string;
}; };
type Return = { type Return = {
string: string; string: string;

View File

@ -11,16 +11,17 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
const finalQuery = genObject.query ? genObject.query : undefined; const finalQuery = genObject.query ? genObject.query : undefined;
const queryKeys = finalQuery ? Object.keys(finalQuery) : undefined; const queryKeys = finalQuery ? Object.keys(finalQuery) : undefined;
const sqlSearhValues = []; const sqlSearhValues = [];
const finalDbName = dbFullName ? `${dbFullName}.` : "";
/** /**
* # Generate Query * # Generate Query
*/ */
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 `${dbFullName}.${queryObj.tableName}.${field}`; return `${finalDbName}${queryObj.tableName}.${field}`;
} }
if (join) { if (join) {
return `${dbFullName}.${tableName}.${field}`; return `${finalDbName}${tableName}.${field}`;
} }
return field; return field;
})(); })();
@ -87,18 +88,18 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
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 `${dbFullName}.${typeof mtch.source == "object" ? mtch.source.tableName : tableName}.${typeof mtch.source == "object" ? mtch.source.fieldName : mtch.source}=${(() => { return `${finalDbName}${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 `${dbFullName}.${typeof mtch.target == "object" return `${finalDbName}${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 `${dbFullName}.${typeof mtch.target == "object" return `${finalDbName}${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 +111,7 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
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) => `${dbFullName}.${tableName}.${fld}`).join(",")}`; str += ` ${(_b = genObject.selectFields) === null || _b === void 0 ? void 0 : _b.map((fld) => `${finalDbName}${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 +119,7 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
} }
else { else {
if (genObject.join) { if (genObject.join) {
str += ` ${dbFullName}.${tableName}.*`; str += ` ${finalDbName}${tableName}.*`;
} }
else { else {
str += " *"; str += " *";
@ -141,12 +142,12 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
return joinObj.selectFields return joinObj.selectFields
.map((selectField) => { .map((selectField) => {
if (typeof selectField == "string") { if (typeof selectField == "string") {
return `${dbFullName}.${joinTableName}.${selectField}`; return `${finalDbName}${joinTableName}.${selectField}`;
} }
else if (typeof selectField == "object") { else if (typeof selectField == "object") {
let aliasSelectField = selectField.count let aliasSelectField = selectField.count
? `COUNT(${dbFullName}.${joinTableName}.${selectField.field})` ? `COUNT(${finalDbName}${joinTableName}.${selectField.field})`
: `${dbFullName}.${joinTableName}.${selectField.field}`; : `${finalDbName}${joinTableName}.${selectField.field}`;
if (selectField.alias) if (selectField.alias)
aliasSelectField += ` AS ${selectField.alias}`; aliasSelectField += ` AS ${selectField.alias}`;
return aliasSelectField; return aliasSelectField;
@ -155,13 +156,13 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
.join(","); .join(",");
} }
else { else {
return `${dbFullName}.${joinTableName}.*`; return `${finalDbName}${joinTableName}.*`;
} }
}) })
.filter((_) => Boolean(_)) .filter((_) => Boolean(_))
.join(","); .join(",");
} }
str += ` FROM ${dbFullName}.${tableName}`; str += ` FROM ${finalDbName}${tableName}`;
if (genObject.join) { if (genObject.join) {
str += str +=
" " + " " +
@ -170,10 +171,10 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
return (join.joinType + return (join.joinType +
" " + " " +
(join.alias (join.alias
? `${dbFullName}.${join.tableName}` + ? `${finalDbName}${join.tableName}` +
" " + " " +
join.alias join.alias
: `${dbFullName}.${join.tableName}`) + : `${finalDbName}${join.tableName}`) +
" ON " + " ON " +
(() => { (() => {
if (Array.isArray(join.match)) { if (Array.isArray(join.match)) {
@ -200,7 +201,7 @@ function sqlGenerator({ tableName, genObject, dbFullName, }) {
} }
if (genObject.order) if (genObject.order)
queryString += ` ORDER BY ${genObject.join queryString += ` ORDER BY ${genObject.join
? `${dbFullName}.${tableName}.${genObject.order.field}` ? `${finalDbName}${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

@ -5,8 +5,9 @@ interface SQLInsertGenReturn {
/** /**
* # SQL Insert Generator * # SQL Insert Generator
*/ */
export default function sqlInsertGenerator({ tableName, data, }: { export default function sqlInsertGenerator({ tableName, data, dbFullName, }: {
data: any[]; data: any[];
tableName: string; tableName: string;
dbFullName?: string;
}): SQLInsertGenReturn | undefined; }): SQLInsertGenReturn | undefined;
export {}; export {};

View File

@ -5,10 +5,10 @@ exports.default = sqlInsertGenerator;
/** /**
* # SQL Insert Generator * # SQL Insert Generator
*/ */
function sqlInsertGenerator({ tableName, data, }) { function sqlInsertGenerator({ tableName, data, dbFullName, }) {
const finalDbName = dbFullName ? `${dbFullName}.` : "";
try { try {
if (Array.isArray(data) && (data === null || data === void 0 ? void 0 : data[0])) { if (Array.isArray(data) && (data === null || data === void 0 ? void 0 : data[0])) {
/** @type {string[]} */
let insertKeys = []; let insertKeys = [];
data.forEach((dt) => { data.forEach((dt) => {
const kys = Object.keys(dt); const kys = Object.keys(dt);
@ -33,7 +33,7 @@ function sqlInsertGenerator({ tableName, data, }) {
}) })
.join(",")})`); .join(",")})`);
}); });
let query = `INSERT INTO ${tableName} (${insertKeys.join(",")}) VALUES ${queryBatches.join(",")}`; let query = `INSERT INTO ${finalDbName}${tableName} (${insertKeys.join(",")}) VALUES ${queryBatches.join(",")}`;
return { return {
query: query, query: query,
values: queryValues, values: queryValues,

View File

@ -9,12 +9,16 @@ interface SQLDeleteGenReturn {
export default function sqlDeleteGenerator({ export default function sqlDeleteGenerator({
tableName, tableName,
data, data,
dbFullName,
}: { }: {
data: any; data: any;
tableName: string; tableName: string;
dbFullName?: string;
}): SQLDeleteGenReturn | undefined { }): SQLDeleteGenReturn | undefined {
const finalDbName = dbFullName ? `${dbFullName}.` : "";
try { try {
let queryStr = `DELETE FROM ${tableName}`; let queryStr = `DELETE FROM ${finalDbName}${tableName}`;
let deleteBatch: string[] = []; let deleteBatch: string[] = [];
let queryArr: string[] = []; let queryArr: string[] = [];

View File

@ -7,7 +7,7 @@ import {
type Param = { type Param = {
genObject?: ServerQueryParam; genObject?: ServerQueryParam;
tableName: string; tableName: string;
dbFullName: string; dbFullName?: string;
}; };
type Return = type Return =
@ -34,6 +34,8 @@ export default function sqlGenerator({
const sqlSearhValues: string[] = []; const sqlSearhValues: string[] = [];
const finalDbName = dbFullName ? `${dbFullName}.` : "";
/** /**
* # Generate Query * # Generate Query
*/ */
@ -48,10 +50,10 @@ export default function sqlGenerator({
}) { }) {
const finalFieldName = (() => { const finalFieldName = (() => {
if (queryObj?.tableName) { if (queryObj?.tableName) {
return `${dbFullName}.${queryObj.tableName}.${field}`; return `${finalDbName}${queryObj.tableName}.${field}`;
} }
if (join) { if (join) {
return `${dbFullName}.${tableName}.${field}`; return `${finalDbName}${tableName}.${field}`;
} }
return field; return field;
})(); })();
@ -133,7 +135,7 @@ export default function sqlGenerator({
/** @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 `${dbFullName}.${ return `${finalDbName}${
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
@ -143,7 +145,7 @@ export default function sqlGenerator({
} }
if (join.alias) { if (join.alias) {
return `${dbFullName}.${ return `${finalDbName}${
typeof mtch.target == "object" typeof mtch.target == "object"
? mtch.target.tableName ? mtch.target.tableName
: join.alias : join.alias
@ -154,7 +156,7 @@ export default function sqlGenerator({
}`; }`;
} }
return `${dbFullName}.${ return `${finalDbName}${
typeof mtch.target == "object" typeof mtch.target == "object"
? mtch.target.tableName ? mtch.target.tableName
: join.tableName : join.tableName
@ -171,14 +173,14 @@ export default function sqlGenerator({
if (genObject.selectFields?.[0]) { if (genObject.selectFields?.[0]) {
if (genObject.join) { if (genObject.join) {
str += ` ${genObject.selectFields str += ` ${genObject.selectFields
?.map((fld) => `${dbFullName}.${tableName}.${fld}`) ?.map((fld) => `${finalDbName}${tableName}.${fld}`)
.join(",")}`; .join(",")}`;
} else { } else {
str += ` ${genObject.selectFields?.join(",")}`; str += ` ${genObject.selectFields?.join(",")}`;
} }
} else { } else {
if (genObject.join) { if (genObject.join) {
str += ` ${dbFullName}.${tableName}.*`; str += ` ${finalDbName}${tableName}.*`;
} else { } else {
str += " *"; str += " *";
} }
@ -204,11 +206,11 @@ export default function sqlGenerator({
return joinObj.selectFields return joinObj.selectFields
.map((selectField) => { .map((selectField) => {
if (typeof selectField == "string") { if (typeof selectField == "string") {
return `${dbFullName}.${joinTableName}.${selectField}`; return `${finalDbName}${joinTableName}.${selectField}`;
} else if (typeof selectField == "object") { } else if (typeof selectField == "object") {
let aliasSelectField = selectField.count let aliasSelectField = selectField.count
? `COUNT(${dbFullName}.${joinTableName}.${selectField.field})` ? `COUNT(${finalDbName}${joinTableName}.${selectField.field})`
: `${dbFullName}.${joinTableName}.${selectField.field}`; : `${finalDbName}${joinTableName}.${selectField.field}`;
if (selectField.alias) if (selectField.alias)
aliasSelectField += ` AS ${selectField.alias}`; aliasSelectField += ` AS ${selectField.alias}`;
return aliasSelectField; return aliasSelectField;
@ -216,14 +218,14 @@ export default function sqlGenerator({
}) })
.join(","); .join(",");
} else { } else {
return `${dbFullName}.${joinTableName}.*`; return `${finalDbName}${joinTableName}.*`;
} }
}) })
.filter((_) => Boolean(_)) .filter((_) => Boolean(_))
.join(","); .join(",");
} }
str += ` FROM ${dbFullName}.${tableName}`; str += ` FROM ${finalDbName}${tableName}`;
if (genObject.join) { if (genObject.join) {
str += str +=
@ -234,10 +236,10 @@ export default function sqlGenerator({
join.joinType + join.joinType +
" " + " " +
(join.alias (join.alias
? `${dbFullName}.${join.tableName}` + ? `${finalDbName}${join.tableName}` +
" " + " " +
join.alias join.alias
: `${dbFullName}.${join.tableName}`) + : `${finalDbName}${join.tableName}`) +
" ON " + " ON " +
(() => { (() => {
if (Array.isArray(join.match)) { if (Array.isArray(join.match)) {
@ -274,7 +276,7 @@ export default function sqlGenerator({
if (genObject.order) if (genObject.order)
queryString += ` ORDER BY ${ queryString += ` ORDER BY ${
genObject.join genObject.join
? `${dbFullName}.${tableName}.${genObject.order.field}` ? `${finalDbName}${tableName}.${genObject.order.field}`
: genObject.order.field : genObject.order.field
} ${genObject.order.strategy}`; } ${genObject.order.strategy}`;

View File

@ -11,13 +11,16 @@ interface SQLInsertGenReturn {
export default function sqlInsertGenerator({ export default function sqlInsertGenerator({
tableName, tableName,
data, data,
dbFullName,
}: { }: {
data: any[]; data: any[];
tableName: string; tableName: string;
dbFullName?: string;
}): SQLInsertGenReturn | undefined { }): SQLInsertGenReturn | undefined {
const finalDbName = dbFullName ? `${dbFullName}.` : "";
try { try {
if (Array.isArray(data) && data?.[0]) { if (Array.isArray(data) && data?.[0]) {
/** @type {string[]} */
let insertKeys: string[] = []; let insertKeys: string[] = [];
data.forEach((dt) => { data.forEach((dt) => {
@ -48,7 +51,7 @@ export default function sqlInsertGenerator({
.join(",")})` .join(",")})`
); );
}); });
let query = `INSERT INTO ${tableName} (${insertKeys.join( let query = `INSERT INTO ${finalDbName}${tableName} (${insertKeys.join(
"," ","
)}) VALUES ${queryBatches.join(",")}`; )}) VALUES ${queryBatches.join(",")}`;

View File

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