Reputation: 568
I'm building a mobile application using the Ionic Framework. The application is a simple corporate "employee directory" using PouchDB to store and synchronize with my database.
My app features a pull-to-refresh function that starts a one way replication from CouchDB to PouchDB on the app. This works perfectly, but I have one little snafu...
The app has a "friends list" feature where a user can swipe an employee from the list to add them to a quick contact menu. When they swipe an employee, the application adds a favorite: true
property to the PouchDB document. This functions great until that particular document is replicated. As one would expect, the added favorite: true
property is erased as it is not synched to the CouchDB (I want to keep this property local).
My question: Is there a way to have one way replication, but persist part of the document? I'd love for the CouchDB database to replicate the entire document, but leave the favorite
property untouched.
I attempted to use a solution with localStorage and comparing a stored array to the document ID's, but it is quite messy and not very performant.
Interested to hear what others may have done to solve similar situations. Thanks in advance!
Here is some sample data of what my PouchDB looks like:
0: {
doc: {
first_name: 'John',
last_name: 'Smith',
phone: 'xxxxxxx',
email: '[email protected]'
},
id: '12314131231',
key: '12314131231'
},
1: {
doc: {
first_name: 'Jane',
last_name: 'Doe',
phone: 'xxxxxxx',
email: '[email protected]',
favorite: true <!-- Added by app, overwritten when replication occurs -->
},
id: '1231344431',
key: '1231344431'
}
Upvotes: 1
Views: 1045
Reputation: 875
You are storing each user detail as separate document. Replication is done on basic of document _rev (revision).
you are using one way replication from couch to pouch. So each time you replicate all the documents will be restored as they are in couch.
only documents which are in couch will be only replicated to pouch. if you have any extra documents in pouch then replication will not affect any extra extra documents.
so what you can do is make one documents which contents id of user which you mark as friend.
so when you load friend just you need to check friend id in that document (separate document to maintain friends id).
This approach can solve your problem. using this each user has it's own friend list in separate document.
document may be look like
{
"_id": "Friendlist",
"friendlist": [12314131231, 1231344431]
}
so this document will be there in pouch.Replication doesn't effect it.
Good Luck
Upvotes: 3