UpdateAttribute

Toma como parâmetro uma entidade e um objeto, mapeia o objeto e retorna a entidade com as propriedades atualizadas. Considera valores vazios enviados, mas propriedades desconexas são descartadas.

Sem mapeador:

const example: Example = {
  name: "example",
  description: "this is an example",
  extra: undefined,
  size: "123",
  price: 100,
};

const inputData: IExampleDTO = {
  name: undefined,
  description: "this is a new description",
  extra: "this is an extra",
  size: undefined,
  price: 20,
};

await this.examplesRepository.update(
  {
    name: inputData.name,
    description: inputData.name, // Possível erro humano
    extra: inputData.extra,
    size: inputData.size,
    price: inputData.price,
  },
  trx,
);

output = {
  name: "",
  description: "",
  extra: "this is an extra",
  size: "",
  price: 20,
};

Usando mapeador:

import { updateAttribute } from "@utils/mappers";

const example: Example = {
  name: "example",
  description: "this is an example",
  extra: undefined,
  size: "123",
  price: 100,
};

const inputData: IExampleDTO = {
  name: undefined,
  description: "this is a new description",
  extra: "this is an extra",
  size: undefined,
  price: 20,
  nonEntityFieldSent_1: "1", // Também previne contra dados não relacionados
  nonEntityFieldSent_2: "2",
  nonEntityFieldSent_3: "3",
  nonEntityFieldSent_4: "4",
};

await this.examplesRepository.update(
  updateAttribute(example, inputData),
  trx,
);

output = {
  name: "",
  description: "this is a new description",
  extra: "this is an extra",
  size: "",
  price: 20,
};