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

Was this helpful?

  1. Entity
  2. Methods

datastoreEntity()

In case you need at any moment to fetch the entity data from Google Datastore, this method will do just that right on the entity instance. It is useful for example in "pre" delete hooks where the entity to be deleted is passed but we don't have its data.

// user.model.js

const userSchema = new Schema({ name: { type: String }, pictIdx: { type: Number });

schema.pre('delete', function() {
    // The scope "this" is the entity to be deleted.
    // At this stage we don't have its data but we need it in order
    // to check if there is an Image to delete with the User.
    // We use datastoreEntity() for that.

    return this.datastoreEntity().then(entity => {
        if (!entity.pictIdx) {
            return;
        }
        // We delete the associate image entity
        return entity.model('Image').delete(entity.entity.pictIdx);
    });
});

module.exports = gstore.model('User', userSchema);

// ...

// Now each time we delete a User, we'll delete any Image associated with it.

const User = require('user.model');
User.delete(123).then(...);
Previousmodel()Nextvalidate()

Last updated 6 years ago

Was this helpful?