Takes as a parameter an entity and an object, maps the object and returns the entity with the updated properties. Considers empty values sent, but non-entity-type properties are discarded.
No mapper:
constexample:Example= { name:"example", description:"this is an example", extra:undefined, size:"123", price:100,};constinputData:IExampleDTO= { name:undefined, description:"this is a new description", extra:"this is an extra", size:undefined, price:20,};awaitthis.examplesRepository.update( { name:inputData.name, description:inputData.name,// Possible human error extra:inputData.extra, size:inputData.size, price:inputData.price, }, trx,);output = { name:"", description:"", extra:"this is an extra", size:"", price:20,};
Using mapper:
import { updateAttribute } from"@utils/mappers";constexample:Example= { name:"example", description:"this is an example", extra:undefined, size:"123", price:100,};constinputData:IExampleDTO= { name:undefined, description:"this is a new description", extra:"this is an extra", size:undefined, price:20, nonEntityFieldSent_1:"1",// It also prevents against non-related inputs nonEntityFieldSent_2:"2", nonEntityFieldSent_3:"3", nonEntityFieldSent_4:"4",};awaitthis.examplesRepository.update(updateAttribute(example, inputData), trx,);output = { name:"", description:"this is a new description", extra:"this is an extra", size:"", price:20,};