Skip to content

Possibly incorrect example on the datastore preview in README.md #812

Closed

Description

@clement

The following example seems incorrect, as datastore doesn't support partial update:

dataset.save({
  key: blogPostKey,
  data: blogPostData
}, function(err) {
  // `blogPostKey` has been updated with an ID so you can do more operations
  // with it, such as an update:
  dataset.save({
    key: blogPostKey,
    data: {
      isDraft: false
    }
  }, function(err) {
    if (!err) {
      // The blog post is now published!
    }
  });
});

It looks like the second update will overwrite the blog post with a single property entity {isDraft: false}

Activity

  1. stephenplusplus commented on Aug 19, 2015

    @stephenplusplus
    Contributor

    Datastore does support partial updates in the form of an "upsert mutation" -- https://cloud.google.com/datastore/docs/concepts/entities (best link I could find to explain it). Our save method tries to pick the best one that applies: http://googlecloudplatform.github.io/gcloud-node/#/docs/v0.19.0/datastore/transaction?method=save

  2. clement commented on Aug 19, 2015

    @clement
    Author

    An upsert mutation is not a partial update (an upsert is akin to a force write, it will insert the entity or update it if an entity already exists for the provided key).

    The example I quoted above is indeed incorrect, the resulting BlogPost entity contains only {isDraft: false}.

    Here is a (failing) test to illustrate further:

    var key = dataset.key('Test');
    dataset.save({
      key: key,
      data: {a: 42, bar: 'foo'}
    }, function(err) {
      dataset.save({
        key: key,
        data: {bar: 'baz'}
      }, function(err) {
        dataset.get(key, function(err, entity) {
          assert.equal(entity, {a: 42, bar: 'baz'}); // fails, entity is {bar: 'baz'}
        });
      });
    });
    

    Here is an updated getting started example that doesn't have this issue:

    dataset.save({
      key: blogPostKey,
      data: blogPostData
    }, function(err) {
      // `blogPostKey` has been updated with an ID so you can do more operations
      // with it, such as an update:
      blogPostData.isDraft = false;
    
      dataset.save({
        key: blogPostKey,
        data: blogPostData
      }, function(err) {
        if (!err) {
          // The blog post is now published!
        }
      });
    });
    
  3. stephenplusplus commented on Aug 19, 2015

    @stephenplusplus
    Contributor

    Good to know! Thanks for not letting me go on misunderstanding an upsert. Would you be able to send your example in a PR? Either way, thanks for catching this.

  4. roboshoes commented on Jan 7, 2016

    @roboshoes

    Appreciate this discussion @clement. I've been wondering why it kept overwriting my entire document, when I wanted to simply update one field.

  5. 1 remaining item

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

api: datastoreIssues related to the Datastore API.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions