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

Method buildFields

packages/core/src/entity/EntityLoader.ts:1031–1082  ·  view source on GitHub ↗
(
    fields: readonly EntityField<Entity>[] = [],
    prop: EntityProperty<Entity>,
    ref?: boolean,
  )

Source from the content-addressed store, hash-verified

1029 }
1030
1031 private buildFields<Entity>(
1032 fields: readonly EntityField<Entity>[] = [],
1033 prop: EntityProperty<Entity>,
1034 ref?: boolean,
1035 ): readonly EntityField<Entity>[] | undefined {
1036 if (ref) {
1037 fields = prop.targetMeta!.primaryKeys.map(
1038 targetPkName => `${prop.name}.${targetPkName}`,
1039 ) as EntityField<Entity>[];
1040 }
1041
1042 const ret = fields.reduce((ret, f) => {
1043 if (Utils.isPlainObject(f)) {
1044 Utils.keys(f)
1045 .filter(ff => ff === prop.name)
1046 .forEach(ff => ret.push(...(f[ff] as EntityField<Entity>[])));
1047 } else if (f.toString().includes('.')) {
1048 const parts = f.toString().split('.');
1049 const propName = parts.shift();
1050 const childPropName = parts.join('.') as EntityField<Entity>;
1051
1052 /* v8 ignore next */
1053 if (propName === prop.name) {
1054 ret.push(childPropName);
1055 }
1056 }
1057
1058 return ret;
1059 }, [] as EntityField<Entity>[]);
1060
1061 // we need to automatically select the FKs too, e.g. for 1:m and inverse 1:1 relations
1062 // to be able to wire them with the items
1063 if (
1064 prop.kind === ReferenceKind.ONE_TO_MANY ||
1065 prop.kind === ReferenceKind.MANY_TO_MANY ||
1066 (prop.kind === ReferenceKind.ONE_TO_ONE && !prop.owner)
1067 ) {
1068 const owner = prop.targetMeta!.properties[prop.mappedBy] as EntityProperty<Entity>;
1069
1070 // when the owning FK is lazy, we need to explicitly select it even without user-provided fields,
1071 // otherwise the driver will exclude it and we won't be able to map children to their parent collections
1072 if (owner && !ret.includes(owner.name) && (ret.length > 0 || owner.lazy)) {
1073 ret.push(owner.name);
1074 }
1075 }
1076
1077 if (ret.length === 0) {
1078 return undefined;
1079 }
1080
1081 return ret;
1082 }
1083
1084 private getChildReferences<Entity extends object>(
1085 entities: Entity[],

Callers 3

findChildrenMethod · 0.95
populateFieldMethod · 0.95

Calls 8

reduceMethod · 0.80
isPlainObjectMethod · 0.80
filterMethod · 0.80
pushMethod · 0.80
joinMethod · 0.65
mapMethod · 0.45
keysMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected