I am writing a social networking web app with couchdb as backend. Currently I am maintaining user profiles as JSON docs.
My app has a friendship feature when one user will request other user and upon acceptance the friendship is solemnized. Besides friendship there is one way relationship called “follow” relationship as well.
I thought of creating a connection doc
{
source_user:'',
target_user:'',
source_follows_target:'',
target_follows_source:'',
..Similarly for friendship...
}
This doesn’t look right to me at all. Since the relationship exists between two exactly similar entities (users in this case) I dont thin the model should try to distinguish between source and target.
The fact that the relationship can be (or is always) symmetric doesn’t mean it necessarily has to be modelled as one logical relationship. I think it might more general to model the possibility of either, and then prevent one-way friendships in your application if you so desire.
In which case for each user you might have a set of users that they consider their friend (One -> Many). You could store a copy (or cache) of whether it is symmetric or not on each of these relationship objects to make it a bit more scalable.
A rough example of how a user object might look:
Operations on these sets of users, e.g. intersections (friends in common), would then be a lot easier since they are accessed directly from the user object.