|
@@ -1,16 +1,24 @@
|
|
|
import { Injectable } from '@angular/core';
|
|
|
import { Platform } from 'ionic-angular';
|
|
|
import PouchDB from 'pouchdb';
|
|
|
-import * as Relational from 'relational-pouch';
|
|
|
-import * as Sqlite from 'pouchdb-adapter-cordova-sqlite';
|
|
|
+import * as PouchRelational from 'relational-pouch';
|
|
|
+import * as PouchUpsert from 'pouchdb-upsert';
|
|
|
+import * as PouchSqlite from 'pouchdb-adapter-cordova-sqlite';
|
|
|
import each from 'async/each';
|
|
|
|
|
|
-PouchDB.plugin(Relational).plugin(Sqlite);
|
|
|
+PouchDB.plugin(PouchRelational).plugin(PouchUpsert).plugin(PouchSqlite);
|
|
|
|
|
|
@Injectable()
|
|
|
export class DataProvider {
|
|
|
|
|
|
- data: any;
|
|
|
+ public static readonly DOCS = {
|
|
|
+ PRODUCT_TEMPLATE: 'product.template',
|
|
|
+ PRODUCT_ATTRIBUTE: 'product.attribute',
|
|
|
+ PRODUCT_ATTRIBUTE_LINE: 'product.attribute.line',
|
|
|
+ PRODUCT_ATTRIBUTE_VALUE: 'product.attribute.value'
|
|
|
+ };
|
|
|
+
|
|
|
+ db: any;
|
|
|
schema: Array<any> = [
|
|
|
{
|
|
|
singular: 'partner',
|
|
@@ -29,8 +37,8 @@ export class DataProvider {
|
|
|
plural: 'companies'
|
|
|
},
|
|
|
{
|
|
|
- singular: 'product_template',
|
|
|
- plural: 'product_templates'
|
|
|
+ singular: 'product.template',
|
|
|
+ plural: 'product.templates'
|
|
|
},
|
|
|
{
|
|
|
singular: 'warehouse',
|
|
@@ -45,16 +53,16 @@ export class DataProvider {
|
|
|
plural: 'leads'
|
|
|
},
|
|
|
{
|
|
|
- singular: 'product_attribute',
|
|
|
- plural: 'product_attributes'
|
|
|
+ singular: 'product.attribute',
|
|
|
+ plural: 'product.attributes'
|
|
|
},
|
|
|
{
|
|
|
- singular: 'product_attribute_line',
|
|
|
- plural: 'product_attribute_lines'
|
|
|
+ singular: 'product.attribute.line',
|
|
|
+ plural: 'product.attribute.lines'
|
|
|
},
|
|
|
{
|
|
|
- singular: 'product_attribute_value',
|
|
|
- plural: 'product_attribute_values'
|
|
|
+ singular: 'product.attribute.value',
|
|
|
+ plural: 'product.attribute.values'
|
|
|
},
|
|
|
{
|
|
|
singular: 'product',
|
|
@@ -72,12 +80,12 @@ export class DataProvider {
|
|
|
*
|
|
|
*/
|
|
|
private initialize(): void {
|
|
|
- this.data = new PouchDB('odoo', {
|
|
|
+ this.db = new PouchDB('odoo', {
|
|
|
auto_compaction: true,
|
|
|
adapter: this.platform.is('core') ? 'idb' : 'websql'
|
|
|
});
|
|
|
|
|
|
- this.data.setSchema(this.schema);
|
|
|
+ this.db.setSchema(this.schema);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -104,7 +112,7 @@ export class DataProvider {
|
|
|
if (data instanceof Array) {
|
|
|
|
|
|
each(data, (i, c) => {
|
|
|
- this.data.rel.save(type, i).then(() => {
|
|
|
+ this.save(type, i).then(() => {
|
|
|
c();
|
|
|
}).catch(e => {
|
|
|
c(e);
|
|
@@ -118,11 +126,18 @@ export class DataProvider {
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
- this.data.rel.save(type, data).then(doc => {
|
|
|
+
|
|
|
+ this.db.rel.save(type, data).then(doc => {
|
|
|
resolve(doc[this.getPlural(type)][0]);
|
|
|
}).catch(e => {
|
|
|
- reject(e);
|
|
|
- });
|
|
|
+ this.db.upsert(data.id, doc => {
|
|
|
+ return doc;
|
|
|
+ }).then(r => {
|
|
|
+ resolve(r);
|
|
|
+ }).catch(e => {
|
|
|
+ reject(e);
|
|
|
+ });
|
|
|
+ });
|
|
|
}
|
|
|
});
|
|
|
}
|
|
@@ -133,7 +148,7 @@ export class DataProvider {
|
|
|
delete(type: string, data: any): any {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
this.platform.ready().then(r => {
|
|
|
- resolve(data.remote_id ? this.save(type, data) : this.data.rel.del(type, data));
|
|
|
+ resolve(data.remote_id ? this.save(type, data) : this.db.rel.del(type, data));
|
|
|
});
|
|
|
});
|
|
|
}
|
|
@@ -145,7 +160,7 @@ export class DataProvider {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
this.getAll(type).then(data => {
|
|
|
each(data[type], (i, c) => {
|
|
|
- this.data.rel.del(type, i).then(r => {
|
|
|
+ this.db.rel.del(type, i).then(r => {
|
|
|
c();
|
|
|
}).catch(e => {
|
|
|
c(e);
|
|
@@ -168,7 +183,20 @@ export class DataProvider {
|
|
|
*/
|
|
|
getAll(type: string): Promise<any> {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
- this.data.rel.find(type).then(result => {
|
|
|
+ this.db.rel.find(type).then(result => {
|
|
|
+ resolve(result[this.getPlural(type)]);
|
|
|
+ }).catch(e => {
|
|
|
+ reject(e);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ get(type: string, id?: string) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ this.db.rel.find(type, id).then(result => {
|
|
|
resolve(result[this.getPlural(type)]);
|
|
|
}).catch(e => {
|
|
|
reject(e);
|
|
@@ -181,7 +209,12 @@ export class DataProvider {
|
|
|
*/
|
|
|
dropDatabase(): any {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
- resolve(this.data.destroy());
|
|
|
+ this.db.destroy().then(r => {
|
|
|
+ this.initialize();
|
|
|
+ resolve(r);
|
|
|
+ }).catch(e => {
|
|
|
+ reject(e);
|
|
|
+ });
|
|
|
});
|
|
|
}
|
|
|
|
|
@@ -189,6 +222,6 @@ export class DataProvider {
|
|
|
*
|
|
|
*/
|
|
|
parseDocID(id: string): any {
|
|
|
- return this.data.rel.parseDocID(id);
|
|
|
+ return this.db.rel.parseDocID(id);
|
|
|
}
|
|
|
}
|