i have a feeling that issue i am having facing is some stupid simple basic thing but i m still not able to figure it out.
I have having couchdb server which have thousands of documents. which are categories as type:topics and type:quiz. basically topic documents contains list of quiz documents ids and the type quiz documents contain the question and answer
Sample : topic document
{
"_id": "topic-57913023930b4",
"_rev": "1-1b8968c43bacffa849fab9bc62fbe910",
"name": "Programming Languages",
"type": "topic",
"pid": "topic-57913023930b4",
"totalQuiz": 19,
"quiz": [
{
"id": "quiz-5790b7035f496"
},
{
"id": "quiz-5790b7fbacc8e"
},
{
"id": "quiz-5790b3291766d"
},
{
"id": "quiz-5790afc077f64"
}
]
}
sample: quiz document
{
"_id": "quiz-5790a476e184c",
"_rev": "1-48acc3b900b6010cf3d527fcf26f8e0c",
"question": "this is a question",
"correctanswer": 0,
"answers": [
"option 1",
"option 2",
"option 3",
"option 4"
]
}
Now to sync the server database to the local database .if i sync whole database then my local database like this :
this.db = new PouchDB('mydb');
this.remote ='http://IP_ADDRESS:5984/mydb';
let options = {
live: true,
retry: true,
continuous: true
};
this.db.sync(this.remote, options);
then my local database will get huge in just few minutes because my server contains thousands of documents. i wish to avoid that and download documents only on demand. i am not sure how to do that
if i use some basic filter to pull only the documents that are of type : topics at the start like this :
this.db = new PouchDB('mydb');
this.remote ='http://IP_ADDRESS:5984/mydb';
let options = {
live: true,
retry: true,
continuous: true,
filter: function (doc) {
return doc.type === 'topic';
}
};
this.db.sync(this.remote, options);
if i do above then only topics documents get pulled but i dont have access to the quiz documents at all. perhaps i need to disable syncing and figure out a way to download data only on demand. not sure how to do that though
But anyway even i let the whole database gets synced automatically in the constructor. still it takes along time to replicate the database to the local database .
lets say in the home page of the app i want to display a topic ‘Programming Languages’ from the document id : “_id”: “topic-57913023930b4” . for that i made this code
ionViewLoaded(){
this.DataService.getDocument('topic-57913023930b4').then((result) => {
this.data = result;
});
}
and in my service i have this code:
getDocument(id) {
return new Promise(resolve => {
this.db.get(id).then((result) =>{
this.data=result;
resolve(this.data);
}).catch((error) => {
console.log(error);
});
});
}
But when the app starts it hasnt pulled all those thousand documents yet so my local database dont have document with id ‘topic-57913023930b4’ . it might take several minutes to get that document if i rely on the constructor’s data sync code.
so i need a way to pull this data from the couchdb server at this very moment. but i m not sure how to do that. although i know can use HTTP request but it wont get synced with the pouchdb local database.
Please guide me in the right direction. i have been trying to make this working since yesterday but not able to figure this out.