(joins: JoinOptions[], cond: Dictionary, filter: boolean, op?: string)
| 3863 | } |
| 3864 | |
| 3865 | private mergeOnConditions(joins: JoinOptions[], cond: Dictionary, filter: boolean, op?: string) { |
| 3866 | for (const k of Object.keys(cond)) { |
| 3867 | if (Utils.isOperator(k)) { |
| 3868 | if (Array.isArray(cond[k])) { |
| 3869 | cond[k].forEach((c: Dictionary) => this.mergeOnConditions(joins, c, filter, k)); |
| 3870 | } |
| 3871 | |
| 3872 | /* v8 ignore next */ |
| 3873 | this.mergeOnConditions(joins, cond[k], filter, k); |
| 3874 | } |
| 3875 | |
| 3876 | const [alias] = this.helper.splitField(k as EntityKey<Entity>); |
| 3877 | const join = joins.find(j => j.alias === alias); |
| 3878 | |
| 3879 | if (join) { |
| 3880 | const parentJoin = joins.find(j => j.alias === join.ownerAlias); |
| 3881 | |
| 3882 | // https://stackoverflow.com/a/56815807/3665878 |
| 3883 | if (parentJoin && !filter) { |
| 3884 | const nested = (parentJoin.nested ??= new Set()); |
| 3885 | join.type = |
| 3886 | join.type === JoinType.innerJoin || |
| 3887 | [ReferenceKind.ONE_TO_MANY, ReferenceKind.MANY_TO_MANY].includes(parentJoin.prop.kind) |
| 3888 | ? JoinType.nestedInnerJoin |
| 3889 | : JoinType.nestedLeftJoin; |
| 3890 | nested.add(join); |
| 3891 | } |
| 3892 | |
| 3893 | if (join.cond[k]) { |
| 3894 | /* v8 ignore next */ |
| 3895 | join.cond = { [op ?? '$and']: [join.cond, { [k]: cond[k] }] }; |
| 3896 | } else if (op === '$or') { |
| 3897 | join.cond.$or ??= []; |
| 3898 | join.cond.$or.push({ [k]: cond[k] }); |
| 3899 | } else { |
| 3900 | join.cond = { ...join.cond, [k]: cond[k] }; |
| 3901 | } |
| 3902 | } |
| 3903 | } |
| 3904 | } |
| 3905 | |
| 3906 | /** |
| 3907 | * When adding an inner join on a left joined relation, we need to nest them, |
no test coverage detected