pouchdb generic database provider
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);
})
}
}