简体   繁体   English

Google Cloud Datastore更新实体:Node.js客户端

[英]Google Cloud Datastore Update Entity: Node.js Client

How to update GCP datastore entity in nodejs. 如何在Node.js中更新GCP数据存储区实体。

// Saves the entity
datastore
.save(task)
.then(() => {
  console.log(`Saved ${task.key.name}: ${task.data.description}`);
})
.catch(err => {
 console.error('ERROR:', err);
});

Usually save function is storing the data and if we send the existing the data it is updating fine. 通常,保存功能是存储数据,如果我们发送现有数据,则可以更新。 But if we miss some columns then it is nullifying those columns. 但是,如果我们错过了一些列,那么它将使那些列无效。 Is there any function to update entity in datastore. 是否有任何功能可以更新数据存储区中的实体。

save() in datastore overwrites the entity. 数据存储区中的save()覆盖实体。 If you want to do an update : 如果要进行更新:

  1. Get the entity. 获取实体。
  2. update the relevant fields in the entity. 更新实体中的相关字段。
  3. use save() to update. 使用save()进行更新。

To avoid race conditions, datastore transaction can be used 为避免争用情况,可以使用数据存储transaction

What I understand is that you are confused about why updating an entity with, say 8 valid properties, will have 2 properties nulled if you only provide 6/8 properties in the update. 我了解的是,您对为什么要更新一个实体(如果有8个有效属性)将使2个属性为空(如果仅在更新中提供6/8个属性)感到困惑。

This is by design as Datastore will write the entire entity with save() . 这是设计Datastore ,因为Datastore将使用save()写入整个实体。 What you want to do is force save(method=upsert, yourdata) , and a shortcut is to use the upsert() method. 您要执行的操作是强制save(method=upsert, yourdata) ,快捷方式是使用upsert()方法。

reading the "save" documentation at https://cloud.google.com/nodejs/docs/reference/datastore/1.3.x/Datastore - 阅读https://cloud.google.com/nodejs/docs/reference/datastore/1.3.x/Datastore中的“保存”文档-

By default, all properties are indexed. 默认情况下,所有属性均已建立索引。 To prevent a property from being included in all indexes, you must supply an excludeFromIndexes array. 为了防止属性包含在所有索引中,必须提供excludeFromIndexes数组。

I assume that you get null fields because they're indexed. 我假设您得到空字段,因为它们已被索引。 try to remove indexes from fields that may not exist. 尝试从可能不存在的字段中删除索引。

If you use gstore-node there is an update() method on the Model that does this for you. 如果使用gstore-node ,则模型上有一个update()方法可以为您完成此操作。
gstore will do (in a transaction): gstore将执行(在交易中):

  • Get the entity in the Datastore 在数据存储区中获取实体
  • Merge the data you pass with the entity fetched 合并您获取的实体传递的数据
  • save the entity back in the Datastore. 将实体保存回数据存储区。

Here is the documentation of the update() on Model: https://sebelga.gitbooks.io/gstore-node/content/model/update-method.html 这是有关模型的update()的文档: https : //sebelga.gitbooks.io/gstore-node/content/model/update-method.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node.js Google Cloud Platform数据存储日期比较 - Node.js Google Cloud Platform Datastore date comparison 在node.js中通过各种方式查询Google Cloud Datastore - Google Cloud Datastore query by all kinds in node.js 使用Node.js的Google Cloud Datastore查询 - Google Cloud Datastore Queries using Node.js Node.js和Google Cloud数据存储入门教程中的令牌错误 - Token error on getting started tutorial with Node.js and Google Cloud datastore 如何在Google Cloud Datastore(Node.js)中执行“仅键查询” - How to do a “keys-only-query” in Google Cloud Datastore (Node.js) 无法使用 node.js 通过 Google Cloud 功能访问数据存储区 - Can't access Datastore through Google Cloud function using node.js 使用 Node.js 更新 Google Cloud Secret Manager 中的数据 - Update data in Google Cloud Secret Manager using Node.js 如何使用Node JS从Google Cloud DataStore返回所有实体ID? - How to return all entity ids from google cloud datastore using node js? google cloud pubsub node.js客户端与谷歌云功能不兼容 - google cloud pubsub node.js client not compatible with google cloud functions 结合使用bluebirdjs和Google Cloud Datastore节点客户端库 - Using bluebirdjs with Google Cloud Datastore node client library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM