entity-routes logo
Docs

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.

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#

#
ts
1class Employee {
2 firstName: string;
3 lastName: string;
4 rights: string[];
6 @Groups("all")
7 getFullName() {
8 return this.firstName + " " + this.lastName;
9 }
11 @Groups("all", "isAdmin")
12 hasAdminRight() {
13 return this.rights.includes("admin");
14 }

Here, the getFullName accessor will be exposed as fullName while the hasAdminRight will be displayed as isAdmin in responses.

Async example#

#
ts
1class Employee {
2 firstName: string;
3 lastName: string;
4 rights: string[];
6 @Groups("all")
7 getFullName() {
8 return this.firstName + " " + this.lastName;
9 }
11 @Groups("all", "isAdmin")
12 hasAdminRight() {
13 return this.rights.includes("admin");
14 }

Accessor#

Accessors are the special methods get/set.

They are treated as properties even though they will not be used for SQL selection.

Using almost the same entity/properties as the computed property basic example.

#
ts
1class Employee {
2 firstName: string;
3 lastName: string;
4 rights: string[];
6 @Groups("all")
7 get fullName() {
8 return this.firstName + " " + this.lastName;
9 }
11 @Groups("all", "isAdmin")
12 get hasAdminRight() {
13 return this.rights.includes("admin");
14 }

Here, the fullName accessor will also be exposed as fullName while the hasAdminRight will be displayed as isAdmin in responses.

@DependsOn#

TODO

Prev
Introduction
Next
Examples