And*_*tev 7 reactjs electron rxdb
案例
如何打开已有的数据库?
问题
我已经使用 RxDB.create() 创建了一个数据库,并创建了一些集合,在其中放入了一些文档因此,在另一个脚本中,我想打开该数据库来执行一些查询,但我不知道该怎么做。我尝试使用具有相同数据库名称的 RxDB.create() ,但它创建新数据库并覆盖上面创建的数据库。
代码示例
const database = await createRxDatabase({
name: path.join(app.getPath('userData') + '/db'),
adapter: leveldown, // the full leveldown-module
multiInstance: false,
});
Run Code Online (Sandbox Code Playgroud)
我只是没有看到适当的方法来检查 .ldb 文件是否存在以及如何在不创建新文件的情况下获取对 rxdb 数据库对象的引用。
信息
环境:电子
适配器:LevelDB
堆栈:React
包: “rxdb”:“9.5.0”,“leveldown”:“5.6.0”, “pouchdb-adapter-leveldb”:“7.2.2”
为时已晚,但值得分享,因为有关 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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1498 次 |
| 最近记录: |