Exposing get fields
It would be great to be able to deliver calculations as fields, as though they were attributes.
In the order-item.entity file, we still need to add the get
field that calculates the subTotal
of an item.
And in order.entity, calculate the total
by adding the subTotals of its items
.
Due to the optional chaining operator (
?
), this calculation only occurs if theitems
field is present. This is a good safety measure asitems
is a relation and may not be present.
It would be even better if these get
fields were automatically included in their respective entities when they are fetched, like if they were attributes. To achieve this, we first need to enable the ClassSerializerInterceptor
globally. We can do this in the providers
array of the CommonModule
.
After that, we just need to add on top of both these fields the @Expose()
decorator, and we're done.
Commit - Using serialization to return get fields
Last updated