gstore-node
v8.x
v8.x
  • Introduction
  • Getting Started
    • Motivation
    • Installation
    • Create a gstore instance
  • Schema
    • About
    • Type validation
    • Value validation
    • Additional properties settings
    • Schema options
    • Joi Schema
    • Methods
      • path()
      • virtual()
    • Custom methods
  • Model
    • Creation
    • Methods
      • GET
      • UPDATE
      • DELETE
      • excludeFromIndexes()
      • key()
      • sanitize()
      • clearCache()
  • Entity
    • Creation
    • Properties
    • Methods
      • SAVE
      • plain()
      • populate()
      • model()
      • datastoreEntity()
      • validate()
  • Queries
    • @google-cloud Query
    • list()
    • findOne()
    • deleteAll()
    • findAround()
  • Populate
  • Middleware (hooks)
    • About
    • Pre hooks
    • Post hooks
  • Cache / Dataloader
    • Dataloader
    • Cache
  • gstore Methods
    • save()
  • Typescript
  • Appendix
    • Error Codes
    • Credits
Powered by GitBook
On this page
  • getter
  • setter

Was this helpful?

  1. Schema
  2. Methods

virtual()

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';

setter

const userSchema = new Schema({
    firstname: {},
    lastname : {}
});

userSchema.virtual('fullname').set(function(name) {
    const split = name.split(' ');
    this.firstname = split[0];
    this.lastname = split[1];
});

const User = gstore.model('User', schema);

const user = new User();
user.fullname = 'John Snow';

console.log(user.firstname); // 'John';
console.log(user.lastname); // 'Snow';

Note: You can save entities without worrying about the virtuals as they are removed from the entity data automatically.

Previouspath()NextCustom methods

Last updated 6 years ago

Was this helpful?