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. Model
  2. Methods

UPDATE

PreviousGETNextDELETE

Last updated 5 years ago

Was this helpful?

This method allows you to partially update (providing a few properties) or completely replace an entity in the Datastore. It will first fetch the entity, then update its data with the new ones and finally save the data back to the Datastore. The whole operation occurs inside a . Note: This method is different than using `Entity.save(null, { method: 'update' })` (which correspond to the ).

This method accepts the following arguments:

MyModel.update(
    /* {int|string}. -- the id of the entity to update */
    <id>,
    /* {object} -- the data to update */
    <data>,
    /* {Array} -- optional. ex: ['ParentEntity', 1234 ] */
    <ancestors>,
    /* {string} -- optional. A specific namespace */
    <namespace>,
    /* {Transaction} -- optional. The transaction currently in progress */
    <transaction>,
    /* {object} -- optional. Additional config */
    <options>
)

@Returns -- an entity instance.

Example

const BlogPost = require('./blog-post.model');

const blogPostData = {
    title : 'New title'
};

BlogPost.update(123, blogPostData).then((entity) => {
    console.log(entity.plain());
});

// with *ancestors* and a *namespace*
BlogPost.update(123, data, ['Grandpa', 123, 'Dad', 123], 'dev.namespace.com')
        .then((entity) => {
            console.log(entity);
        });

// from inside a Transaction
const transaction = gstore.transaction();
transaction.run().then(async () => {
    await BlogPost.update(123, data, null, null, transaction);

    transaction.commit().then(() => { ... });
});

Options

options properties

  • dataloader (a Dataloader instance)

  • replace (Boolean. default: false)

const { instances } = require('gstore-node');

const gstore = instances.get('default');

// Important! This should be done on **each** request (read the Dataloader documentation)
const dataloader = gstore.createDataLoader();
BlogPost.update(123, data, null, null, null, { dataloader }).then( ... );

replace If you set it to true gstore will replace the the entity data in the Datastore without merging the data first. By default, MyModel.update() does 2 operations inside one transaction:

  • get() the entity and merges its data with the new data

  • save() the entity

If you just want to override the entity data without doing any merge set replace to true in the options parameter.

BlogPost.update(123, data, null, null, null, { replace:true }).then( ... );

// which is the same as doing

const blogPost = new BlogPost(data, 123);
blogPost.save().then( ... );

dataloader The dataloader instance must be created on each request. for more information on this. The dataloader instance will be added to the entity instance being saved. This means that it is available in your "pre" save middlewares (attached on the "this" scope (const dataloader = this.dataloader;))

Datastore transaction
datastore update() method
Read the documentation