🧙‍♂️
Dynamic Mapper
  • DynamicMapper
  • Getting started
  • Configuration
    • Member configuration
    • Subtyping
  • Profiles
  • Integrations
    • Angular
Powered by GitBook
On this page
  • Mapping configuration
  • Mapping

Was this helpful?

Getting started

PreviousDynamicMapperNextConfiguration

Last updated 5 years ago

Was this helpful?

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 .

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' }
here