MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / mergeOnConditions

Method mergeOnConditions

packages/sql/src/query/QueryBuilder.ts:3865–3904  ·  view source on GitHub ↗
(joins: JoinOptions[], cond: Dictionary, filter: boolean, op?: string)

Source from the content-addressed store, hash-verified

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,

Callers 1

processPopulateWhereMethod · 0.95

Calls 6

isOperatorMethod · 0.80
splitFieldMethod · 0.80
addMethod · 0.80
pushMethod · 0.80
findMethod · 0.65
keysMethod · 0.45

Tested by

no test coverage detected