Skip to main content
Version: 6.5

Computed Properties

Entity classes are just normal classes, so any common derived data can just be added as getters to the class itself.

import { Entity } from '@rest-hooks/rest';

class User extends Entity {
id = '';
firstName = '';
lastName = '';
username = '';
email = '';

pk() {
return this.id;
}

get fullName() {
return `${this.firstName} ${this.lastName}`;
}
}

If the computations are expensive feel free to add some memoization.

import { Entity } from '@rest-hooks/rest';
import { memoize } from 'lodash';

class User extends Entity {
truelyExpensiveValue = memoize(() => {
// compute that expensive thing!
});
}
tip

If you simply want to deserialize a field to a more useful form like Date or BigNumber, you can use the declarative static schema.

import { Entity } from '@rest-hooks/rest';

class User extends Entity {
id = '';
firstName = '';
lastName = '';
createdAt = new Date(0);

pk() {
return this.id;
}

static schema = {
createdAt: Date,
};
}