Skip to main content

Self Mapping (Same Identifier)

In some cases, we might want to map a model (identifier) to itself. In AutoMapper TypeScript, this is called Self Mapping. Let's explore the following models

export class Person {
@AutoMap()
name!: string; // always required
@AutoMap()
nickname?: string; // can be optional
}

export class Org {
@AutoMap(() => [Person])
people!: Person[];
}

export class OrgDto {
@AutoMap(() => [Person])
people!: Person[];
}

Instead of having a PersonDto, our Org and OrgDto use Person for the field people. There are situations where this is the case. With this in mind, we can create our mappings as follow:

/**
* Short-hand syntax for
*
* createMap(
* mapper,
* Person,
* Person,
* forMember(...)
* )
*/
createMap(mapper, Person, forMember(d => d.nickname, mapFrom(s => s.nickname ?? s.name));
createMap(mapper, Org, OrgDto);

We can then map Org to OrgDto as normal

const dto = mapper.map(org, Org, OrgDto);
// we can also map the "people"
/**
* Short-hand syntax for: mapper.mapArray(org.people, Person, Person);
*/
const mappedPeople = mapper.mapArray(org.people, Person);