From b4cdf4899a54586d7dc23616240397b19cd15b78 Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Thu, 19 Dec 2024 10:32:28 +0100 Subject: [PATCH] prevent duplicate table names by depth --- src/helpers/dynamicQueryBuilder.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/helpers/dynamicQueryBuilder.ts b/src/helpers/dynamicQueryBuilder.ts index ad0ba7b..437fd9c 100644 --- a/src/helpers/dynamicQueryBuilder.ts +++ b/src/helpers/dynamicQueryBuilder.ts @@ -55,9 +55,10 @@ export default abstract class DynamicQueryBuilder { offset: number = 0, count: number = 25 ): SelectQueryBuilder { - let query = dataSource.getRepository(queryObj.table).createQueryBuilder(queryObj.table + "_0"); + let affix = Math.random().toString(36).substring(2); + let query = dataSource.getRepository(queryObj.table).createQueryBuilder(`${queryObj.table}_${affix}`); - this.buildDynamicQuery(query, queryObj); + this.buildDynamicQuery(query, queryObj, affix); query.offset(offset); query.limit(count); @@ -68,9 +69,10 @@ export default abstract class DynamicQueryBuilder { private static buildDynamicQuery( query: SelectQueryBuilder, queryObject: DynamicQueryStructure, + affix: string = "", depth: number = 0 ): void { - const alias = queryObject.table + "_" + depth; + const alias = queryObject.table + "_" + affix; let firstSelect = true; let selects: Array = []; @@ -97,9 +99,10 @@ export default abstract class DynamicQueryBuilder { if (queryObject.join) { for (const join of queryObject.join) { - query.leftJoinAndSelect(`${alias}.${join.foreignColumn}`, join.table + "_" + (depth + 1)); + let subaffix = Math.random().toString(36).substring(2); + query.leftJoinAndSelect(`${alias}.${join.foreignColumn}`, join.table + "_" + subaffix); - this.buildDynamicQuery(query, join, depth + 1); + this.buildDynamicQuery(query, join, subaffix, depth + 1); } }