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

Function buildPopulateWhere

packages/sql/src/AbstractSqlDriver.ts:3095–3133  ·  view source on GitHub ↗
(
    meta: EntityMetadata<T>,
    joinedProps: PopulateOptions<T>[],
    options: Pick<FindOptions<any>, 'populateWhere'>,
  )

Source from the content-addressed store, hash-verified

3093 }
3094
3095 protected buildPopulateWhere<T extends object>(
3096 meta: EntityMetadata<T>,
3097 joinedProps: PopulateOptions<T>[],
3098 options: Pick<FindOptions<any>, 'populateWhere'>,
3099 ): ObjectQuery<T> {
3100 const where = {} as ObjectQuery<T>;
3101
3102 for (const hint of joinedProps) {
3103 const [propName] = hint.field.split(':', 2) as [EntityKey<T>];
3104 const prop = meta.properties[propName];
3105
3106 if (!Utils.isEmpty(prop.where) || RawQueryFragment.hasObjectFragments(prop.where)) {
3107 where[prop.name] = Utils.copy(prop.where);
3108 }
3109
3110 if (hint.children) {
3111 const targetMeta = prop.targetMeta;
3112 if (targetMeta) {
3113 const inner = this.buildPopulateWhere(targetMeta, hint.children as any, {});
3114
3115 if (!Utils.isEmpty(inner) || RawQueryFragment.hasObjectFragments(inner)) {
3116 where[prop.name] ??= {} as any;
3117 Object.assign(where[prop.name] as object, inner);
3118 }
3119 }
3120 }
3121 }
3122
3123 if (Utils.isEmpty(options.populateWhere) && !RawQueryFragment.hasObjectFragments(options.populateWhere)) {
3124 return where;
3125 }
3126
3127 if (Utils.isEmpty(where) && !RawQueryFragment.hasObjectFragments(where)) {
3128 return options.populateWhere as ObjectQuery<T>;
3129 }
3130
3131 /* v8 ignore next */
3132 return { $and: [options.populateWhere, where] } as unknown as ObjectQuery<T>;
3133 }
3134
3135 /**
3136 * Builds a UNION ALL (or UNION) subquery from `unionWhere` branches and merges it

Callers

nothing calls this directly

Calls 4

hasObjectFragmentsMethod · 0.80
copyMethod · 0.80
assignMethod · 0.65
isEmptyMethod · 0.45

Tested by

no test coverage detected