javascript - 在 Angular2+ 中正确使用 PouchDB?
问题描述
我试图将对象存储在客户端的 pouchdb 中。
创建数据库并获取所有文档运行良好。但是当我尝试通过 db.find() 查询数据库时,需要几分钟才能得到它——浏览器也会滞后,CPU 和 RAM 也会增加很多。
我知道有错误,但我不知道我做错了什么。
这里是角度 pouchDB 服务:
import { Injectable } from '@angular/core';
import PouchDB from 'pouchdb';
import PouchFind from 'pouchdb-find';
import { IImage } from '../_interfaces/image';
import { ISelector } from '../_interfaces/pouchdb-selector';
PouchDB.plugin(PouchFind);
@Injectable({
providedIn: 'root'
})
export class PouchdbService {
public pouchdb = new PouchDB("pouchform");
constructor() {
this.pouchdb.info().then(function (info) {
console.log(info);
})
}
insert(content: IImage){
this.pouchdb.post(content)
}
getAll(): Promise<IImage[]>{
return new Promise<IImage[]>((resolve, reject) => {
this.pouchdb.allDocs({include_docs: true, attachments: true})
.then(data => {
let rows: any = data.rows
let images: IImage[] = rows.map(d => d.doc)
resolve(images)
})
})
}
query(selector: ISelector): Promise<any>{
return new Promise<any>((resolve, reject) => {
this.pouchdb.find({
selector: selector.selectors // e.g. it is {cam_brand: "Apple"}
}).then(result => {
console.log(result)
resolve(result)
}).catch(err => {
console.log("err", err)
})
})
}
}
查询:
this.pouchdb.query({selectors: {cam_brand: "Apple"}}).then(i => {
console.log("filtered" , i)
})
我之前也尝试过 createIndex,但它并没有提高性能
例如:
- 该对象仅包含绝对图像路径(<200 个字符)和 camera_brand 字符串
- 它在数据库中只有 400 个对象,相当少
编辑:现在我直接用 indexedDB 尝试了 id。当我添加这 400 个对象(在 for 循环中一个接一个)时,循环在几毫秒内完成。等待 2-3 分钟后,插入的响应发生。我想我对 indexedDB 本身有一些问题。
如果这有帮助,我正在使用带有电子的 Angular 10。
EDIT-Electron:我现在将 pouchdb 迁移到一个新的锅炉模板中。在平台上的“热重载”功能上:“WEB”它工作正常。但是,当我构建具有平台 win32 的电子产品时,它的行为同样缓慢,我需要一些特殊的进口来解决这个问题吗?
解决方案
推荐阅读
- javascript - 无法使用外部 css 文件删除 Material Ui 表默认样式
- c++ - OpenCV 拼接模式:SCANS 因某些属性而崩溃
- curl - Charles 代理 localhost 到 https
- sql - SQL Server 为计算列提供值
- sql-server - AX 2012 - 通过非生产恢复生产
- codenameone - createBackgroundMedia 方法不适用于 URL 和本地文件
- architecture - 单周期 mips,存储 word(sw) 控制值,为什么 MemRead 为 0 而不是“不关心”?
- google-cloud-platform - 如何使用 Google Cloud Armor 将 GKE 上的几个 IP 列入白名单?
- java - MethodHandles.lookup().defineClass 保留
- r - 我希望 R 在将我的输出导出为 pdf 时提示输入文件名和位置