简体   繁体   中英

Ionic 2 couchDB pouchDB syning issue

I m building an ionic 2 quiz app with couchdb and pouchdb but facing this syncing issue.

My couchdb server has more then 20k files and will keep increasing. files are tagged as Topic documents and Quiz documents . basically topic documents has list of quiz documents .

i have about 200 topic documents and rest are quiz documents.

Now if i use bi directional syncing with the below code

this.db = new PouchDB('mydb'); 
this.remote ='http://server_ip:5984/mydb';
this.db.sync(this.remote, options);

as soon as the app start it starts downloading all 20k files from the couchdb server making the app very slow . i want to stop that . i want to download files only when its required.

Example : When the app load i just want the app to download a document say 'mainTopic' from the server .this document contains list of subtopics ,and those subtopics in turn contatins list of quiz. so i wish to download file only when a particular topic or quiz is called

this.DataService.getDocument('mainTopic').then((result) => {
this.data = result;
});

//Function in my dataservice
getDocument(id) {      
      if(id==null || id=='')
      {
        id='quiz-578f4b9cb991f';
      }

      console.log('Going to fetch '+id);

      return new Promise(resolve => {

        this.db.get(id).then((result) =>{                    
          this.data=result;          
          resolve(this.data);                 
        }).catch((error) => {   
          console.log(error);   
        }); 

      });
}

i have read a bit about filters but i am not sure how to use them. please advise

First, you will need to build filters into CouchDB. A filter looks like this :

{
  "_id": "_design/global",
  "language": "javascript",
  "filters": {
    "byPage": "function(doc, req) { return doc.page == req.query.page }"
  }
}

Basically, a filter function takes a document and a request object. From the request object, you can get your params. The filter is pretty simple. It needs to return true if the document passes the filter otherwise false.

Using the example's filter "byPage", I could sync only the data associated with a certain page.

For further informations about PouchDB and CouchDB filtered replication, take a look at this site .

And for the full documentation on CouchDB's filters function, check this link .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM