UI for Zipcoin Blue

storage.js 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import { InjectionToken } from '@angular/core';
  2. import LocalForage from 'localforage';
  3. import CordovaSQLiteDriver from 'localforage-cordovasqlitedriver';
  4. /**
  5. * Storage is an easy way to store key/value pairs and JSON objects.
  6. * Storage uses a variety of storage engines underneath, picking the best one available
  7. * depending on the platform.
  8. *
  9. * When running in a native app context, Storage will prioritize using SQLite, as it's one of
  10. * the most stable and widely used file-based databases, and avoids some of the
  11. * pitfalls of things like localstorage and IndexedDB, such as the OS deciding to clear out such
  12. * data in low disk-space situations.
  13. *
  14. * When running in the web or as a Progressive Web App, Storage will attempt to use
  15. * IndexedDB, WebSQL, and localstorage, in that order.
  16. *
  17. * @usage
  18. * First, if you'd like to use SQLite, install the cordova-sqlite-storage plugin:
  19. * ```bash
  20. * ionic cordova plugin add cordova-sqlite-storage
  21. * ```
  22. *
  23. * Next, install the package (comes by default for Ionic apps > Ionic V1):
  24. * ```bash
  25. * npm install --save @ionic/storage
  26. * ```
  27. *
  28. * Next, add it to the imports list in your `NgModule` declaration (for example, in `src/app/app.module.ts`):
  29. *
  30. * ```typescript
  31. * import { IonicStorageModule } from '@ionic/storage';
  32. *
  33. * @NgModule({
  34. * declarations: [
  35. * // ...
  36. * ],
  37. * imports: [
  38. * BrowserModule,
  39. * IonicModule.forRoot(MyApp),
  40. * IonicStorageModule.forRoot()
  41. * ],
  42. * bootstrap: [IonicApp],
  43. * entryComponents: [
  44. * // ...
  45. * ],
  46. * providers: [
  47. * // ...
  48. * ]
  49. * })
  50. * export class AppModule {}
  51. *```
  52. *
  53. * Finally, inject it into any of your components or pages:
  54. * ```typescript
  55. * import { Storage } from '@ionic/storage';
  56. * export class MyApp {
  57. * constructor(private storage: Storage) { }
  58. *
  59. * ...
  60. *
  61. * // set a key/value
  62. * storage.set('name', 'Max');
  63. *
  64. * // Or to get a key/value pair
  65. * storage.get('age').then((val) => {
  66. * console.log('Your age is', val);
  67. * });
  68. * }
  69. * ```
  70. *
  71. *
  72. * ### Configuring Storage
  73. *
  74. * The Storage engine can be configured both with specific storage engine priorities, or custom configuration
  75. * options to pass to localForage. See the localForage config docs for possible options: https://github.com/localForage/localForage#configuration
  76. *
  77. * Note: Any custom configurations will be merged with the default configuration
  78. *
  79. * ```typescript
  80. * import { IonicStorageModule } from '@ionic/storage';
  81. *
  82. * @NgModule({
  83. * declarations: [...],
  84. * imports: [
  85. * IonicStorageModule.forRoot({
  86. * name: '__mydb',
  87. driverOrder: ['indexeddb', 'sqlite', 'websql']
  88. * })
  89. * ],
  90. * bootstrap: [...],
  91. * entryComponents: [...],
  92. * providers: [...]
  93. * })
  94. * export class AppModule { }
  95. * ```
  96. */
  97. var Storage = (function () {
  98. /**
  99. * Create a new Storage instance using the order of drivers and any additional config
  100. * options to pass to LocalForage.
  101. *
  102. * Possible driver options are: ['sqlite', 'indexeddb', 'websql', 'localstorage'] and the
  103. * default is that exact ordering.
  104. */
  105. function Storage(config) {
  106. var _this = this;
  107. this._driver = null;
  108. this._dbPromise = new Promise(function (resolve, reject) {
  109. var db;
  110. var defaultConfig = getDefaultConfig();
  111. var actualConfig = Object.assign(defaultConfig, config || {});
  112. LocalForage.defineDriver(CordovaSQLiteDriver).then(function () {
  113. db = LocalForage.createInstance(actualConfig);
  114. })
  115. .then(function () { return db.setDriver(_this._getDriverOrder(actualConfig.driverOrder)); })
  116. .then(function () {
  117. _this._driver = db.driver();
  118. resolve(db);
  119. })
  120. .catch(function (reason) { return reject(reason); });
  121. });
  122. }
  123. Object.defineProperty(Storage.prototype, "driver", {
  124. /**
  125. * Get the name of the driver being used.
  126. * @returns {string | null} Name of the driver
  127. */
  128. get: function () {
  129. return this._driver;
  130. },
  131. enumerable: true,
  132. configurable: true
  133. });
  134. /**
  135. * Reflect the readiness of the store.
  136. * @returns {Promise<LocalForage>} Returns a promise that resolves when the store is ready
  137. */
  138. Storage.prototype.ready = function () {
  139. return this._dbPromise;
  140. };
  141. Storage.prototype._getDriverOrder = function (driverOrder) {
  142. return driverOrder.map(function (driver) {
  143. switch (driver) {
  144. case 'sqlite':
  145. return CordovaSQLiteDriver._driver;
  146. case 'indexeddb':
  147. return LocalForage.INDEXEDDB;
  148. case 'websql':
  149. return LocalForage.WEBSQL;
  150. case 'localstorage':
  151. return LocalForage.LOCALSTORAGE;
  152. }
  153. });
  154. };
  155. /**
  156. * Get the value associated with the given key.
  157. * @param {any} key the key to identify this value
  158. * @returns {Promise} Returns a promise with the value of the given key
  159. */
  160. Storage.prototype.get = function (key) {
  161. return this._dbPromise.then(function (db) { return db.getItem(key); });
  162. };
  163. /**
  164. * Set the value for the given key.
  165. * @param {any} key the key to identify this value
  166. * @param {any} value the value for this key
  167. * @returns {Promise} Returns a promise that resolves when the key and value are set
  168. */
  169. Storage.prototype.set = function (key, value) {
  170. return this._dbPromise.then(function (db) { return db.setItem(key, value); });
  171. };
  172. /**
  173. * Remove any value associated with this key.
  174. * @param {any} key the key to identify this value
  175. * @returns {Promise} Returns a promise that resolves when the value is removed
  176. */
  177. Storage.prototype.remove = function (key) {
  178. return this._dbPromise.then(function (db) { return db.removeItem(key); });
  179. };
  180. /**
  181. * Clear the entire key value store. WARNING: HOT!
  182. * @returns {Promise} Returns a promise that resolves when the store is cleared
  183. */
  184. Storage.prototype.clear = function () {
  185. return this._dbPromise.then(function (db) { return db.clear(); });
  186. };
  187. /**
  188. * @returns {Promise} Returns a promise that resolves with the number of keys stored.
  189. */
  190. Storage.prototype.length = function () {
  191. return this._dbPromise.then(function (db) { return db.length(); });
  192. };
  193. /**
  194. * @returns {Promise} Returns a promise that resolves with the keys in the store.
  195. */
  196. Storage.prototype.keys = function () {
  197. return this._dbPromise.then(function (db) { return db.keys(); });
  198. };
  199. /**
  200. * Iterate through each key,value pair.
  201. * @param {any} iteratorCallback a callback of the form (value, key, iterationNumber)
  202. * @returns {Promise} Returns a promise that resolves when the iteration has finished.
  203. */
  204. Storage.prototype.forEach = function (iteratorCallback) {
  205. return this._dbPromise.then(function (db) { return db.iterate(iteratorCallback); });
  206. };
  207. return Storage;
  208. }());
  209. export { Storage };
  210. /** @hidden */
  211. export function getDefaultConfig() {
  212. return {
  213. name: '_ionicstorage',
  214. storeName: '_ionickv',
  215. driverOrder: ['sqlite', 'indexeddb', 'websql', 'localstorage']
  216. };
  217. }
  218. ;
  219. /** @hidden */
  220. export var StorageConfigToken = new InjectionToken('STORAGE_CONFIG_TOKEN');
  221. /** @hidden */
  222. export function provideStorage(storageConfig) {
  223. var config = !!storageConfig ? storageConfig : getDefaultConfig();
  224. return new Storage(config);
  225. }