|
@@ -1,11 +1,14 @@
|
|
|
import { Injectable } from "@angular/core";
|
|
|
import { Observable } from "rxjs/Observable";
|
|
|
+import { Observer } from "rxjs/Observer";
|
|
|
import PouchDB from "pouchdb";
|
|
|
|
|
|
import * as PouchFind from "pouchdb-find";
|
|
|
|
|
|
import "rxjs/add/observable/throw";
|
|
|
import "rxjs/add/observable/fromPromise";
|
|
|
+import "rxjs/add/observable/from";
|
|
|
+import "rxjs/add/operator/concatMap";
|
|
|
|
|
|
PouchDB.plugin(PouchFind);
|
|
|
|
|
@@ -21,15 +24,16 @@ export class PouchService {
|
|
|
/**
|
|
|
*
|
|
|
*/
|
|
|
- private initialize(): void {
|
|
|
+ private initialize(): Observable<any> {
|
|
|
this.pouchDB = new PouchDB("odoo", {
|
|
|
auto_compaction: true
|
|
|
});
|
|
|
- this.pouchDB.createIndex({
|
|
|
+
|
|
|
+ return Observable.fromPromise(this.pouchDB.createIndex({
|
|
|
index: {
|
|
|
fields: ['type']
|
|
|
}
|
|
|
- }).then(response => console.log(response)).catch(error => console.log(error));
|
|
|
+ }));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -40,6 +44,9 @@ export class PouchService {
|
|
|
return Observable.throw("Error: Cannot save data without type");
|
|
|
}
|
|
|
|
|
|
+ delete data.__last_update;
|
|
|
+
|
|
|
+
|
|
|
return Observable.fromPromise(this.pouchDB.post(data));
|
|
|
}
|
|
|
|
|
@@ -54,6 +61,13 @@ export class PouchService {
|
|
|
return Observable.fromPromise(this.pouchDB.remove(data));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ removeAll(type: string): Observable<any> {
|
|
|
+ return this.getAll(type).concatMap(result => Observable.from(result.docs)).concatMap(doc => this.remove(Object.assign(doc, { _deleted: true })));
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
*
|
|
|
*/
|
|
@@ -77,4 +91,18 @@ export class PouchService {
|
|
|
}
|
|
|
}));
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ destroy(): Observable<any> {
|
|
|
+ return Observable.fromPromise(this.pouchDB.destroy());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ destroyAndInitialize() {
|
|
|
+ return this.destroy().concat(this.initialize());
|
|
|
+ }
|
|
|
}
|