prevent duplicate table names by depth

This commit is contained in:
Julian Krauser 2024-12-19 10:32:28 +01:00
parent 6b7b5a99d6
commit b4cdf4899a

View file

@ -55,9 +55,10 @@ export default abstract class DynamicQueryBuilder {
offset: number = 0,
count: number = 25
): SelectQueryBuilder<ObjectLiteral> {
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<ObjectLiteral>,
queryObject: DynamicQueryStructure,
affix: string = "",
depth: number = 0
): void {
const alias = queryObject.table + "_" + depth;
const alias = queryObject.table + "_" + affix;
let firstSelect = true;
let selects: Array<string> = [];
@ -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);
}
}