Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nestjsx/automapper
An Object-Object AutoMapper module for NestJS.
https://github.com/nestjsx/automapper
automapper nestjs typescript
Last synced: 6 days ago
JSON representation
An Object-Object AutoMapper module for NestJS.
- Host: GitHub
- URL: https://github.com/nestjsx/automapper
- Owner: nestjsx
- License: mit
- Archived: true
- Created: 2019-11-03T15:42:28.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-01-07T02:52:00.000Z (almost 4 years ago)
- Last Synced: 2024-10-29T22:37:19.381Z (14 days ago)
- Topics: automapper, nestjs, typescript
- Language: TypeScript
- Homepage: https://nestjsx.github.io/automapper/
- Size: 1.35 MB
- Stars: 122
- Watchers: 5
- Forks: 7
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-nestjs - Nest AutoMapper - NestJS 的 AutoMapper 模块。 (资源 / 组件和库)
README
NestJSX Automapper
A wrapper around @nartc/automapper to be used with NestJS as a
Module
.## Announcement
`@nartc/automapper` (which is what this wrapper wraps around) has been broken up into a monorepo and released as under the scope `@automapper/*`. With that change, NestJS wrapper is also under that same scope as `@automapper/nestjs`. Please take a look at the [documentations](https://automapperts.netlify.app) for Migration Guide.
## 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 information2. `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);
```