| 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[], |