npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-simple-automapper

v1.0.3

Published

A simple AutoMapper implementation using property decorators.

Downloads

134

Readme

ts-simple-automapper

A simple AutoMapper implementation using property decorators.

The issue

When relying on the keys of a destination object alone to map only desired properties from a source object, if the property has not been initialized, the key will not exist and will not be mapped to.

For example:

class User {
    public password: string;
    public username: string;
}

class UserDto {
    public username: string;
}

const user = new User();
user.password = "guest";
user.username = "johndoe";

const userDto = new UserDto();

for (const propKey in userDto) {
    userDto[propKey] = user[propKey];
}

// Results in undefined since the username property was never initialized on the userDto object.
console.log(userDto.username);

Enter Mapper

The Mapper class processes existing keys on the destination object along with property mapping rules set up for source and destination objects via decorators.

class UserDto {
    @MapProp()
    public username: string;
}

import { Mapper } from "ts-simple-automapper";

const user: User;
const userDto: UserDto = new Mapper().map(user, new UserDto());

// Results in the value of user.username.
console.log(userDto.username);

// Map an array of source objects to an array of destination objects.
const users: User[];
const userDtos: UserDto[] = new Mapper().mapList(users, UserDto);

Decorators

Destination Decorators

For mapping values TO a property on the destination object FROM the corresponding property on the source object (or a custom mapping function), use the following decorators:

  • @MapProp(): Expose the property for mapping from a source object.
  • @Ignore(): Skip mapping to this property for either all source object types or, optionally, one source object type.
  • @MapFrom(): Map from the specified source type even if otherwise ignored; also use options for constructing a class type for the destination value and/or mapping from a custom property path.
class DestinationType {
    // Mapped even if not initialized prior to mapping.
    @MapProp()
    public mappedProp: string;

    // Ignored for all mappings.
    @Ignore()
    public ignoredForAllSourceTypes: string;

    // Ignored for only IgnoredSourceType.
    @Ignore(() => IgnoredSourceType)
    public ignoreForOneSourceType: string;

    // Ignored for only IgnoredSourceType
    // and OtherIgnoredSourceType.
    @Ignore(() => IgnoredSourceType)
    @Ignore(() => OtherIgnoredSourceType)
    public ignoreForTwoSourceTypes: string;

    // Ignored for all source types except for AllowedSourceType.
    @Ignore()
    @MapFrom(() => AllowedSourceType)
    public ignoredForAllExceptOneSourceType: string;

    // Ignored for all source types except for AllowedSourceType
    // and for OtherAllowedSourceType.
    @Ignore()
    @MapFrom(() => AllowedSourceType)
    @MapFrom(() => OtherAllowedSourceType)
    public ignoredForAllExceptTwoSourceTypes: string;
}

More advanced examples of MapFrom():

// Entities (source types)
class User {
    public profile: UserProfile;
}

class UserProfile {
    public firstName: string;
    public lastName: string;
    public status: UserStatus;
    public profile: UserProfile;
}

class UserStatus {
    public profile: UserProfile;
}

// DTOs (destination types)
class UserDto {
    // Map User.profile (a Profile object) as an instance of UserProfileDto.
    @MapFrom(() => User, { destinationValueTypeProvider: () => UserProfileDto })
    public profile: UserProfileDto;

    // Map UserDto.status from User.profile.status
    // and map it as an instance of UserStatusDto.
    @MapFrom(() => User, {
        destinationValueTypeProvider: () => UserStatusDto,
        mapFrom: u => u.profile.status
    })
    public status: UserStatusDto;
}

class UserProfileDto {
    // Map UserProfileDto.name as a concatenation of UserProfile.firstName
    // and UserProfile.lastName.
    @MapFrom(() => UserProfile, { mapFrom: up => `${up.firstName} ${up.lastName}` })
    public name: string;
}

class UserStatusDto {
}

Mapping Arrays using @MapFrom

Note that, when using the @MapFrom() decorator with a specified destinationValueTypeProvider, Mapper will map the object normally if the source value is not an array and will use mapList to map an array of objects of the specified destination type if the source value is an array.

class User {
    public attributes: UserAttribute[];
    public profile: UserProfile;
}

class UserProfile {
    public name: string;
    public user: User;
}

class UserAttribute {
    public attribute: string;
    public user: User;
}

class UserDto {
    @MapFrom(() => User, {
        destinationValueTypeProvider: () => UserAttributeDto
    })
    public attributes: UserAttributeDto[];

    @MapFrom(() => User, {
        destinationValueTypeProvider: () => UserProfileDto
    })
    public profile: UserProfileDto;
}

class UserProfileDto {
    @MapProp()
    public name: string;
}

class UserAttributeDto {
    @MapProp()
    public attribute: string;
}

// Given an existing User entity with all relationships included
// with an existing profile and at least one attribute:
const userDto = new Mapper().map(user, new UserDto());

// Logs true since user.profile was not an array, so mapList was not used.
console.log(userDto.profile instanceof UserProfileDto);
// Logs true since user.attributes was an array, so mapList was used.
console.log(userDto.attributes instanceof Array);
// Logs true as well; the desired class type constructor is used for arrays.
console.log(userDto.attributes[0] instanceof UserAttributeDto);

Source Decorators

To skip mapping a value FROM a property on the source object TO the corresponding property on the destination object, use the @Hide() decorator with an optional class type provider that hides the property only from the specified destination class type.

class SourceType {
    // Hidden from all destination types.
    @Hide()
    public hiddenFromAllDestinationTypes: string;

    // Hidden only from destination objects of type ForbiddenDestinationType.
    @Hide(() => ForbiddenDestinationType)
    public hiddenFromOneDestinationType: string;

    // Hidden only from destination objects of type ForbiddenDestinationType
    // or of type OtherForbiddenDestinationType.
    @Hide(() => ForbiddenDestinationType)
    @Hide(() => OtherForbiddenDestinationType)
    public hiddenFromTwoDestinationTypes: string;
}