GithubHelp home page GithubHelp logo

tiagoha / automapper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nestjsx/automapper

0.0 1.0 0.0 1.61 MB

An Object-Object AutoMapper module for NestJS.

Home Page: https://nestjsx.github.io/automapper/

License: MIT License

TypeScript 98.01% JavaScript 1.99%

automapper's Introduction

travis npm downloads bundlephobia license coveralls Greenkeeper

NestJSX Automapper

A wrapper around @nartc/automapper to be used with NestJS as a Module.

Migrations

Migrate to v3

forRoot() method will be deprecated soon (still available in v3). Please use withMapper() instead.

Documentations

This module is a wrapper around @nartc/automapper so all usage documentations should be referenced at the link below.

Github Pages https://automapper.netlify.com/ Github Repo https://github.com/nartc/mapper

Features

  • Mapping between two classes
  • Mapping for nested classes
  • Array/List Mapping
  • Flattening
  • Basic ReverseMap
  • Value Converters
  • Value Resolvers
  • Async
  • Before/After Callback
  • Naming Conventions (PascalCase and camelCase)

Contributions are appreciated.

Setup

npm i -s nestjsx-automapper

Installing nestjsx-automapper will also install @nartc/automapper.

Note 1: Please make sure that you've read @nartc/automapper documentations to familiarize yourself with AutoMapper's terminology and how to setup your Profile and such.

Setup

  1. Import AutomapperModule in AppModule and call .withMapper() method
@Module({
  imports: [AutomapperModule.withMapper()],
})
export class AppModule {}

AutomapperModule.withMapper() has the following overloads:

static withMapper(name?: string, options?: AutoMapperGlobalSettings);
static withMapper(options?: AutoMapperGlobalSettings);
  • name: Name of the AutoMapper instance being created with withMapper(). Default to "default"
  • options: Check AutoMapperGlobalSettings for more information
  1. nestjsx-automapper exposes a @Profile() decorator to decorate your Profile classes.
@Profile()
class UserProfile extends ProfileBase {}

@Profile() takes in an optional name argument. This is the name if the AutoMapper instance you use to create the instance with withMapper(). Default to "default"

Usually, NestJS will have many Feature Modules for each of the Domain Models. Hence, a Profile should stay in close to where the feature module is. If you want to separate Profile out to a separate file, then you need to make sure that file gets executed by importing it somewhere (again, the module is a good place).

  1. Inject the AutoMapper instance in your Injectable
@Injectable()
export class UserService {
  constructor(@InjectMapper() private readonly mapper: AutoMapper) {}
}

@InjectMapper() takes in an optional name argument which will tell the decorator which AutoMapper instance to inject. Default to "default"

InjectMapper is imported from nestjsx-automapper. AutoMapper is imported from @nartc/automapper

  1. Use AutoMapper on your models
// ...
const result = await newUser.save();
return this.mapper.map(result.toJSON(), UserVm, User);

automapper's People

Contributors

bashleigh avatar dependabot-preview[bot] avatar dependabot[bot] avatar greenkeeper[bot] avatar nartc avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.