Getting started
Let's take the following source and destination types:
interface CustomerDto {
firstName: string;
lastName: string;
}
interface Customer {
firstName: string;
lastName: string;
fullName: string;
}
TypeScript static typings are not available at runtime. To introduce a runtime representation of mapping from CustomerDto
to Customer
a MappingPair
token has to be created.
import { MappingPair } from '@dynamic-mapper/mapper';
const CustomerDtoToCustomer = new MappingPair<CustomerDto, Customer>();
Mapping configuration
Configuring strategies are described here.
import { MapperConfiguration } from '@dynamic-mapper/mapper';
const configuration = new MapperConfiguration(cfg => {
cfg.createAutoMap(CustomerDtoToCustomer, {
fullName: opt => opt.mapFrom(src => `${src.firstName} ${src.lastName}`)
});
});
Mapping
const mapper = configuration.createMapper();
const customer: CustomerDto = {
firstName: 'John',
lastName: 'Doe'
};
const dto = mapper.map(CustomerDtoToCustomer, customer);
console.log(dto);
// { firstName: 'John', lastName: 'Doe', fullName: 'John Doe' }
Last updated
Was this helpful?