可以在一个事务中保存多种多个数据存储区实体吗?

时间:2017-03-16 18:05:30

标签: google-cloud-datastore

是否可以使用针对Google数据存储区的节点api在一个事务中保存多种类型的多个数据存储区实体?

1 个答案:

答案 0 :(得分:1)

是的,Cloud Datastore支持在同一个事务中编写多种类型。要获取example from the getting started guide,您可以更新Task实体并编写新类型,在这种情况下记录用户事件:

function markDone (taskId) {
  const transaction = datastore.transaction();
  const taskKey = datastore.key([
    'Task',
    taskId
  ]);

  return transaction.run()
    .then(() => transaction.get(taskKey))
    .then((results) => {
      const task = results[0];
      task.done = true;
      transaction.save({
        key: taskKey,
        data: task
      },
      // Also write a log of the user action.
      {
        key: datastore.key(['User', current_user, 'Event']),
        data: [{
            name: 'event',
            value: 'TaskDone'
          }]
      }]);
      return transaction.commit();
    })
    .then(() => {
      // The transaction completed successfully.
      console.log(`Task ${taskId} updated successfully.`);
    })
    .catch(() => transaction.rollback());
}