gstore-node
Search…
SAVE
After you create an entity you can persist its data to the Datastore with entity.save() This method accepts the following arguments
1
entity.save(
2
/* {Transaction} -- optional. Will execute the save operation inside this transaction */
3
<transaction>,
4
/* {object} -- optional. Additional config */
5
<options>
6
)
Copied!
@Returns -- the entity saved

options

The options argument has a method property where you can set the saving method. It default to 'upsert'.
1
{
2
method: 'upsert|insert|update', // default: 'upsert'
3
}
Copied!
Example:
1
// blog-post.model.js
2
const { instances } = require('gstore-node');
3
4
const gstore = intances.get('default');
5
6
const blogPostSchema = new gstore.Schema({
7
title: { type: String },
8
createdOn: { type: Date, default: gstore.defaultValues.NOW }
9
});
10
11
module.exports = gstore.model('BlogPost', blogPostSchema);
Copied!
1
// blog-post.controller.js
2
const BlogPost = require('./blog-post.model');
3
4
const data = { title: 'My first blog post' };
5
const blogPostEntity = new BlogPost(data);
6
7
blogPostEntity.save().then((entity) => {
8
console.log(entity.entityKey.id); // auto-generated id
9
}).catch(err => { ... });
10
11
// changing the save method
12
var blogPostEntity = new BlogPost(data);
13
blogPostEntity.save(null, { method: 'insert' }).then( ... );
14
15
// from inside a transaction
16
// Info: if you have middleware on "pre" see note below
17
const transaction = gstore.transaction();
18
transaction.run()
19
.then(() => {
20
const blogPost = new BlogPost({ title: 'My new blog post' });
21
blogPost.save(transaction);
22
23
// ... any other operation on the Transaction
24
25
return transaction.commit();
26
})
27
.then((response) => {
28
// ... transaction finished
29
const apiResponse = data[0];
30
}).catch((err) => {
31
// handle error
32
});
Copied!

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:
1
const user = new User({ name: 'john' });
2
const transaction = gstore.transaction();
3
4
// option 1: chaining Promise before committing
5
transaction.run()
6
.then(() => {
7
return user.save(transaction) // there are some "pre" save hooks
8
.then(() => {
9
// need to chain Promise before committing
10
return transaction.commit();
11
});
12
})
13
.then((response) => {
14
const apiResponse = data[0];
15
// ... transaction finished
16
}).catch((err) => {
17
// handle error
18
});
19
20
// option 2: disable "pre" middlewares on entity before saving
21
transaction.run().then() => {
22
User.get(123, null, null, transaction)
23
.then((entity) => {
24
entity.email = '[email protected]';
25
26
// validate before so we can rollback the transaction if necessary
27
const valid = user.validate();
28
29
if (!valid) {
30
// rollback the transaction;
31
}
32
33
// disable pre middleware(s)
34
user.preHooksEnabled = false;
35
36
// save inside transaction in "sync"
37
user.save(transaction);
38
39
// ... any other transaction operations
40
41
transaction.commit().then(() => {
42
...
43
});
44
});
45
});
Copied!
Last modified 2yr ago