问题
I am about to write a filter function on CouchDB
server side to filter documents specific to the user. This filter will allow replication only for those few selected documents that a particular user has access to rather than whole database of TBs of size.
Here I found a similar question CouchDB: Restricting users to only replicating their own documents but it does not provide me the info that I need.
SO my questions remain:
- What can be the best approach to specify such a filter?
- How should I write such a filter function, any written example?
What should I include in every doc so that filter will work this way:
In the context of a social network, suppose you want to fetch user data like pictures from the server, in this case a document that is containing pictures will be holding
userId
in it. But sometimes it can be the case when friends of this person will visit his profile and then they will also be able to view those photos. In that case whenever a friend visits his profile then Picture doc will be replicated to the friend and he will be able to view those pictures also.
How can I accomplish such a filter?
More Info: On the Mobile platform side I am using CouchbaseLite and on the server side I am using CouchDB. I want only filtered documents on Mobile platform.
回答1:
A CouchDB filter function only has access to the single document being filtered, so you can't use any information from other documents. This rules out "join-like" approaches, like dereferencing from a photo-post doc to the owner doc to look up the owner's list of friends.
Basically you would have to include a property in every document that lists the exact set of users who should get it.
Also, note that this doesn't provide any sort of access control. There is no way to selectively deny read access to documents in CouchDB, so you can't prevent a user from viewing a stranger's photo albums. To do that you'd have to go with the database-per-user approach, which has its own drawbacks — you end up having to set up a lot of replications between the databases to share documents between them, which doesn't scale well.
Take a look at at the Couchbase Sync Gateway (https://github.com/couchbase/sync_gateway) -- it's replication-compatible with CouchDB but adds features that enable exactly this kind of selective sync and access control. It should definitely be feasible to implement a social network with it; that's one of the use cases we had in mind when we designed it. (I'm the lead engineer on the project.)
回答2:
You could change the structure of the data, and create many small databases that replicate on devices and a central database to replicate on this small databases with the filter you implement.
This is often done so that a user has access to only some of the documents.
I think you should add an array field in docs containing the users to which it must replicate.
And in the central database, _replicate contains one document replication for each secondary database, each one with its replication filter.
This filter checks that the user whom the doc must be replicated (whom the secondary database belongs) is in the users array.
Otherwise, have you thought about using Couchbase + Sync Gateway?
来源:https://stackoverflow.com/questions/22216906/ideal-replication-filter-for-json-based-databases-like-couchbase-couchbaselite