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

Method populateMany

packages/core/src/entity/EntityLoader.ts:281–356  ·  view source on GitHub ↗

* preload everything in one call (this will update already existing references in IM)

(
    entityName: EntityName<Entity>,
    entities: Entity[],
    populate: PopulateOptions<Entity>,
    options: Required<EntityLoaderOptions<Entity>>,
  )

Source from the content-addressed store, hash-verified

279 * preload everything in one call (this will update already existing references in IM)
280 */
281 private async populateMany<Entity extends object>(
282 entityName: EntityName<Entity>,
283 entities: Entity[],
284 populate: PopulateOptions<Entity>,
285 options: Required<EntityLoaderOptions<Entity>>,
286 ): Promise<AnyEntity[]> {
287 const [field, ref] = populate.field.split(':', 2) as [EntityKey<Entity>, string | undefined];
288 const meta = this.#metadata.find<Entity>(entityName)!;
289 const prop = meta.properties[field];
290
291 if (prop.kind === ReferenceKind.MANY_TO_MANY && prop.owner && !this.#driver.getPlatform().usesPivotTable()) {
292 const filtered = entities.filter(e => !(e[prop.name] as Collection<any>)?.isInitialized());
293
294 if (filtered.length > 0) {
295 await this.populateScalar(meta, filtered, { ...options, fields: [prop.name] as any });
296 }
297 }
298
299 if (prop.kind === ReferenceKind.SCALAR && prop.lazy) {
300 const filtered = entities.filter(
301 e =>
302 options.refresh ||
303 (prop.ref ? !(e[prop.name] as ScalarReference<any>)?.isInitialized() : e[prop.name] === undefined),
304 );
305
306 if (options.ignoreLazyScalarProperties || filtered.length === 0) {
307 return entities as AnyEntity[];
308 }
309
310 await this.populateScalar(meta, filtered, { ...options, fields: [prop.name] as any });
311
312 return entities as AnyEntity[];
313 }
314
315 if (prop.kind === ReferenceKind.EMBEDDED) {
316 return [];
317 }
318
319 const filtered = this.filterCollections<Entity>(entities, field, options, ref);
320 const innerOrderBy = Utils.asArray(options.orderBy)
321 .filter(
322 orderBy =>
323 (Array.isArray(orderBy[prop.name]) && (orderBy[prop.name] as unknown[]).length > 0) ||
324 Utils.isObject(orderBy[prop.name]),
325 )
326 .flatMap(orderBy => orderBy[prop.name]);
327 const where = await this.extractChildCondition(options, prop);
328 const mergedOrderBy = populate.orderBy ? Utils.asArray(populate.orderBy) : innerOrderBy;
329
330 if (prop.kind === ReferenceKind.MANY_TO_MANY && this.#driver.getPlatform().usesPivotTable()) {
331 const pivotOrderBy = QueryHelper.mergeOrderBy<Entity>(mergedOrderBy, prop.orderBy, prop.targetMeta?.orderBy);
332 const res = await this.findChildrenFromPivotTable<Entity>(filtered, prop, options, pivotOrderBy, populate, !!ref);
333 return Utils.flatten(res);
334 }
335
336 if (prop.polymorphic && prop.polymorphTargets) {
337 return this.populatePolymorphic<Entity>(entities, prop, options, !!ref);
338 }

Callers

nothing calls this directly

Calls 13

populateScalarMethod · 0.95
filterCollectionsMethod · 0.95
extractChildConditionMethod · 0.95
populatePolymorphicMethod · 0.95
initializeCollectionsMethod · 0.95
filterMethod · 0.80
asArrayMethod · 0.80
isObjectMethod · 0.80
mergeOrderByMethod · 0.80
findMethod · 0.65
getPlatformMethod · 0.65
isInitializedMethod · 0.65

Tested by

no test coverage detected