Question

In a contact management app, each user will have his own database. When users wish to share certain categories of contacts with others, a backend will initiate a replication. Each contact is its own document, but also has various children documents such as notes and appointments.

Here is an example...

Contact:

{
  "_id": 123,
  "type": "contact",
  "owner": "jimmy",
  "category": "customer",
  "name": "Bob Jones",
  "email": "bob@example.com"
}

Note:

{
  "_id": 456,
  "type": "note",
  "owner": "jimmy",
  "contact_id": 123,
  "timestamp": 1383919278,
  "content": "This is a note about Bob Jones"
}

So let's say Jimmy wants to share his only his customers with sales manager Kevin, while his personal contacts remain private. When the note passes through the replication filter, is it possible to access the linked contact's category field?

Or do I have to duplicate the category field in every single child of a contact? I would prefer not to have to do this, as each contact may have many children which I would have to update manually every time the category changes.

Here is some pseudo-code for the filter function:

function(doc, req)
{
  if(doc.type == “contact”) {
    if(doc.category == req.query.category) {
      return true;
    }
  }
  else if(doc.contact_id) {
    if(doc.contact.category == req.query.category) {
      return true;
    }  
  }
  return false;
}

If this is possible, please describe how to do it. Thanks!

Was it helpful?

Solution 2

No, this is not possible. Each document must be consistent so it has no any explicit relations with others documents. Having contact_id value as reference is just an agreement from your side - CouchDB isn't aware about this.

You need to literally have category document be nested within contact one to do such trick e.g. have single document to process by filter function. This is good solution from point when you need to have consistent state of contact document.

OTHER TIPS

There are some other options.

There's a not-so-well-known JOIN trick in CouchDB. Instead of using replication, however, you'll have to share the results of a MapReduce View -- unfortunately you can use a view as a filter for replication. If you're using Cloudant (disclaimer: I'm employed by Cloudant) you can use chained-MapReduce to output the result to another database that you could then replication from...

Additionally, I think this SO post/answer on document structures and this join trick could be helpful: Modeling relationships on CouchDB between documents?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top