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