如何在Google数据存储中更新/创建实体时获取时间值? [英] How to get the time value when an entity is updated/created in Google datastore?
问题描述
我正在我的节点js服务器中创建实体。我的 / message
路由创建实体,我的显示消息
路由读取实体数据。有没有办法检查实体创建或更新的时间?如果没有,是否有另一种方法给实体对象添加时间?基本上我使用时间来检查实体是否已经更新或者不在显示消息
路由中。
消息路由
应用。 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时间戳值信息
datastore.upsert(entity)
.then(()=> {
} );
});
显示讯息路由
app.get('/ display-message',function(req,res){
datastore.get(key)
.then(function(results){
//读取时间戳值信息
let entity_data = results [0];
let message_text = entity_data.message;
});
});
没有内建的创建/更新时间支持在Cloud Datastore实体中,但您可以随时在创建/保存实体时将其中一个添加到您选择的属性中:
const entity = {
key:key,
data:{
message:message,
my_timestamp_property:new Date()
}
};
请注意,将时间戳放入索引(单个属性默认为索引)可能会导致增加读写延迟。有关详情,请参阅最佳做法文章。
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;
});
});
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.
这篇关于如何在Google数据存储中更新/创建实体时获取时间值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!