model-converter-ts
v2.0.0
Published
Reduce boiler plate on converter
Downloads
2
Readme
model-converter-ts
A Node.js module that converter models
Installation
npm install model-converter-ts
Implementation
import {ModelConverter} from 'model-converter-ts';
import {Person, PersonDTO} from './models';
class PersonModelConverter extends ModelConverter<Person, PersonDTO, Context> {
public update(dto: PersonDTO, person: Person, context?: Context):void {
if(!person){
return;
}
dto.age = parseInt(person.age, 10);
dto.dob = new Date(person.dobAsLong);
dto.email = person.email;
dto.married = (person.status === 'married');
dto.name = person.firstName + ' ' + person.lastName;
dto.parent = context.parent;
}
}
Usage
import PersonModelConverter from './PersonModelConverter';
import PersonService from './PersonService';
import {Person, PersonDTO} from './models';
const converter = new PersonModelConverter();
const service = new PersonService();
// Convert Entity to DTO
const dto = converter.connvert(service.findPersonById(1));
// Convert Array of Entities to DTOs
const dtos = converter.connvertList(service.findAllPersons());
// Update dto from entity
const personDTO = new PersonDTO();
converter.update(personDTO, service.findPersonById(1))
Motivation
SRP Pattern
License
MIT (unless noted otherwise)