|
@@ -11,13 +11,61 @@ PouchDB.plugin(Relational).plugin(Sqlite);
|
|
|
export class DataProvider {
|
|
|
|
|
|
data: any;
|
|
|
+ schema: Array<any> = [
|
|
|
+ {
|
|
|
+ singular: 'partner',
|
|
|
+ plural: 'partners'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ singular: 'user',
|
|
|
+ plural: 'users'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ singular: 'currency',
|
|
|
+ plural: 'currencies'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ singular: 'company',
|
|
|
+ plural: 'companies'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ singular: 'product_template',
|
|
|
+ plural: 'product_templates'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ singular: 'warehouse',
|
|
|
+ plural: 'warehouses',
|
|
|
+ },
|
|
|
+ {
|
|
|
+ singular: 'stock',
|
|
|
+ plural: 'stocks'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ singular: 'lead',
|
|
|
+ plural: 'leads'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ singular: 'product_attribute',
|
|
|
+ plural: 'product_attributes'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ singular: 'product_attribute_line',
|
|
|
+ plural: 'product_attribute_lines'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ singular: 'product_attribute_value',
|
|
|
+ plural: 'product_attribute_values'
|
|
|
+ },
|
|
|
+ {
|
|
|
+ singular: 'product',
|
|
|
+ plural: 'products'
|
|
|
+ }
|
|
|
+ ];
|
|
|
|
|
|
constructor(
|
|
|
public platform: Platform
|
|
|
) {
|
|
|
- platform.ready().then(r => {
|
|
|
- this.initialize();
|
|
|
- });
|
|
|
+ this.initialize();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -29,56 +77,21 @@ export class DataProvider {
|
|
|
adapter: this.platform.is('core') ? 'idb' : 'websql'
|
|
|
});
|
|
|
|
|
|
- this.data.setSchema([
|
|
|
- {
|
|
|
- singular: 'partner',
|
|
|
- plural: 'partners'
|
|
|
- },
|
|
|
- {
|
|
|
- singular: 'user',
|
|
|
- plural: 'users'
|
|
|
- },
|
|
|
- {
|
|
|
- singular: 'currency',
|
|
|
- plural: 'currencies'
|
|
|
- },
|
|
|
- {
|
|
|
- singular: 'company',
|
|
|
- plural: 'companies'
|
|
|
- },
|
|
|
- {
|
|
|
- singular: 'product_template',
|
|
|
- plural: 'product_templates'
|
|
|
- },
|
|
|
- {
|
|
|
- singular: 'warehouse',
|
|
|
- plural: 'warehouses',
|
|
|
- },
|
|
|
- {
|
|
|
- singular: 'stock',
|
|
|
- plural: 'stocks'
|
|
|
- },
|
|
|
- {
|
|
|
- singular: 'lead',
|
|
|
- plural: 'leads'
|
|
|
- },
|
|
|
- {
|
|
|
- singular: 'product_attribute',
|
|
|
- plural: 'product_attributes'
|
|
|
- },
|
|
|
- {
|
|
|
- singular: 'product_attribute_line',
|
|
|
- plural: 'product_attribute_lines'
|
|
|
- },
|
|
|
- {
|
|
|
- singular: 'product_attribute_value',
|
|
|
- plural: 'product_attribute_value'
|
|
|
- },
|
|
|
- {
|
|
|
- singular: 'product',
|
|
|
- plural: 'products'
|
|
|
+ this.data.setSchema(this.schema);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ private getPlural(singular: string) {
|
|
|
+ for (let i = 0; i < this.schema.length; i++) {
|
|
|
+ if (this.schema[i].singular === singular) {
|
|
|
+ return this.schema[i].plural;
|
|
|
}
|
|
|
- ]);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -88,27 +101,29 @@ export class DataProvider {
|
|
|
*/
|
|
|
save(type: string, data: any): Promise<any> {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
- this.platform.ready().then(r => {
|
|
|
- if (data instanceof Array) {
|
|
|
+ if (data instanceof Array) {
|
|
|
|
|
|
- each(data, (i, c) => {
|
|
|
- this.data.rel.save(type, i).then(() => {
|
|
|
- c();
|
|
|
- }).catch(e => {
|
|
|
- c(e);
|
|
|
- });
|
|
|
- }, e => {
|
|
|
- if (e) {
|
|
|
- reject(e);
|
|
|
- } else {
|
|
|
- resolve();
|
|
|
- }
|
|
|
+ each(data, (i, c) => {
|
|
|
+ this.data.rel.save(type, i).then(() => {
|
|
|
+ c();
|
|
|
+ }).catch(e => {
|
|
|
+ c(e);
|
|
|
});
|
|
|
+ }, e => {
|
|
|
+ if (e) {
|
|
|
+ reject(e);
|
|
|
+ } else {
|
|
|
+ resolve();
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
- } else {
|
|
|
- resolve(this.data.rel.save(type, data));
|
|
|
- }
|
|
|
- });
|
|
|
+ } else {
|
|
|
+ this.data.rel.save(type, data).then(doc => {
|
|
|
+ resolve(doc[this.getPlural(type)][0]);
|
|
|
+ }).catch(e => {
|
|
|
+ reject(e);
|
|
|
+ });
|
|
|
+ }
|
|
|
});
|
|
|
}
|
|
|
|
|
@@ -153,11 +168,12 @@ export class DataProvider {
|
|
|
*/
|
|
|
getAll(type: string): Promise<any> {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
- this.platform.ready().then(r => {
|
|
|
- resolve(this.data.rel.find(type));
|
|
|
+ this.data.rel.find(type).then(result => {
|
|
|
+ resolve(result[this.getPlural(type)]);
|
|
|
+ }).catch(e => {
|
|
|
+ reject(e);
|
|
|
});
|
|
|
});
|
|
|
-
|
|
|
}
|
|
|
|
|
|
/**
|