【问题标题】:How can I copy pouchdb 0000003.log file to Ionic 5 and retrieve the data?如何将 pouchdb 0000003.log 文件复制到 Ionic 5 并检索数据?
【发布时间】:2021-09-07 07:35:12
【问题描述】:

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

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 应用中导出/导入现有数据库?我必须存储在文件系统或 indexeddb 中吗?

【问题讨论】:

  • 这能回答你的问题吗? How to import/export database from PouchDB
  • 不完全,但它给了我一些想法
  • 您的用例是什么?这是一个存在于项目文件系统中的数据库,并且您想使用该固定数据库部署应用程序吗?确实需要更多细节,因为有很多选项,但是如果没有您明确的用例,任何答案都将是一个疯狂的猜测。

标签: javascript angular ionic-framework pouchdb


【解决方案1】:

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

https://github.com/ramblin-rose/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,然后将该 db 序列化到 app/assets/db,稍后由 ionic 应用程序加载。

重要的代码:

 // create some trivial docs
    const docs = [];
    const dt = new Date(2021, 6, 4, 12, 0, 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)
        );
      }
    }

【讨论】:

    【解决方案2】:

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

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


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

    【讨论】:

    • 问题与适配器无关。 OP 在他的项目文件夹中有一个罐头数据库,并希望序列化该数据库,然后将其反序列化为应用程序实例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多