0

In my app a user has friends. A user can send send requests and confirm friends (marked by the status attribute) and can delete friends. A user can only have X number of friends with X number of pending friends. To store this I have a Kind which doesn't exists , then there is a child Entity called Friends. Looks like this:

Friends

-Key((NonExistantKindParent,my_username), friends_username)
-status
-created_date

The key consists an ancestor which does exists, and the id is the user's username. The children entities will be the all the friends of that user. As a result of this each friendship will be stored twice, once for friend1 and again once for friend2. They are in entity groups so that they can be strongly consistent and I can perform transactions (for example if a user adds a friend it needs to write to both user who requested the friendship and the requestee friend). There are much more operations I need, but I am trying to understand how to do the following in which then I can apply that knowledge to other operations I need:

  • To get all of user's friends order by the date they were created (only username of friend is needed).

  • To get all of user's confirmed friends (status = 'confirmed').

Reading the docs at: https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.30.3/datastore/query?method=select I am confused how to query entitiy groups. For a couple of those queries I need to apply a property filter and an ancestor filter it looks like but am unsure how? If I needed to just query a non-entity group the docs are quite useful.

Dan McGrath
  • 37,828
  • 10
  • 90
  • 123
user2924127
  • 5,201
  • 10
  • 50
  • 120

1 Answers1

1

A property filter is applied using query#filter, and an ancestor query is set with query#hasAncestor.

Stephen
  • 5,560
  • 1
  • 21
  • 31
  • For instance to get the all the user's friends I dont need ancestors I need the descendants. How would I do this, not of these i don't think filters would do it? – user2924127 Apr 15 '16 at 02:58
  • `hasAncestor` will return the descendants of an ancestor. It is saying "return the records that have this ancestor." `filter` is saying "return the records where this property is equal to this value." – Stephen Apr 15 '16 at 12:42
  • Yes, this I know, but how to use them together? For instance I do something like this: var query = datastore.createQuery('Friends').hasAncestor(datastore.key(['FriendParent', 'my_username'])).filter('status','pending'); It should return some data, but it is returning empty array []. – user2924127 Apr 15 '16 at 18:51
  • The 2 simple queries I provided if I had examples how they are done, I would be able to understand much more clearly how it works I think. – user2924127 Apr 15 '16 at 18:55
  • That query you provided should work. Did you confirm that you did not receive an error when running the query? In particular, this query requires an index with (-kind: Friends ancestor: true properties: -name: status). Then to list all friends by date you would need (-kind: Friends ancestor: true properties: -name: created_date) – Patrick Costello Apr 15 '16 at 19:01
  • yes I have the index. In my index.yaml file though for ancestor I used yes instead of true as I copied from these docs: https://cloud.google.com/appengine/docs/python/config/indexconfig . indexes: - kind: Friends ancestor: true properties: - name: status – user2924127 Apr 15 '16 at 19:41
  • I noticed one strange behavior. If I add a limit to this query it returns an empty array. If I don't apply a limit it return no output at all, nothing in the if(err) or anywhere else is shown. I am using gcd-rpc local as my local datastore. I am using 0.30.3 version of gcloud-node. – user2924127 Apr 15 '16 at 19:55