gstore-node
Search…
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.
1
// user.model.js
2
3
const userSchema = new Schema({ name: { type: String }, pictIdx: { type: Number });
4
5
schema.pre('delete', function() {
6
// The scope "this" is the entity to be deleted.
7
// At this stage we don't have its data but we need it in order
8
// to check if there is an Image to delete with the User.
9
// We use datastoreEntity() for that.
10
11
return this.datastoreEntity().then(entity => {
12
if (!entity.pictIdx) {
13
return;
14
}
15
// We delete the associate image entity
16
return entity.model('Image').delete(entity.entity.pictIdx);
17
});
18
});
19
20
module.exports = gstore.model('User', userSchema);
21
22
// ...
23
24
// Now each time we delete a User, we'll delete any Image associated with it.
25
26
const User = require('user.model');
27
User.delete(123).then(...);
Copied!
Last modified 2yr ago
Copy link