Neha Sohail
Neha Sohail

Reputation: 239

How to get the time value when an entity is updated/created in Google datastore?

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

});

Upvotes: 1

Views: 2250

Answers (1)

Ed Davisson
Ed Davisson

Reputation: 2927

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.

Upvotes: 1

Related Questions