How to simulate a social graph like flockDB in Google App Engine

FlockDB provides a very good way to model social graphics: the system has only two objects: “User and friendship”. where the user represents the vertices of the graph, and friendship represents the edges between two users. Like this:

User (id, first_name, last_name, birth_data, registration_timestamp)

Friendship (id, user1_id, user2_id, depth, other parameters of friendship ...)

There are a number of advantages to this approach.

  • You can specify options for friendship, such as a friend, close friend, in a relationship
  • You can choose whether your friendship is directional or not.

My question is: how to implement this using the DataStore provided by Google AppEngine. here is my first attempt, but I don’t understand how to really assign keys to friendship objects.

class User(db.Model): name = db.StringProperty() age = db.IntegerProperty() registeration_ts = db.DateTimeProperty(auto_now_add=True) class FriendShip(db.Model): user1 = db.Key() // key to some user in user1 user2 = db.Key() // key to some user in user2 creation_ts = db.DateTimeProperty(auto_now_add=True) updated_ts = db.DateTimeProperty(auto_now_add=True) 
+4
source share
2 answers

one way might look like this:

 class User(db.Model): name = db.StringProperty() friends = db.SelfReferenceProperty(collection_name="subscribers") 

This would be a simple model for introducing the user and friends. But I think he will have two drawbacks:

  • Since friendship is not an entity, we cannot assign properties to it.
  • since the collection is now supported, I don’t know how quickly it will query how friends choose friends.
0
source

Instead of db.Key() (which is a value, not a class of properties), use db.ReferenceProperty(User) .

0
source

Source: https://habr.com/ru/post/1348065/


All Articles