lundi 21 février 2022

Using different DTO representations for get/create/update of an object?

Let say that i have object A that has a reference to object B (in db).

interface A {
   id: string;
   bId: string;
}

interfaceB {
   id: string;
   name: string;
   // other properties
}

When creating object A, i have an autocomplete component (with API lookup). In this situation i just need bId property of an object B in order to store it in a db).

When i want to edit object A, or to show it (as a single entity or in a table) i need an object B (or a partial object) in order to preselect autocomplete component (for autocomplete component i need id and name property). Also if i want to display object A in a table, i just need a name property from B.

1 - Have one DTO for everything (get/post/put), in that case :

interface A {
  id: string,
  bId: Partial<B> // in partial object i will have just {name and id}
 }

For post/put calls, a payload would be for example {id: 3, {id: 129}} and for get requests data would be {id: 3, {id: 129, name: 'Something'}}

2 - Have different DTOs (that are basically the same in all but in one property)

3 - Something else?

What would be a good practise for this?

Aucun commentaire:

Enregistrer un commentaire