【问题标题】:RxDB - Use the existing local DBRxDB - 使用现有的本地数据库
【发布时间】:2022-03-24 02:16:15
【问题描述】:

案例
如何打开已有的数据库?

问题
我已经使用 RxDB.create() 创建了一个数据库,并创建了一些集合,将一些文档放入其中 所以,在另一个脚本中,我想打开那个数据库来执行一些查询,但我不知道该怎么做。 我尝试使用具有相同数据库名称的 RxDB.create(),但它会创建新数据库并覆盖上面创建的数据库。

代码示例

 const database = await createRxDatabase({
    name: path.join(app.getPath('userData') + '/db'),
    adapter: leveldown, // the full leveldown-module
    multiInstance: false,
  });

我只是看不到检查 .ldb 文件是否存在以及如何在不创建新文件的情况下获取对 rxdb 数据库对象的引用的适当方法。

信息
环境:电子
适配器:LevelDB
堆栈:反应
包:“rxdb”:“9.5.0”,“leveldown”:“5.6.0”, “pouchdb-adapter-leveldb”:“7.2.2”

【问题讨论】:

  • 这个错误发生在我身上,因为出于某种原因,我在我的应用程序中多次调用了这段代码。你确定你只调用一次吗?

标签: reactjs electron rxdb


【解决方案1】:

为时已晚,但值得分享,因为关于 rxdb 的参考资料太少

找到了 react-native (iOS/Android) (rxdb 11.6.0) 的解决方法

对我来说效果很好,数据仍然是持久的

// database instance to be used throughout the runtime
let rxDB: RxDatabase<AppCollections>;

async function createRxDatabaseAsync() {
  // re-create RxDatabase by using the function below 
  // does not override existing data
  rxDB = await createRxDatabase<AppCollections>({
    name: 'mydatabase',
    storage: getRxStoragePouch('react-native-sqlite'),
    ignoreDuplicate: false,
  });
}

export async function initRxDatabaseAsync() {
  if (!rxDB) {
    await createRxDatabaseAsync();
  }

  // make sure we add the collection(s) only one time
  // my case is 'word' collection
  if (!rxDB.word) {
    await rxDB.addCollections({
      word: {
        schema: wordSchema,
      },
    });
  }
  return rxDB;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 2021-06-24
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多