Typevert
Define Object to Object mapping using Typescript decorators
Basic usage
; ;;;
Motivation
A common problem when converting classes one to another is that you have to write a lot of boilerplate mapping functions or converters. For example mapping Mongo Entities to your internal objects could produce a significant number of nested mappings, null checks, etc.
Typevert aims to solve this problem.
Instead of:
You can just write:
Requirements
- TypeScript 3.2+
- Node 8+
emitDecoratorMetadata
andexperimentalDecorators
must be enabled intsconfig.json
Install
npm install typevert -S
Documentation
Mapper decorator
The @Mapper decorator adds the target class the mappingFunction method. It admits converting your source object to target according to the mappings.
Mapper decorator checks that your class is a child of the Converter<IN, OUT> abstract class.
Decorator accepts two arguments:
-
MapperDeclaration - which contains mapper description: name, source type and target type
-
Mappings - array of rules how to convert one field to another
Options in mapping rules have order when
expr
andconverter
are present :- Exec converting by
expr
- Exec converting by
converter.convert
Additionally:
- If source field is null then default value will be set and none
expr
orconverter
called - If isCollection then for each object in collection mapping will be performed
- If none
expr
orconverter
is set and field value is not null then common assigment is performing
- Exec converting by