How to get the time value when an entity is update

2019-09-10 09:03发布

I am creating entities in my node js server. My /message route creates the entity and my display-message route reads the entity data. Is there a way to check when an entity was created or updated? If not, is there another way to attach a time to the entity object? Basically I am using the time to check if the entity has been updated or not in the display-message route.

Message Route

app.post('/message', function (request, response) {
  let message = request.body.Body;
  response.send("<Response><Message>Heyyo!</Message></Response>");

  let entity = {
      key: key,
      data: {
        message: message
        //send timestamp value information 
      }
  };

  datastore.upsert(entity)
    .then(()=> {

    });

});

Display-Message route

app.get('/display-message', function(req,res){

    datastore.get(key)
        .then(function(results) {
            //read timestamp value information 
            let entity_data = results[0];
            let message_text = entity_data.message;
    });

});

1条回答
Rolldiameter
2楼-- · 2019-09-10 09:22

There's no built-in support for creation/update time in Cloud Datastore entities, but you can always attach one in a property of your choice when you create/save the entity:

const entity = {
  key: key,
  data: {
    message: message,
    my_timestamp_property: new Date()
  }
};

Be aware, though, that putting timestamps in an index (single properties are indexed by default) can lead to increased read and write latency. See the best practices article for more info.

查看更多
登录 后发表回答