Reputation: 546
I am using pouchdb and couchDb as a db for my offline first mobile app with react.
https://pouchdb.com/2015/04/05/filtered-replication.html
based on the above like i configured pouchdb and sync with couchdb.I am doing user based filtering. when the user logs out and logs in again, the db values are available. what is the best design approach to do that?
Is there any example available where i can refer ?
configurePouchdb(user) {
var db = new PouchDB('dbname', {adapter: 'websql'});
var serverSideFilter = {
_id: "_design/app",
filters: {
"by_user": function (doc, req) {
return doc._id === '_design/app' || (doc.userId != undefined && doc.userId === req.query.userId);
}.toString()
}
};
db.put(serverSideFilter).then(function (doc) {
// design doc created!
}).catch(function (err) {
// if err.name === 'conflict', then
// design doc already exists
});
db.sync('http://127.0.0.1:5984/dbname', {
live: true,
retry: true,
filter: 'app/by_user',
query_params: {"userId": user}
});
return db;
}
Upvotes: 2
Views: 2542
Reputation: 439
As far as I know, views in either CouchDB or PouchDB don't have a req argument, since views generate only one index that doesn't vary among users.
As said in https://pouchdb.com/2015/04/05/filtered-replication.html, it's not recommended to use filtered replications instead of proper authentication. There is a good recipe from one of the main committers of PouchDB in https://github.com/nolanlawson/pouchdb-authentication#couchdb-authentication-recipe
In any case, there's a very good guide to filtered replication in the PouchDB API documentation: https://pouchdb.com/api.html#filtered-replication.
Upvotes: 5