I am trying to model friendships using the Parse.com javascript API (essentially backbone models). I am using the built in Parse.com User Class. I have a mySQL / relational DB model background, and finding it tricky changing my thinking to a non-sql / denormalized way.
It should support the standard facebook style methods: - user a can request user b's friendship - user b can accept or ignore the friendship request
For a given user I want to be able to fetch a collection of their friends (accepted friendships in both directions).
I had been considering using a Friendships model and referencing the user models as initiators and acceptors:
initiator | acceptor | status
==========+==========+========
user a | user b | accepted
user b | user c |
user c | user a | accepted
I am unsure if this is the best way, and also unsure about securing it with ACLs. I am concerned that both users in a friendship will need to be able to update the row - in order to remove a friendship. Also, user b should not be able to make requests to the Parse API that would reveal another user's friendships.
To query for a user a's friends I would do two queries, one for where user a is the acceptor, and one for the initiator.
Can someone please comment if this is an acceptable way to model this, or if not why not and suggest improvements. Thanks!
Update 8/8/12: A problem I see here: If I set an ACL on each row in the Friendships Class so the initiator may write the row, and it is publicly readable - then the acceptor will not be able to update the row to status = accepted.
Could a friendship be modeled as two rows in the table? eg:
initiator | acceptor | status
==========+==========+========
user a | user b | requested
user b | user a | accepted
No idea how I would query that to get a given user's friends however!