typescript pouchdb通用数据库提供程序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript pouchdb通用数据库提供程序相关的知识,希望对你有一定的参考价值。

import {DatabaseProvider} from "./database/database";
import {LoginComponent} from "../components/login/login";

export class GenericDatabaseProvider {

  docType: string;

  constructor(
    protected databaseProvider: DatabaseProvider,
    docType: string) {
    this.docType = docType;
  }

  save(item: any): Promise<any> {
    if(!item.agentUsername) {
      item.agentUsername = LoginComponent.userEmail;
    }
    return this.databaseProvider.save(item, this.docType);
  }

  saveByType(item: any, docType: string): Promise<any> {
    return this.databaseProvider.save(item, docType);
  }

  putAttachment(docType: string, item: any, attachment: any, contentType = 'image/jpeg') {
    return this.databaseProvider.db.rel.putAttachment(docType, item, `${item.id}.jpeg`, attachment, contentType);
  }

  findById(id) {
    return this.databaseProvider.db.rel.find(this.docType, id);
  }

  findByIdAndType(id, type) {
    return this.databaseProvider.db.rel.find(type, id);
  }

  getAttachment(docType: string, docId: string, attachmentId: string, bindData) {
    return this.databaseProvider.db.rel
      .getAttachment(docType, docId, attachmentId)
        .then((data) => {
          const reader = new FileReader();
          reader.onloadend = () => {
            bindData(reader.result);
          };
          if (data) {
            reader.readAsDataURL(data);
          }
        })
        .catch((e) => {
          console.log('Error fetching attachment');
          console.dir(e);
        })
  }

}

以上是关于typescript pouchdb通用数据库提供程序的主要内容,如果未能解决你的问题,请参考以下文章

TypeScript:通过提供类体创建通用匿名类

TypeScript - 通用约束可以提供“允许的”类型吗?

Ionic PouchDb Sqlite 插件问题

TypeScript 3 的通用 curry 函数

具有通用方法的Typescript接口

Typescript - 为非常通用的功能定义接口[重复]