Computed property#
Computed property#
You can expose methods that will be lazily called them after the entity they belong to has been retrieved.
Prefixing your method's name by get|is|has and then using PascalCase will define the entity
key under which the method's result will be set in the response. If you don't want to use one of these prefix, you can
use the 2nd arg of the @Groups decorator to define an alias that will be used as entity key
in entity-routes responses.
Asynchrone#
A computed property can be asynchrone, in which case the response will end for the Promise to be done before sending
the response. In case there are multiple async computed properties, they will be processed in parallel to keep things
sane, but stil wait for all of them to be done.
Currently, if computed property (both async & sync) somehow throws an error, the error is simply ignored. It is not
recommended to abuse computed properties as it can make responses time slow, especially when working with
collections.
With dependencies#
There is one caveat though : What if your computed property depends on another property that is not exposed (through
@Groups) ? You could always just decorate the properties you depend onto with a
@Groups, but then you will end up having those properties returned in the response and you
might not want that. That's what the @DependsOn decorator is for, selecting needed properties from the
database table when the decorated computed property is exposed without returning them.
Basic example#
Here, the getFullName accessor will be exposed as fullName while the hasAdminRight will be displayed as isAdmin
in responses.
Async example#
Accessor#
They are treated as properties even though they will not be used for SQL selection.
Just like regular methods, you can use the 2nd arg of @Groups to define an alias if you want
your getter to have another name in your entity-routes responses.
Using almost the same entity/properties as the computed property basic example.
Here, the fullName accessor will also be exposed as fullName while the hasAdminRight will be displayed as
isAdmin in responses.
@DependsOn#
TODO