add error checking for QueryService.leftJoin

This commit is contained in:
Hazelnoot 2025-08-11 16:25:46 -04:00
parent efc8bbb4b3
commit 21700a668e

View file

@ -285,7 +285,27 @@ export class QueryService {
@bindThis
public leftJoin<E extends ObjectLiteral>(q: SelectQueryBuilder<E>, relation: string, alias: string, condition?: string): SelectQueryBuilder<E> {
// Skip if it's already joined, otherwise we'll get an error
if (!q.expressionMap.joinAttributes.some(j => j.alias.name === alias)) {
const join = q.expressionMap.joinAttributes.find(j => j.alias.name === alias);
if (join) {
const oldRelation = typeof(join.entityOrProperty) === 'function'
? join.entityOrProperty.name
: join.entityOrProperty;
const oldQuery = join.condition
? `JOIN ${oldRelation} AS ${alias} ON ${join.condition}`
: `JOIN ${oldRelation} AS ${alias}`;
const newQuery = condition
? `JOIN ${relation} AS ${alias} ON ${oldRelation}`
: `JOIN ${relation} AS ${alias}`;
if (oldRelation !== relation) {
throw new Error(`Query error: cannot add ${newQuery}: alias already used by ${oldQuery}`);
}
if (join.condition !== condition) {
throw new Error(`Query error: cannot add ${newQuery}: relation already defined with different condition by ${oldQuery}`);
}
} else {
q.leftJoin(relation, alias, condition);
}