SAVE
After you create an entity you can persist its data to the Datastore with entity.save()
This method accepts the following arguments
entity.save(
/* {Transaction} -- optional. Will execute the save operation inside this transaction */
<transaction>,
/* {object} -- optional. Additional config */
<options>
)@Returns -- the entity saved
options
The options argument has a method property where you can set the saving method. It default to 'upsert'.
{
method: 'upsert|insert|update', // default: 'upsert'
}Example:
// blog-post.model.js
const { instances } = require('gstore-node');
const gstore = intances.get('default');
const blogPostSchema = new gstore.Schema({
title: { type: String },
createdOn: { type: Date, default: gstore.defaultValues.NOW }
});
module.exports = gstore.model('BlogPost', blogPostSchema);Saving inside a Transaction with middleware on Model
If you have "pre" middlewares on the save method of your Model (mySchema.pre('save', myMiddleware)) you need to chain the Promise of the save method before committing the transaction, otherwise the entity won't be saved.
You can avoid this by disabling the middlewares on the entity with preHooksEnabled set to false on the entity.
Examples:
Last updated
Was this helpful?