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

Method findChildren

packages/core/src/entity/EntityLoader.ts:555–755  ·  view source on GitHub ↗
(
    entities: Entity[],
    prop: EntityProperty<Entity>,
    populate: PopulateOptions<Entity>,
    options: Required<EntityLoaderOptions<Entity>>,
    ref: boolean,
  )

Source from the content-addressed store, hash-verified

553 }
554
555 private async findChildren<Entity extends object>(
556 entities: Entity[],
557 prop: EntityProperty<Entity>,
558 populate: PopulateOptions<Entity>,
559 options: Required<EntityLoaderOptions<Entity>>,
560 ref: boolean,
561 ): Promise<{ items: AnyEntity[]; partial: boolean }> {
562 const children = Utils.unique(this.getChildReferences<Entity>(entities, prop, options, ref, populate));
563 const meta = prop.targetMeta!;
564 // When targetKey is set, use it for FK lookup instead of the PK
565 let fk: string | string[] = prop.targetKey ?? Utils.getPrimaryKeyHash(meta.primaryKeys);
566 let schema: string | undefined = options.schema;
567 const partial = !Utils.isEmpty(prop.where) || !Utils.isEmpty(options.where);
568 let polymorphicOwnerProp: EntityProperty | undefined;
569 const ownerProp =
570 prop.kind === ReferenceKind.ONE_TO_MANY || (prop.kind === ReferenceKind.MANY_TO_MANY && !prop.owner)
571 ? meta.properties[prop.mappedBy]
572 : undefined;
573
574 if (ownerProp) {
575 if (ownerProp.polymorphic && ownerProp.fieldNames.length >= 2) {
576 const idColumns = ownerProp.fieldNames.slice(1);
577 fk = idColumns.length === 1 ? idColumns[0] : idColumns;
578 polymorphicOwnerProp = ownerProp;
579 } else {
580 fk = ownerProp.name;
581 }
582 }
583
584 if (prop.kind === ReferenceKind.ONE_TO_ONE && !prop.owner && (!ref || (!prop.mapToPk && !populate.filter))) {
585 children.length = 0;
586 fk = meta.properties[prop.mappedBy].name;
587 children.push(...(this.filterByReferences(entities, prop, options, ref) as AnyEntity[]));
588 }
589
590 if (children.length === 0) {
591 return { items: [], partial };
592 }
593
594 if (!schema && [ReferenceKind.ONE_TO_ONE, ReferenceKind.MANY_TO_ONE].includes(prop.kind)) {
595 schema = children.find(e => e.__helper!.__schema)?.__helper!.__schema;
596 }
597
598 // for inverse sides the `targetKey` lives on the owning property, otherwise on the prop itself
599 const targetKey = prop.targetKey ?? ownerProp?.targetKey;
600 const ids = Utils.unique(children.map(e => e.__helper.getTargetKeyValue(targetKey)));
601 let where: FilterQuery<Entity>;
602
603 if (polymorphicOwnerProp && Array.isArray(fk)) {
604 const conditions = ids.map(id => {
605 const pkValues = Object.values(id as Record<string, unknown>);
606 return Object.fromEntries(fk.map((col, idx) => [col, pkValues[idx]]));
607 });
608 where = (conditions.length === 1 ? conditions[0] : { $or: conditions }) as FilterQuery<Entity>;
609 } else {
610 where = this.mergePrimaryCondition<Entity>(
611 ids,
612 fk as FilterKey<Entity>,

Callers

nothing calls this directly

Calls 15

getChildReferencesMethod · 0.95
filterByReferencesMethod · 0.95
mergePrimaryConditionMethod · 0.95
buildFieldsMethod · 0.95
extractChildConditionMethod · 0.95
helperFunction · 0.85
getPrimaryKeyHashMethod · 0.80
sliceMethod · 0.80
pushMethod · 0.80
hasObjectFragmentsMethod · 0.80
mergeOrderByMethod · 0.80

Tested by

no test coverage detected