Toma como parâmetro um objeto stringificado e outro objeto, converte, mapeia e retorna o objeto stringificado com as propriedades atualizadas. As propriedades desconexas são descartadas.
Sem mapeador:
constexample:Example= { data: "{\"name\": \"example\",\"description\": \"this is an example\",\"extra\": \"\",\"size\": \"123\", \"price\": 100}",
}constinputData:IExampleDTO= { name:undefined, description:"this is a new description", extra:"this is an extra", size:undefined, price:20,};constupdatedData=JSON.parse(example.data)updatedData.name =inputData.name,updatedData.description =inputData.description,updatedData.extra =inputData.extra,updatedData.size =inputData.size,updatedData.price =inputData.price,awaitthis.examplesRepository.update( {...example, data:JSON.stringify(updatedData), }, trx,);output => example.data = "{\"name\": \"\",\"description\": \"this is a new description\",\"extra\": \"this is an extra\",\"size\": \"\", \"price\": 20}";
Usando mapeador:
import { updateString } from"@utils/mappers";constexample:Example= { data: "{\"name\": \"example\",\"description\": \"this is an example\",\"extra\": \"\",\"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( {...example, data:updateString(example.data, inputData), }, trx,);output => example.data = "{\"name\": \"\",\"description\": \"this is a new description\",\"extra\": \"this is an extra\",\"size\": \"\", \"price\": 20}"