data-provider.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { Injectable } from '@angular/core';
  2. import { Platform } from 'ionic-angular';
  3. import PouchDB from 'pouchdb';
  4. import * as Relational from 'relational-pouch';
  5. import * as Sqlite from 'pouchdb-adapter-cordova-sqlite';
  6. import each from 'async/each';
  7. PouchDB.plugin(Relational).plugin(Sqlite);
  8. @Injectable()
  9. export class DataProvider {
  10. data: any;
  11. schema: Array<any> = [
  12. {
  13. singular: 'partner',
  14. plural: 'partners'
  15. },
  16. {
  17. singular: 'user',
  18. plural: 'users'
  19. },
  20. {
  21. singular: 'currency',
  22. plural: 'currencies'
  23. },
  24. {
  25. singular: 'company',
  26. plural: 'companies'
  27. },
  28. {
  29. singular: 'product_template',
  30. plural: 'product_templates'
  31. },
  32. {
  33. singular: 'warehouse',
  34. plural: 'warehouses',
  35. },
  36. {
  37. singular: 'stock',
  38. plural: 'stocks'
  39. },
  40. {
  41. singular: 'lead',
  42. plural: 'leads'
  43. },
  44. {
  45. singular: 'product_attribute',
  46. plural: 'product_attributes'
  47. },
  48. {
  49. singular: 'product_attribute_line',
  50. plural: 'product_attribute_lines'
  51. },
  52. {
  53. singular: 'product_attribute_value',
  54. plural: 'product_attribute_values'
  55. },
  56. {
  57. singular: 'product',
  58. plural: 'products'
  59. }
  60. ];
  61. constructor(
  62. public platform: Platform
  63. ) {
  64. this.initialize();
  65. }
  66. /**
  67. *
  68. */
  69. private initialize(): void {
  70. this.data = new PouchDB('odoo', {
  71. auto_compaction: true,
  72. adapter: this.platform.is('core') ? 'idb' : 'websql'
  73. });
  74. this.data.setSchema(this.schema);
  75. }
  76. /**
  77. *
  78. */
  79. private getPlural(singular: string) {
  80. for (let i = 0; i < this.schema.length; i++) {
  81. if (this.schema[i].singular === singular) {
  82. return this.schema[i].plural;
  83. }
  84. }
  85. return null;
  86. }
  87. /**
  88. * Save object or array of objects to database
  89. * @argument type: string
  90. * @argument data: object or array of objects
  91. */
  92. save(type: string, data: any): Promise<any> {
  93. return new Promise((resolve, reject) => {
  94. if (data instanceof Array) {
  95. each(data, (i, c) => {
  96. this.data.rel.save(type, i).then(() => {
  97. c();
  98. }).catch(e => {
  99. c(e);
  100. });
  101. }, e => {
  102. if (e) {
  103. reject(e);
  104. } else {
  105. resolve();
  106. }
  107. });
  108. } else {
  109. this.data.rel.save(type, data).then(doc => {
  110. resolve(doc[this.getPlural(type)][0]);
  111. }).catch(e => {
  112. reject(e);
  113. });
  114. }
  115. });
  116. }
  117. /**
  118. *
  119. */
  120. delete(type: string, data: any): any {
  121. return new Promise((resolve, reject) => {
  122. this.platform.ready().then(r => {
  123. resolve(data.remote_id ? this.save(type, data) : this.data.rel.del(type, data));
  124. });
  125. });
  126. }
  127. /**
  128. *
  129. */
  130. deleteAll(type: string): Promise<any> {
  131. return new Promise((resolve, reject) => {
  132. this.getAll(type).then(data => {
  133. each(data[type], (i, c) => {
  134. this.data.rel.del(type, i).then(r => {
  135. c();
  136. }).catch(e => {
  137. c(e);
  138. });
  139. }, e => {
  140. if (e) {
  141. reject(e);
  142. } else {
  143. resolve();
  144. }
  145. });
  146. }).catch(e => {
  147. reject(e);
  148. });
  149. });
  150. }
  151. /**
  152. *
  153. */
  154. getAll(type: string): Promise<any> {
  155. return new Promise((resolve, reject) => {
  156. this.data.rel.find(type).then(result => {
  157. resolve(result[this.getPlural(type)]);
  158. }).catch(e => {
  159. reject(e);
  160. });
  161. });
  162. }
  163. /**
  164. *
  165. */
  166. dropDatabase(): any {
  167. return new Promise((resolve, reject) => {
  168. resolve(this.data.destroy());
  169. });
  170. }
  171. /**
  172. *
  173. */
  174. parseDocID(id: string): any {
  175. return this.data.rel.parseDocID(id);
  176. }
  177. }