Reputation: 470
I need to synchronize single documents between databases, so that when one is updated the other is on the same server. The motivation being an attempt to share individual documents between databases for collaboration purposes, since there is no document level permissions. What that means is if databaseA and userA have a document that userA wants to share with userB who "owns" databaseB. There is no good way (that I know of) to share the document in place on userA's database because userB is not a reader on databseA.
Now my solution would be to allow the linking of a single document between databases (at the request of the owner and the acceptance of the destination database) then keep them constantly up to date. Any ideas how I might achieve it?
Upvotes: 2
Views: 2403
Reputation: 619
You could make use of a feature called Filtered Replication. Where ordinary replication keeps entire databases in sync, you can supply a filter function that returns true for any document that is supposed to be replicated and false otherwise. Combined with a continuous replication task you can use this feature to keep single document in sync in near-realtime.
Practically, you will have to decide on how to set up the replication tasks. Assuming that you have many users in your application that can share an arbitrary number of documents, you could use the following approach: 1 database per user + 1 master database containing all the documents. That is, you set up continuous replication from any user's database to the master database (which is readable for none of your users, only admins). In each document you store a field, say "owners", with an array of users that are meant to have access to that document (which will be editable by the user). Then you set up filtered replication from the master database to each user's database and the filter function decides based on the field "owners" whether the document shall be replicated or not. This approach will require two replication tasks per user.
For further information on filtered replication see http://wiki.apache.org/couchdb/Replication#Filtered_Replication and for replication in general see also http://guide.couchdb.org/draft/replication.html. Note that the API for replication has slightly changed in CouchDB 1.1, essentially replacing _replicate by _replicator.
Upvotes: 3