【问题标题】:Preload / Pre-populate PouchDB in React Native Application在 React Native 应用程序中预加载/预填充 PouchDB
【发布时间】:2018-09-07 11:44:40
【问题描述】:

是否可以在我的 React Native 应用程序中预加载/预填充数据库,然后在第一次运行时,只需进行同步?在分发应用程序之前,我已经拥有了大部分(如果不是全部)数据库信息,如果它只需要在应用程序运行时进行快速同步,那就太棒了。有什么想法我会如何去做吗?

我找到了这个 - https://pouchdb.com/2016/04/28/prebuilt-databases-with-pouchdb.html,但它没有提到 React Native

使用:

  • pouchdb 查找:^7.0.0
  • pouchdb-react-native: ^6.4.1
  • 反应:16.3.1
  • 反应时刻:^0.7.9
  • 反应原生:~0.55.2

感谢您的任何指点。

更新这是我用来尝试加载转储文件的代码。此代码存在于 /screens/Home.js 转储文件位于 /client/dbfile/database.txt

var db = new PouchDB("cs1");

db.get("_local/initial_load_complete")
  .catch(function(err) {
    console.log("loading dumpfile");
    if (err.status !== 404) {
      // 404 means not found
      throw err;
    }
    db.load("/client/dbfile/database.txt").then(function() {
      return db.put({ _id: "_local/initial_load_complete" });
    });
  })
  .then(function() {
    // at this point, we are sure that
    // initial replication is complete
    console.log("loading is complete!");
    return db.allDocs({ include_docs: true });
  })
  .then(
    function(res) {
      // display all docs for debugging purposes (empty)
      console.log(res);
    });

this.localDB = db;

当它运行时,我的控制台会显示 - 显示已添加 0 行。

Object {
    "offset": 0,
    "rows": Array [],
    "total_rows": 0,
}
Possible Unhandled Promise Rejection (id: 0):
Object {
    "message": undefined,
    "name": "unknown",
    "status": 0,
}

【问题讨论】:

  • 跟进:我尝试从上述文章中的转储文件加载,但收到db.load is not a function. (In 'db.load("client/dbfile/database.txt")', 'db.load' is undefined) 错误消息
  • 能分享一下代码吗?
  • 跟进:解决了负载未定义的问题,我必须安装这个github.com/pouchdb-community/pouchdb-load 并添加插件PouchDB.plugin(require('pouchdb-load')); 但是,我没有取回任何文档。用一些代码更新我最初的问题

标签: react-native pouchdb


【解决方案1】:

在我的项目中,我有几个与应用程序一起分发的数据库文档(翻译 JSON 就是一个很好的例子)。 所以在应用程序初始化时,我只是尝试从 db 中读取翻译文档,如果没有 - 我从 js 模块导入内容并存储在 db 中。 然后翻译更改只是从服务器复制到本地数据库。

//transmob.js
const transMobFile = {
//content goes here
);
module.exports = transMobFile

//dbInit.js
import transMobFile from 'data/transMob';
..
PDB.getDoc('TransMob')
.then((doc)=> {
  if (!doc) {
  global.locales = transMobFile.localesMob; // locales
  global.translations = transMobFile.langMob; // translations
  return PDB.saveDoc('TransMob', transMobFile)
  }
})

【讨论】:

    【解决方案2】:

    您可以使用 react-native-fs 从 /android/app/src/main/assets 加载文件。只需将文件放入 assets 文件夹并使用 RNFS.readFileAssets 读取即可。

    import PouchDB from 'pouchdb-core';
    
    PouchDB
     .plugin(require('pouchdb-find'))
     .plugin(require('pouchdb-load'));
    
    import RNFS from 'react-native-fs';
    
    const localDB = new PouchDB("cs1", {adapter: 'asyncstorage'});
    
    
    localDB.get("_local/initial_load_complete")
     .catch(function(err) {
       console.log("loading dumpfile");
       if (err.status !== 404) {
        // 404 means not found
         throw err;
       }
    
     RNFS.readFileAssets('yourdb.txt', 'utf8')
        .then((contents) => {
          localDB.load(contents).then(function() {
            return localDB.put({ _id: "_local/initial_load_complete" });
          }).then(function() {
            // at this point, we are sure that
            // initial replication is complete
            console.log("loading is complete!");
            return localDB.allDocs({ include_docs: true }).then(
              function(res) {
                // display all docs for debugging purposes (empty)
                console.log(res);
              });
          }, function(err) {
            console.log(err);
          });
        })
     })
    

    您需要重新构建项目,重新加载是不够的。 当我尝试加载一个 30MB 的文件时,我的项目崩溃了,所以我可能会将它拆分为几个较小的文件。如果需要,请查看 https://github.com/pouchdb-community/pouchdb-load 以了解其工作原理。

    【讨论】:

    • hmmm,安装它,链接它,添加导入,修改我的代码,得到错误:readFileAssets 在这个平台上不可用
    【解决方案3】:

    我发现pouchdb-load 模块中的db.load() 函数需要一个URL。我将它指向设备文件系统上的文件路径。我将我的 database.txt 文件放在我的服务器上,将其更改为使用 url 并且它工作。

    在我看来这并不理想,因为如果他们安装了应用程序并且无线速度很慢,它仍然必须从服务器中提取文件。但是,它仍然比在应用首次打开时执行完全复制要快得多。

    【讨论】:

    • 无限快...所以现在需要 0 时间? :)
    • 但更严肃地说......我认为这个答案并不完整。 db.load() 是什么?这不是常规(记录在案的)PouchDB API 的一部分。你在使用插件吗?最好把它包括在这里。
    • 是的,初始加载时间几乎是即时的,太棒了。我使用了这个模块 - github.com/pouchdb-community/pouchdb-load
    猜你喜欢
    • 2020-04-09
    • 2018-11-04
    • 2021-03-19
    • 1970-01-01
    • 2016-05-17
    • 1970-01-01
    • 2016-01-14
    • 2017-01-23
    • 2014-06-01
    相关资源
    最近更新 更多