|
@@ -5,8 +5,6 @@ import PouchDB from "pouchdb";
|
|
|
|
|
|
import * as PouchFind from "pouchdb-find";
|
|
|
|
|
|
-import { UUID } from "angular2-uuid";
|
|
|
-
|
|
|
import "rxjs/add/observable/throw";
|
|
|
import "rxjs/add/observable/fromPromise";
|
|
|
import "rxjs/add/observable/from";
|
|
@@ -14,12 +12,6 @@ import "rxjs/add/operator/concatMap";
|
|
|
|
|
|
PouchDB.plugin(PouchFind);
|
|
|
|
|
|
-export enum SaveAction {
|
|
|
- Add = 1,
|
|
|
- Update = 2,
|
|
|
- Delete = 3
|
|
|
-}
|
|
|
-
|
|
|
@Injectable()
|
|
|
export class PouchService {
|
|
|
|
|
@@ -33,6 +25,7 @@ export class PouchService {
|
|
|
*
|
|
|
*/
|
|
|
private initialize(destroy?: boolean): Observable<any> {
|
|
|
+ // PouchDB.debug.enable('pouchdb:api');
|
|
|
this.pouchDB = new PouchDB("odoo", {
|
|
|
auto_compaction: true
|
|
|
});
|
|
@@ -44,67 +37,30 @@ export class PouchService {
|
|
|
}));
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
*
|
|
|
*/
|
|
|
- save(data: any, action?: SaveAction): Observable<any> {
|
|
|
+ save(data: any): Observable<any> {
|
|
|
if (!data.odoo_model) {
|
|
|
return Observable.throw("Error: Cannot save data without model name");
|
|
|
}
|
|
|
|
|
|
- return this.getAll(data.odoo_model).concatMap(result => this.pouchDB.post(result.docs.length !== 0 ? Object.assign(result.docs.shift(), { records: data.records }) : data ));
|
|
|
- // return this.getAll(data.odoo_model).concatMap(r => this.pouchDB.post(this.alignData(r.docs.shift(), data, action)));
|
|
|
+ return Observable.fromPromise(this.pouchDB.post(data));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
- * @param dataStored
|
|
|
- * @param newData
|
|
|
- * @param action
|
|
|
+ * @param data
|
|
|
+ * @param model
|
|
|
*/
|
|
|
- protected alignData(dataStored: any, data: any, action?: SaveAction) {
|
|
|
- if (!dataStored) {
|
|
|
- return this.fillRecordsWithUUID(data);
|
|
|
- }
|
|
|
-
|
|
|
- if (!action) {
|
|
|
- dataStored.records = data.records;
|
|
|
- return this.fillRecordsWithUUID(dataStored);
|
|
|
- }
|
|
|
-
|
|
|
- if (action === SaveAction.Add) {
|
|
|
- dataStored.records = this.fillRecordsWithUUID(data).records.concat(dataStored.records);
|
|
|
- return dataStored;
|
|
|
- }
|
|
|
-
|
|
|
- if (action === SaveAction.Update) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- if (action === SaveAction.Delete) {
|
|
|
-
|
|
|
- }
|
|
|
+ bulkSave(data: Array<any>): Observable<any> {
|
|
|
+ return Observable.fromPromise(this.pouchDB.bulkDocs(data));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
* @param data
|
|
|
*/
|
|
|
- protected fillRecordsWithUUID(data: any): any {
|
|
|
- data.records = data.records.map(record => {
|
|
|
- record.odoo_id = record.id;
|
|
|
- record.id = UUID.UUID();
|
|
|
-
|
|
|
- return record;
|
|
|
- });
|
|
|
-
|
|
|
- return data;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- *
|
|
|
- */
|
|
|
remove(data: any): Observable<any> {
|
|
|
if (!data._id || !data._rev) {
|
|
|
return Observable.throw("Error: Cannot remove data without id or rev");
|
|
@@ -114,24 +70,22 @@ export class PouchService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
+ *
|
|
|
+ * @param data
|
|
|
*/
|
|
|
- removeAll(type: string): Observable<any> {
|
|
|
- return this.getAll(type).concatMap(result => this.cleanDoc(result.docs.shift()));
|
|
|
+ bulkRemove(data: Array<any>): Observable<any> {
|
|
|
+ return this.bulkSave(data.map(item => {
|
|
|
+ item._deleted = true;
|
|
|
+
|
|
|
+ return item;
|
|
|
+ }));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- *
|
|
|
- * @param doc
|
|
|
+ *
|
|
|
*/
|
|
|
- private cleanDoc(doc: any): Observable<any> {
|
|
|
- if (!doc || !doc.records) {
|
|
|
- return Observable.empty();
|
|
|
- }
|
|
|
-
|
|
|
- doc.records = [];
|
|
|
-
|
|
|
- return this.save(doc);
|
|
|
+ removeAll(type: string): Observable<any> {
|
|
|
+ return this.getAll(type).concatMap(result => this.bulkRemove(result.docs));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -171,4 +125,20 @@ export class PouchService {
|
|
|
destroyAndInitialize() {
|
|
|
return this.destroy().concatMap(response => this.initialize(response.ok));
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ changes(): Observable<any> {
|
|
|
+ return Observable.create((o: Observer<any>) => {
|
|
|
+ this.pouchDB.changes({
|
|
|
+ since: "now",
|
|
|
+ live: true
|
|
|
+ }).on("change", change => {
|
|
|
+ o.next(change);
|
|
|
+ }).on("error", error => {
|
|
|
+ o.next(error);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|