Virtuals are properties that are added to the entities at runtime that are not persisted in the Datastore. You can both define a getter and a setter.
getter
const userSchema = new Schema({
firstname: {},
lastname : {}
});
userSchema.virtual('fullname').get(function fullName() {
// the scope (this) is the entityData of the entity instance
// for this reason don't use arrow functions here
return `${this.firstname} ${this.lastname}`;
});
const User = gstore.model('User', schema);
const user = new User({ firstname:'John', lastname:'Snow' });
console.log(user.fullname); // 'John Snow';
/*
* You can also set virtuals to true when calling plain() on your entity
* to add them to the object returned.
*/
const response = user.plain({ virtuals: true });
console.log(response.fullname); // 'John Snow';