UpdateAttribute

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:

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, // 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";

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", // It also prevents against non-related inputs
  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,
};