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

Function mapJoinedProp

packages/sql/src/AbstractSqlDriver.ts:804–878  ·  view source on GitHub ↗

* Maps a single property from a joined result row into the relation pojo. * Handles polymorphic FKs, composite keys, Date parsing, and embedded objects.

(
    relationPojo: EntityData<T>,
    prop: EntityProperty<T>,
    relationAlias: string,
    root: EntityData<T>,
    tz: string | undefined,
    meta: EntityMetadata<T>,
    options?: { deleteFromRoot?: boolean },
  )

Source from the content-addressed store, hash-verified

802 * Handles polymorphic FKs, composite keys, Date parsing, and embedded objects.
803 */
804 private mapJoinedProp<T extends object>(
805 relationPojo: EntityData<T>,
806 prop: EntityProperty<T>,
807 relationAlias: string,
808 root: EntityData<T>,
809 tz: string | undefined,
810 meta: EntityMetadata<T>,
811 options?: { deleteFromRoot?: boolean },
812 ): void {
813 if (prop.fieldNames.every(name => typeof root[`${relationAlias}__${name}` as EntityKey<T>] === 'undefined')) {
814 return;
815 }
816
817 if (prop.polymorphic && prop.kind !== ReferenceKind.EMBEDDED) {
818 const discriminatorAlias = `${relationAlias}__${prop.fieldNames[0]}` as EntityKey<T>;
819 const discriminatorValue = root[discriminatorAlias] as string;
820 const pkFieldNames = prop.fieldNames.slice(1);
821 const pkValues = pkFieldNames.map(name => root[`${relationAlias}__${name}` as EntityKey<T>]);
822 const pkValue = pkValues.length === 1 ? pkValues[0] : pkValues;
823
824 if (discriminatorValue != null && pkValue != null) {
825 relationPojo[prop.name] = new PolymorphicRef(discriminatorValue, pkValue) as EntityDataValue<T>;
826 } else {
827 relationPojo[prop.name] = null;
828 }
829 } else if (prop.fieldNames.length > 1) {
830 // composite keys
831 const fk = prop.fieldNames.map(name => root[`${relationAlias}__${name}` as EntityKey<T>]) as Primary<T>[];
832 // `mapFlatCompositePrimaryKey` collapses to a scalar when the target PK is a single prop
833 // (e.g. a relation referencing a composite unique key on a single-PK target).
834 const pk = Utils.mapFlatCompositePrimaryKey(fk, prop);
835 const valid = Array.isArray(pk) ? pk.every(val => val != null) : pk != null;
836 relationPojo[prop.name] = valid ? (pk as EntityDataValue<T>) : null;
837 } else if (prop.runtimeType === 'Date') {
838 const alias = `${relationAlias}__${prop.fieldNames[0]}` as EntityKey<T>;
839 const value = root[alias] as unknown;
840
841 if (
842 tz &&
843 tz !== 'local' &&
844 typeof value === 'string' &&
845 !value.includes('+') &&
846 value.lastIndexOf('-') < 11 &&
847 !value.endsWith('Z')
848 ) {
849 relationPojo[prop.name] = this.platform.parseDate(value + tz) as EntityDataValue<T>;
850 } else if (['string', 'number'].includes(typeof value)) {
851 relationPojo[prop.name] = this.platform.parseDate(value as string) as EntityDataValue<T>;
852 } else {
853 relationPojo[prop.name] = value as EntityDataValue<T>;
854 }
855 } else {
856 const alias = `${relationAlias}__${prop.fieldNames[0]}` as EntityKey<T>;
857 relationPojo[prop.name] = root[alias];
858
859 if (prop.kind === ReferenceKind.EMBEDDED && (prop.object || meta.embeddable)) {
860 const item = parseJsonSafe(relationPojo[prop.name]);
861

Callers

nothing calls this directly

Calls 6

parseJsonSafeFunction · 0.90
sliceMethod · 0.80
mapResultMethod · 0.65
mapMethod · 0.45
parseDateMethod · 0.45

Tested by

no test coverage detected