From 6d5d8d9dedefadafca473e16d29c7b635c5eefd9 Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Wed, 16 Apr 2025 16:11:33 +0200 Subject: [PATCH 1/2] extend join structure for custom join --- src/type/dynamicQueries.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/type/dynamicQueries.ts b/src/type/dynamicQueries.ts index ca6a7e5..2d870b7 100644 --- a/src/type/dynamicQueries.ts +++ b/src/type/dynamicQueries.ts @@ -3,7 +3,7 @@ export interface DynamicQueryStructure { select: string[] | "*"; table: string; where?: Array; - join?: Array; + join?: Array; orderBy?: Array; // only at top level } @@ -48,6 +48,8 @@ export type WhereOperation = | "timespanEq"; // Date before x years (YYYY-01-01 YYYY-12-31) // TODO: age between | age equals | age greater | age smaller +export type JoinStructure = { foreignColumn: string; type: "defined" } | { condition: string; type: "custom" }; + export type OrderByStructure = { id: string; depth: number; @@ -101,18 +103,21 @@ export const exampleQuery: DynamicQueryStructure = { select: "*", table: "communication", foreignColumn: "sendNewsletter", + type: "defined", }, { id: "91011", select: "*", table: "membership", foreignColumn: "memberships", + type: "defined", join: [ { id: "121314", select: "*", table: "membership_status", foreignColumn: "status", + type: "defined", where: [ { structureType: "condition", From fd2f49f175b21eb00aa689339fc905341a32de8e Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Wed, 16 Apr 2025 16:35:03 +0200 Subject: [PATCH 2/2] custom join by condition --- src/helpers/dynamicQueryBuilder.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/helpers/dynamicQueryBuilder.ts b/src/helpers/dynamicQueryBuilder.ts index 159f518..580bbd2 100644 --- a/src/helpers/dynamicQueryBuilder.ts +++ b/src/helpers/dynamicQueryBuilder.ts @@ -117,7 +117,16 @@ export default abstract class DynamicQueryBuilder { if (queryObject.join) { for (const join of queryObject.join) { let subaffix = join.id ?? StringHelper.random(10); - query.leftJoin(`${alias}.${join.foreignColumn}`, `${subaffix}_${join.table}`); + if (join.type == undefined) join.type = "defined"; + if (join.type == "defined") { + query.innerJoin(`${alias}.${join.foreignColumn}`, `${subaffix}_${join.table}`); + } else { + let condition = join.condition + .replaceAll(`${join.table}.`, `${subaffix}_${join.table}.`) + .replaceAll(`${queryObject.table}.`, `${alias}.`); + + query.innerJoin(join.table, `${subaffix}_${join.table}`, condition); + } this.buildDynamicQuery(query, join, subaffix, depth + 1); } @@ -129,7 +138,6 @@ export default abstract class DynamicQueryBuilder { conditions: Array, alias: string ): void { - console.log(conditions, alias); for (const condition of conditions) { if (condition.structureType == "condition") { const whereClause = this.buildConditionClause(condition, alias); @@ -239,8 +247,6 @@ export default abstract class DynamicQueryBuilder { parameters[`${parameterKey}_end`] = new Date(new Date().getFullYear() - (condition.value as number), 11, 31); } - console.log(query, parameters); - return { query, parameters }; } @@ -417,6 +423,7 @@ const memberByRunningMembershipQuery: DynamicQueryStructure = { table: "membership", where: [{ structureType: "condition", concat: "_", operation: "null", column: "end", value: "" }], foreignColumn: "memberships", + type: "defined", }, ], orderBy: [