gstore-node
v7.x
v7.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

Was this helpful?

  1. Schema

Custom methods

Custom methods can be added to entities instances through their Schemas.

schema.methods.<methodName> = function(){ ... }

Make sure not to use arrow function as you would lose the scope of the entity instance.

const blogPostSchema = new Schema({ title: {} });

// Custom method to retrieve all children Text entities
blogPostSchema.methods.texts = function getTexts() {
    // the scope (this) is the entity instance
    const query = this.model('Text')
                        .query()
                        .hasAncestor(this.entityKey);

    return query.run();
};

// In your Controller
// You can then call it on an entity instance of BlogPost

const BlogPost = require('../models/blogpost.model');
BlogPost.get(123)
    .then((blogEntity) => {
        blogEntity.texts()
            .then((response) => {
                const texts = response[0].entities;
            });
        });

Note how entities instances can access other models through entity.model('MyModel'). Denormalization can then easily be done with a custom method:

// Add custom "profilePict()" method on the User Schema
userSchema.methods.profilePicture = function profilePicture() {
    // Any type of query can be done here
    return this.model('Image').get(this.imageIdx);
};

// In your controller
const User = require('../models/user.model');

const user = new User({ name: 'John', imageIdx: 1234 });
user.profilePicture()
    .then((imageEntity) => {
        user.profilePicture = imageEntity.url;
        user.save()
            .then(() { ... });
    });

// Or with a callback
userSchema.methods.profilePict = function(cb) {
    return this.model('Image').get(this.imageIdx, cb);
};
...

const user = new User({ name:'John', imageIdx:1234 });

// Call custom Method 'getImage'
user.profilePict(function onProfilePict(err, imageEntity) {
    user.profilePict = imageEntity.url;
    user.save().then(() { ... });
});
Previousvirtual()NextModel

Last updated 6 years ago

Was this helpful?