本文介绍了如何将 pouchdb 0000003.log 文件复制到 Ionic 5 并检索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My scenario is to use pouch db data in ionic and I successfully added pouch db package to ionic and created a sample and it worked fine. Now I have a scenario I have the below file

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

000003.log in which I have all the data, but in ionic it is storing in the indexdb so how can I use this 000003.log data and copy it to indexeddb or is there any way copy the contents ?

下面是我的应用代码

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 中吗?

How to export/import the existing database in ionic app? Do I have to store in file system or indexeddb?

推荐答案

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

I've created an Ionic 5/Angular repo that demonstrates how to take a local pouchdb as described in the OP and load it as a default canned database in the app.

https://github.com/ramblin-rose/canned-pouch-db

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

The hurdles were not huge, but I encountered some problems along the way, mainly some wrangling with respect to pouchdb's es modules and module default exports.

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

Specifically, the documentation for pouchdb-replication-stream is not helpful for incorporation for Ionic5/Angular. I assumed the import

import ReplicationStream from 'pouchdb-replication-stream';

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

Would just work, but unfortunately at runtime this dread error would popup

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

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

Ouch! That's a show stopper. However I came across the pouchdb-replication-stream issue es modules

提示解决方案:

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

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

Anyway the highlights of the repo are 'can-a-pouchdb.js' and 'data.service.ts'.

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

This script will create a local node pouchdb and then serialize that db to app/assets/db, which is later loaded by the ionic app.

重要的代码:

 // 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 运行以下命令:

To recreate the canned database run the following from the CL:

$ node can-a-pouchdb.js

data.service.ts

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

data.service.ts

Here's how the app's pouchdb is hydrated from the canned database. Take note the db is using the memory adapter, because as a demo app not persisting the db is desirable.

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)
        );
      }
    }

这篇关于如何将 pouchdb 0000003.log 文件复制到 Ionic 5 并检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 09:56