Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
clement opened this issue Aug 18, 2015 · 4 comments
Closed

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

clement opened this issue Aug 18, 2015 · 4 comments
Assignees
Labels
api: datastore Issues related to the Datastore API. type: question Request for information or clarification. Not an issue.

Comments

@clement
Copy link

clement commented Aug 18, 2015

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}

@stephenplusplus
Copy link
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

@stephenplusplus stephenplusplus added type: question Request for information or clarification. Not an issue. docs api: datastore Issues related to the Datastore API. labels Aug 19, 2015
@clement
Copy link
Author

clement commented Aug 19, 2015

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!
    }
  });
});

@stephenplusplus
Copy link
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.

@roboshoes
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: datastore Issues related to the Datastore API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

3 participants