# nestjsx-automapper

Latest version **3.1.4** (published 2020-10-21) · MIT license · 241 weekly downloads

## Install

```sh
npm install nestjsx-automapper
pnpm add nestjsx-automapper
yarn add nestjsx-automapper
bun add nestjsx-automapper
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.4 |
| Published | 2020-10-21 |
| First published | 2019-11-06 |
| Weekly downloads | 241 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=8.0.0 |
| Dependencies | 1 |
| Unpacked size | 32 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 120 |
| Author | Chau Tran |
| Maintainers | nartc |
| Keywords | nestjs, automapper, typescript |

## Links

- npm: https://www.npmjs.com/package/nestjsx-automapper
- Repository: https://github.com/nestjsx/automapper
- Homepage: https://github.com/nestjsx/automapper#readme
- Issues: https://github.com/nestjsx/automapper/issues
- npm.io page: https://npm.io/package/nestjsx-automapper

## Dependencies (1)

- [@nartc/automapper](https://npm.io/package/@nartc/automapper.md) 7.0.4

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 3.1.4 (latest) — 2020-10-21
- 3.0.0-next.4 (next) — 2020-03-31
- 3.1.3 — 2020-09-22
- 3.1.2 — 2020-09-13
- 3.1.1 — 2020-09-06
- 3.1.0 — 2020-08-23
- 3.0.23 — 2020-08-16
- 3.0.22 — 2020-08-11
- 3.0.21 — 2020-08-01
- 3.0.20 — 2020-07-27
- 3.0.19 — 2020-07-20
- 3.0.18 — 2020-07-16
- 3.0.17 — 2020-07-12
- 3.0.16 — 2020-07-04
- 3.0.15 — 2020-06-29
- … 58 more at https://npm.io/package/nestjsx-automapper/versions

## README

<p align="center"><img src="https://avatars1.githubusercontent.com/u/43827489?s=400&u=45ac0ac47d40b6d8f277c96bdf00244c10508aef&v=4"/></p>
<p align="center">
    <a href="https://badgen.net/travis/nestjsx/automapper"><img src="https://badgen.net/travis/nestjsx/automapper" alt="travis"/></a>
    <a href="https://badgen.net/npm/v/nestjsx-automapper"><img src="https://badgen.net/npm/v/nestjsx-automapper" alt="npm"/></a>
    <a href="https://badgen.net/npm/dt/nestjsx-automapper"><img src="https://badgen.net/npm/dt/nestjsx-automapper" alt="downloads"/></a>
    <a href="https://badgen.net/bundlephobia/minzip/nestjsx-automapper"><img src="https://badgen.net/bundlephobia/minzip/nestjsx-automapper" alt="bundlephobia"/></a>
    <a href="https://badgen.net/github/license/nestjsx/automapper"><img src="https://badgen.net/github/license/nestjsx/automapper" alt="license"/></a>
    <a href="https://coveralls.io/repos/github/nestjsx/automapper/badge.svg?branch=master"><img src="https://coveralls.io/repos/github/nestjsx/automapper/badge.svg?branch=master" alt="coveralls"/></a>
    <a href="https://greenkeeper.io/"><img src="https://badges.greenkeeper.io/nestjsx/automapper.svg" alt="Greenkeeper"/></a>
</p>
<h1 align="center">NestJSX Automapper</h1>
<p align="center">A wrapper around <a href="https://github.com/nartc/mapper">@nartc/automapper</a> to be used with <strong>NestJS</strong> as a <code>Module</code>.</p>

## 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/](https://automapper.netlify.com/)
Github Repo [https://github.com/nartc/mapper](https://github.com/nartc/mapper)

## Features

- [x] Mapping between two classes
- [x] Mapping for nested classes
- [x] Array/List Mapping
- [x] Flattening
- [x] Basic ReverseMap
- [x] Value Converters
- [x] Value Resolvers
- [x] Async
- [x] Before/After Callback
- [x] 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

```typescript
@Module({
  imports: [AutomapperModule.withMapper()],
})
export class AppModule {}
```

`AutomapperModule.withMapper()` has the following overloads:

```typescript
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](https://automapper.netlify.app/docs/usages/init/with-global-settings/) for more information

2. `nestjsx-automapper` exposes a `@Profile()` decorator to decorate your `Profile` classes.

```typescript
@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).

3. Inject the `AutoMapper` instance in your `Injectable`

```typescript
@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`

4. Use `AutoMapper` on your models

```typescript
// ...
const result = await newUser.save();
return this.mapper.map(result.toJSON(), UserVm, User);
```

---
_Source: https://npm.io/package/nestjsx-automapper · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
