Hey there,
I'm trying to figure out the best way to map a simple relation in my App
while still trying to keep the Document Structure seperate.
The Relation
user <-> membership <-> group
So far I've been using:
user = {
'memberships':[
{ 'group_id':'idofgroup', 'permissions':['create', 'update',
'publish'] }
]
}
and for map group_member:function(doc) {
if (doc.type == 'User') {
doc.memberships.forEach(function(it) {
emit(it.group_id, null);
})
}
}
Then to load the members of each Group
/group_member?include_docs=true&key=group_id.
Fairly straightforward and simple BUT the Membership part of the User
Document keeps bugging me. Obviously I have a long long history with SQL and
it should be clear how you'd handle such things there. Now the Idea I have
in mind looks like this:
user = { just the users data }
group = { just the group data }
membership = {
'group_id':'idofgroup',
'user_id':'idofuser',
'permissions':[...],
etc...
}
group_members:function(doc) {
if (doc.type == 'membership') {
emit(doc.group_id, null, doc.user_id);
}
}
user_groups:function(doc) {
if (doc.type == 'membership') {
emit(doc.user_id, null, doc.group_id);
}
}
The THIRD argument to emit would change the associated id to the user_id so
when fetchting /group_members?include_docs=true&key=idofgroup it would
return the user doc instead of the membership doc. This of course is a pure
optimization since I can just emit the user_id, fetch those and then do a
second request to _all_docs with keys=[id1,id2,...].
Don't know if that made sense to anybody, if so what do you think? Obviously
I have no idea if this is even remotely possible with the couchdb
architecture or even if it would be any faster then the current solution.
The only Problem I have with my implementation is that a user is never
allowed to edit his own membership, only a group admin is allowed to do
that. So I have 2 different people editing the same Document and somehow
that feels dirty.
Regards,
/thomas