Ionic 2 couchDB pouchDB 同步问题
Ionic 2 couchDB pouchDB syning issue
我正在使用 couchdb 和 pouchdb 构建一个 ionic 2 测验应用程序,但面临这个同步问题。
我的 couchdb 服务器有超过 20k 个文件,并且会不断增加。
文件被标记为主题文档和测验文档。基本上主题文件有测验文件列表。
我有大约 200 个主题文档,剩下的是测验文档。
现在,如果我使用以下代码进行双向同步
this.db = new PouchDB('mydb');
this.remote ='http://server_ip:5984/mydb';
this.db.sync(this.remote, options);
应用程序一启动,它就开始从 couchdb 服务器下载所有 20k 文件,这使得应用程序非常慢。我想阻止那个。我只想在需要时下载文件。
示例:
当应用程序加载时,我只想让应用程序从服务器下载一个文档说 'mainTopic'。该文档包含子主题列表,而这些子主题又包含测验列表。所以我希望仅在调用特定主题或测验时才下载文件
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);
});
});
}
我已经阅读了一些有关过滤器的内容,但我不确定如何使用它们。请指教
首先,您需要在 CouchDB 中构建过滤器。过滤器看起来像这样:
{
"_id": "_design/global",
"language": "javascript",
"filters": {
"byPage": "function(doc, req) { return doc.page == req.query.page }"
}
}
基本上,过滤器函数接受一个文档和一个请求对象。从请求对象中,您可以获得参数。过滤器非常简单。如果文档通过过滤器,它需要 return 为真,否则为假。
使用示例过滤器 "byPage",我可以仅同步与特定页面关联的数据。
有关 PouchDB 和 CouchDB 过滤复制的更多信息,请查看 this site。
有关 CouchDB 过滤器功能的完整文档,请查看 this link。
我正在使用 couchdb 和 pouchdb 构建一个 ionic 2 测验应用程序,但面临这个同步问题。
我的 couchdb 服务器有超过 20k 个文件,并且会不断增加。 文件被标记为主题文档和测验文档。基本上主题文件有测验文件列表。
我有大约 200 个主题文档,剩下的是测验文档。
现在,如果我使用以下代码进行双向同步
this.db = new PouchDB('mydb');
this.remote ='http://server_ip:5984/mydb';
this.db.sync(this.remote, options);
应用程序一启动,它就开始从 couchdb 服务器下载所有 20k 文件,这使得应用程序非常慢。我想阻止那个。我只想在需要时下载文件。
示例: 当应用程序加载时,我只想让应用程序从服务器下载一个文档说 'mainTopic'。该文档包含子主题列表,而这些子主题又包含测验列表。所以我希望仅在调用特定主题或测验时才下载文件
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);
});
});
}
我已经阅读了一些有关过滤器的内容,但我不确定如何使用它们。请指教
首先,您需要在 CouchDB 中构建过滤器。过滤器看起来像这样:
{
"_id": "_design/global",
"language": "javascript",
"filters": {
"byPage": "function(doc, req) { return doc.page == req.query.page }"
}
}
基本上,过滤器函数接受一个文档和一个请求对象。从请求对象中,您可以获得参数。过滤器非常简单。如果文档通过过滤器,它需要 return 为真,否则为假。
使用示例过滤器 "byPage",我可以仅同步与特定页面关联的数据。
有关 PouchDB 和 CouchDB 过滤复制的更多信息,请查看 this site。
有关 CouchDB 过滤器功能的完整文档,请查看 this link。