Create mappings
Register nested and top-level mappings, then map a source object to its destination type.
Create mappings once during application startup. Register a nested mapping before a mapping that depends on it.
import { createMap } from '@automapper/core';
import { mapper } from './mapper';
import { Bio, User } from '../models/user';
import { BioDto, UserDto } from '../models/user.dto';
createMap(mapper, Bio, BioDto);
createMap(mapper, User, UserDto);
createMap(mapper, Source, Destination) registers and returns a unidirectional mapping. Register the reverse direction separately if you need it.
Execute the mapping
const dto = mapper.map(user, User, UserDto);
Matching metadata maps automatically:
firstName,lastName, andusernamecopy by name;passwordis not part of destination metadata;biouses the registeredBiotoBioDtomapping.
Three differences remain:
fullNameis computed;birthdaychanges fromDatetostring;jobTitleandjobSalaryflatten values fromjob.
The next page configures those differences.