Reputation: 10891
How can I have a property in an Entity be calculated at query time?
For example if I had a Post
Entity and I did getPost(Long postId)
, I would like the myPost.likes
property to be calculated at that moment by getting a count of how many Like
Entities there are with the specific Post
key.
OR
If this is not possible, I was considering storing a Int likes
property for each Post
Entity that I simply increment by one every time a new Like
is inserted.
Upvotes: 0
Views: 50
Reputation: 5227
Both would work. For the latter i would use sharding counters if you expect many lps (likes per second). I would use the first approach though since then a like can have meta like, when it was created, by whom, etc.
There's no real at query time calculation for that. You would have to write the query to sum up the likes yourself. In Objectify you could use the @OnLoad annotation to query it for each post, after a query. I strongly recommend that you don't do that though since that would be a query for each returned Post of a query. You really should do that manually to avoid redundant queries for likes when you don't even need them.
Upvotes: 1