微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将 pouchdb 0000003.log 文件复制到 Ionic 5 并检索数据?

如何解决如何将 pouchdb 0000003.log 文件复制到 Ionic 5 并检索数据?

我的方案是在 ionic 中使用 pouch db 数据,我成功地将 pouch db 包添加到 ionic 并创建了一个示例,并且运行良好。现在我有一个场景,我有以下文件

enter image description here

000003.log 其中我拥有所有数据,但在 ionic 中它存储在 indexdb 中,那么我如何使用此 000003.log 数据并将其复制到 indexeddb 或有什么方法可以复制内容

下面是我的应用代码

import { Injectable } from '@angular/core';
import PouchDB from 'pouchdb';

@Injectable({
providedIn: 'root'
})
export class DataService {

private database: any;
private myNotes: any;

constructor() {
  this.database = new PouchDB('my-notes');
}

public addNote(theNote: string): Promise<string> {
  const promise = this.database
    .put({
      _id: ('note:' + (new Date()).getTime()),note: theNote
    })
    .then((result): string => (result.id));

  return (promise);
}

getMyNotes() {
  return new Promise(resolve => {
    let _self = this;
    this.database.allDocs({
      include_docs: true,attachments: true
    }).then(function (result) {
      // handle result
      _self.myNotes = result.rows;
      console.log("Results: " + JSON.stringify(_self.myNotes));
      resolve(_self.myNotes);

    }).catch(function (err) {
      console.log(err);
    });
  });
}

如何在ionic app中导出/导入现有的数据库?我必须存储在文件系统或 indexeddb 中吗?

解决方法

默认情况下,PouchDb 将使用 IndexDb,因此它正确执行。如果要更改存储,则需要设置不同的适配器。

我没有看到你在哪里设置本地适配器的选项,所以我认为you are missing the local & adapter setup options支持它

how to setup PouchDb with Iconic locally and Remote


现在使用您想要的正确适配器PouchDB here

PouchDb adapter setup

,

我创建了一个 Ionic 5/Angular 存储库,它演示了如何按照 OP 中的描述获取本地 pouchdb,并将其加载为应用程序中的默认罐装数据库。

https://github.com/RambleOnRose/canned-pouch-db

障碍并不大,但一路上我遇到了一些问题,主要是关于 pouchdb 的 es 模块和模块默认导出的一些争论。

具体来说,pouchdb-replication-stream 的文档对 Ionic5/Angular 的合并没有帮助。我假设导入

import ReplicationStream from 'pouchdb-replication-stream';

可以正常工作,但不幸的是在运行时会弹出这个可怕的错误

类型错误:Promise 不是构造函数

哎哟!那是一个节目塞子。但是我遇到了 pouchdb-replication-stream 问题 es modules

提示解决方案:

import ReplicationStream from 'pouchdb-replication-stream/dist/pouchdb.replication-stream.min.js';

无论如何,repo 的亮点是“can-a-pouchdb.js”和“data.service.ts”。

can-a-pouchdb.js

此脚本将创建一个本地节点 pouchdb,然后将该数据库序列化为 app/assets/db,稍后由 ionic 应用程序加载。

重要的代码:

 // create some trivial docs
    const docs = [];
    const dt = new Date(2021,6,4,12,0);
    for (let i = 0; i < 10; i++,dt.setMinutes(dt.getMinutes() + i)) {
      docs[i] = {
        _id: "note:" + dt.getTime(),note: `Note number ${i}`,};
    }
    // always start clean - remove database dump file
    fs.rmdirSync(dbPath,{ recursive: true });

    PouchDB.plugin(replicationStream.plugin);
    PouchDB.adapter(
      "writableStream",replicationStream.adapters.writableStream
    );
    const db = new PouchDB(dbName);

    console.log(JSON.stringify(docs));
    await db.bulkDocs(docs);
    //
    // dump db to file.
    //
    fs.mkdirSync(dumpFileFolder,{ recursive: true });
    const ws = fs.createWriteStream(dumpFilePath);
    await db.dump(ws);

要重新创建固定数据库,请从 CL 运行以下命令:

$ node can-a-pouchdb.js

data.service.ts

以下是应用程序的 pouchdb 从罐头数据库中提取的方法。请注意数据库正在使用内存适配器,因为作为演示应用程序持久化数据库是可取的。

public async init(): Promise<void> {
    if (this.db === undefined) {
      PouchDB.plugin(PouchdbAdapterMemory);
      PouchDB.plugin(ReplicationStream.plugin);
      this.db = new PouchDB(DataService.dbName,{ adapter: 'memory' });
      // if the db is empty,hydrate it with the canned db assets/db
      const info = await this.db.info();
      if (info.doc_count === 0) {
        //load the asset into a string
        const cannedDbText = await this.http
          .get('/assets/db/mydb.dump.txt',{
            responseType: 'text',})
          .toPromise();
        // hydrate the db
        return (this.db as any).load(
          MemoryStream.createReadStream(cannedDbText)
        );
      }
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。