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

Method populateField

packages/core/src/entity/EntityLoader.ts:778–902  ·  view source on GitHub ↗
(
    entityName: EntityName<Entity>,
    entities: Entity[],
    populate: PopulateOptions<Entity>,
    options: Required<EntityLoaderOptions<Entity>>,
  )

Source from the content-addressed store, hash-verified

776 }
777
778 private async populateField<Entity extends object>(
779 entityName: EntityName<Entity>,
780 entities: Entity[],
781 populate: PopulateOptions<Entity>,
782 options: Required<EntityLoaderOptions<Entity>>,
783 ): Promise<void> {
784 const field = populate.field.split(':')[0] as EntityKey<Entity>;
785 const prop = this.#metadata.find(entityName)!.properties[field] as EntityProperty<Entity>;
786
787 if (prop.kind === ReferenceKind.SCALAR && !prop.lazy) {
788 return;
789 }
790
791 options = { ...options, filters: QueryHelper.mergePropertyFilters(prop.filters, options.filters)! };
792 const populated = await this.populateMany<Entity>(entityName, entities, populate, options);
793
794 if (!populate.children && !populate.all) {
795 return;
796 }
797
798 const children: Entity[] = [];
799
800 for (const entity of entities) {
801 const ref = entity[field] as unknown;
802
803 if (Utils.isEntity<Entity>(ref)) {
804 children.push(ref);
805 } else if (Reference.isReference<Entity>(ref)) {
806 children.push(ref.unwrap());
807 } else if (Utils.isCollection<Entity>(ref)) {
808 children.push(...ref.getItems());
809 } else if (ref && prop.kind === ReferenceKind.EMBEDDED) {
810 children.push(...Utils.asArray(ref as Entity));
811 }
812 }
813
814 if (populated.length === 0 && !populate.children) {
815 // Populate child-specific relations for TPT entities already in the identity map (GH #7529).
816 // Pass a sentinel hint so the existing TPT child handling in `populate()` does the real work.
817 if (populate.all && prop.targetMeta?.inheritanceType === 'tpt') {
818 const visited = (options as Dictionary).visited as Set<AnyEntity>;
819 const pending = Utils.unique(children).filter(
820 c => helper(c as AnyEntity).__meta !== prop.targetMeta && !visited.has(c as AnyEntity),
821 );
822
823 if (pending.length > 0) {
824 const hint = [
825 { field: prop.targetMeta.primaryKeys[0], strategy: LoadStrategy.SELECT_IN, all: true },
826 ] as PopulateOptions<Entity>[];
827 await this.populate(prop.targetMeta.class as EntityName<Entity>, pending as Entity[], hint, {
828 ...options,
829 lookup: false,
830 validate: false,
831 });
832 }
833 }
834
835 return;

Callers

nothing calls this directly

Calls 15

populateMethod · 0.95
buildFieldsMethod · 0.95
helperFunction · 0.85
mergePropertyFiltersMethod · 0.80
pushMethod · 0.80
isReferenceMethod · 0.80
asArrayMethod · 0.80
filterMethod · 0.80
isObjectMethod · 0.80
extractChildElementsMethod · 0.80
addMethod · 0.80
findMethod · 0.65

Tested by

no test coverage detected