ngx-indexed-db 9.4.2 → 10.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/{esm2015/lib/ngx-indexed-db.meta.js → esm2020/lib/ngx-indexed-db.meta.mjs} +1 -1
- package/{esm2015/lib/ngx-indexed-db.js → esm2020/lib/ngx-indexed-db.mjs} +0 -0
- package/{esm2015/lib/ngx-indexed-db.service.js → esm2020/lib/ngx-indexed-db.service.mjs} +18 -14
- package/esm2020/lib/ngxindexeddb.module.mjs +24 -0
- package/{esm2015/ngx-indexed-db.js → esm2020/ngx-indexed-db.mjs} +0 -0
- package/{esm2015/public-api.js → esm2020/public-api.mjs} +0 -0
- package/{esm2015/utils/index.js → esm2020/utils/index.mjs} +0 -0
- package/fesm2015/{ngx-indexed-db.js → ngx-indexed-db.mjs} +26 -15
- package/fesm2015/ngx-indexed-db.mjs.map +1 -0
- package/fesm2020/ngx-indexed-db.mjs +661 -0
- package/fesm2020/ngx-indexed-db.mjs.map +1 -0
- package/lib/ngx-indexed-db.meta.d.ts +3 -0
- package/lib/ngx-indexed-db.service.d.ts +7 -4
- package/lib/ngxindexeddb.module.d.ts +5 -0
- package/ngx-indexed-db.d.ts +1 -0
- package/package.json +19 -7
- package/bundles/ngx-indexed-db.umd.js +0 -1001
- package/bundles/ngx-indexed-db.umd.js.map +0 -1
- package/esm2015/lib/ngxindexeddb.module.js +0 -19
- package/fesm2015/ngx-indexed-db.js.map +0 -1
- package/ngx-indexed-db.metadata.json +0 -1
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
import * as i0 from '@angular/core';
|
|
2
|
+
import { InjectionToken, PLATFORM_ID, Injectable, Inject, NgModule } from '@angular/core';
|
|
3
|
+
import { isPlatformBrowser, CommonModule } from '@angular/common';
|
|
4
|
+
import { Observable, from, combineLatest, Subject } from 'rxjs';
|
|
5
|
+
import { take } from 'rxjs/operators';
|
|
6
|
+
|
|
7
|
+
function openDatabase(indexedDB, dbName, version, upgradeCallback) {
|
|
8
|
+
return new Promise((resolve, reject) => {
|
|
9
|
+
if (!indexedDB) {
|
|
10
|
+
reject('IndexedDB not available');
|
|
11
|
+
}
|
|
12
|
+
const request = indexedDB.open(dbName, version);
|
|
13
|
+
let db;
|
|
14
|
+
request.onsuccess = (event) => {
|
|
15
|
+
db = request.result;
|
|
16
|
+
resolve(db);
|
|
17
|
+
};
|
|
18
|
+
request.onerror = (event) => {
|
|
19
|
+
reject(`IndexedDB error: ${request.error}`);
|
|
20
|
+
};
|
|
21
|
+
if (typeof upgradeCallback === 'function') {
|
|
22
|
+
request.onupgradeneeded = (event) => {
|
|
23
|
+
upgradeCallback(event, db);
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
function CreateObjectStore(indexedDB, dbName, version, storeSchemas, migrationFactory) {
|
|
29
|
+
if (!indexedDB) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const request = indexedDB.open(dbName, version);
|
|
33
|
+
request.onupgradeneeded = (event) => {
|
|
34
|
+
const database = event.target.result;
|
|
35
|
+
storeSchemas.forEach((storeSchema) => {
|
|
36
|
+
if (!database.objectStoreNames.contains(storeSchema.store)) {
|
|
37
|
+
const objectStore = database.createObjectStore(storeSchema.store, storeSchema.storeConfig);
|
|
38
|
+
storeSchema.storeSchema.forEach((schema) => {
|
|
39
|
+
objectStore.createIndex(schema.name, schema.keypath, schema.options);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
const storeMigrations = migrationFactory && migrationFactory();
|
|
44
|
+
if (storeMigrations) {
|
|
45
|
+
Object.keys(storeMigrations)
|
|
46
|
+
.map((k) => parseInt(k, 10))
|
|
47
|
+
.filter((v) => v > event.oldVersion)
|
|
48
|
+
.sort((a, b) => a - b)
|
|
49
|
+
.forEach((v) => {
|
|
50
|
+
storeMigrations[v](database, request.transaction);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
database.close();
|
|
54
|
+
};
|
|
55
|
+
request.onsuccess = (e) => {
|
|
56
|
+
e.target.result.close();
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function DeleteObjectStore(dbName, version, storeName) {
|
|
60
|
+
if (!dbName || !version || !storeName) {
|
|
61
|
+
throw Error('Params: "dbName", "version", "storeName" are mandatory.');
|
|
62
|
+
}
|
|
63
|
+
return new Observable((obs) => {
|
|
64
|
+
try {
|
|
65
|
+
const newVersion = version + 1;
|
|
66
|
+
const request = indexedDB.open(dbName, newVersion);
|
|
67
|
+
request.onupgradeneeded = (event) => {
|
|
68
|
+
const database = event.target.result;
|
|
69
|
+
database.deleteObjectStore(storeName);
|
|
70
|
+
database.close();
|
|
71
|
+
console.log('onupgradeneeded');
|
|
72
|
+
obs.next(true);
|
|
73
|
+
obs.complete();
|
|
74
|
+
};
|
|
75
|
+
request.onerror = (e) => obs.error(e);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
obs.error(error);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function validateStoreName(db, storeName) {
|
|
84
|
+
return db.objectStoreNames.contains(storeName);
|
|
85
|
+
}
|
|
86
|
+
function validateBeforeTransaction(db, storeName, reject) {
|
|
87
|
+
if (!db) {
|
|
88
|
+
reject('You need to use the openDatabase function to create a database before you query it!');
|
|
89
|
+
}
|
|
90
|
+
if (!validateStoreName(db, storeName)) {
|
|
91
|
+
reject(`objectStore does not exists: ${storeName}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function createTransaction(db, options) {
|
|
95
|
+
const trans = db.transaction(options.storeName, options.dbMode);
|
|
96
|
+
trans.onerror = options.error;
|
|
97
|
+
trans.onabort = options.abort;
|
|
98
|
+
return trans;
|
|
99
|
+
}
|
|
100
|
+
function optionsGenerator(type, storeName, reject, resolve) {
|
|
101
|
+
return {
|
|
102
|
+
storeName,
|
|
103
|
+
dbMode: type,
|
|
104
|
+
error: (e) => {
|
|
105
|
+
reject(e);
|
|
106
|
+
},
|
|
107
|
+
abort: (e) => {
|
|
108
|
+
reject(e);
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
var DBMode;
|
|
114
|
+
(function (DBMode) {
|
|
115
|
+
DBMode["readonly"] = "readonly";
|
|
116
|
+
DBMode["readwrite"] = "readwrite";
|
|
117
|
+
})(DBMode || (DBMode = {}));
|
|
118
|
+
const CONFIG_TOKEN = new InjectionToken(null);
|
|
119
|
+
|
|
120
|
+
class NgxIndexedDBService {
|
|
121
|
+
constructor(dbConfig, platformId) {
|
|
122
|
+
this.dbConfig = dbConfig;
|
|
123
|
+
this.platformId = platformId;
|
|
124
|
+
if (!dbConfig.name) {
|
|
125
|
+
throw new Error('NgxIndexedDB: Please, provide the dbName in the configuration');
|
|
126
|
+
}
|
|
127
|
+
if (!dbConfig.version) {
|
|
128
|
+
throw new Error('NgxIndexedDB: Please, provide the db version in the configuration');
|
|
129
|
+
}
|
|
130
|
+
this.isBrowser = isPlatformBrowser(this.platformId);
|
|
131
|
+
if (this.isBrowser) {
|
|
132
|
+
this.indexedDB =
|
|
133
|
+
window.indexedDB ||
|
|
134
|
+
window.mozIndexedDB ||
|
|
135
|
+
window.webkitIndexedDB ||
|
|
136
|
+
window.msIndexedDB;
|
|
137
|
+
CreateObjectStore(this.indexedDB, dbConfig.name, dbConfig.version, dbConfig.objectStoresMeta, dbConfig.migrationFactory);
|
|
138
|
+
openDatabase(this.indexedDB, dbConfig.name).then((db) => {
|
|
139
|
+
if (db.version !== dbConfig.version) {
|
|
140
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
141
|
+
console.warn(`
|
|
142
|
+
Your DB Config doesn't match the most recent version of the DB with name ${this.dbConfig.name}, please update it
|
|
143
|
+
DB current version: ${db.version};
|
|
144
|
+
Your configuration: ${dbConfig.version};
|
|
145
|
+
`);
|
|
146
|
+
console.warn(`Using latest version ${db.version}`);
|
|
147
|
+
}
|
|
148
|
+
this.dbConfig.version = db.version;
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Allows to crate a new object store ad-hoc
|
|
155
|
+
* @param storeName The name of the store to be created
|
|
156
|
+
* @param migrationFactory The migration factory if exists
|
|
157
|
+
*/
|
|
158
|
+
createObjectStore(storeSchema, migrationFactory) {
|
|
159
|
+
const storeSchemas = [storeSchema];
|
|
160
|
+
CreateObjectStore(this.indexedDB, this.dbConfig.name, ++this.dbConfig.version, storeSchemas, migrationFactory);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Adds new entry in the store and returns its key
|
|
164
|
+
* @param storeName The name of the store to add the item
|
|
165
|
+
* @param value The entry to be added
|
|
166
|
+
* @param key The optional key for the entry
|
|
167
|
+
*/
|
|
168
|
+
add(storeName, value, key) {
|
|
169
|
+
return new Observable((obs) => {
|
|
170
|
+
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
171
|
+
.then((db) => {
|
|
172
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
|
|
173
|
+
const objectStore = transaction.objectStore(storeName);
|
|
174
|
+
const request = Boolean(key) ? objectStore.add(value, key) : objectStore.add(value);
|
|
175
|
+
request.onsuccess = async (evt) => {
|
|
176
|
+
const result = evt.target.result;
|
|
177
|
+
const getRequest = objectStore.get(result);
|
|
178
|
+
getRequest.onsuccess = (event) => {
|
|
179
|
+
obs.next(event.target.result);
|
|
180
|
+
obs.complete();
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
})
|
|
184
|
+
.catch((error) => obs.error(error));
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Adds new entries in the store and returns its key
|
|
189
|
+
* @param storeName The name of the store to add the item
|
|
190
|
+
* @param values The entries to be added containing optional key attribute
|
|
191
|
+
*/
|
|
192
|
+
bulkAdd(storeName, values) {
|
|
193
|
+
const promises = new Promise((resolve, reject) => {
|
|
194
|
+
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
195
|
+
.then((db) => {
|
|
196
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, resolve, reject));
|
|
197
|
+
const objectStore = transaction.objectStore(storeName);
|
|
198
|
+
const results = values.map((value) => {
|
|
199
|
+
return new Promise((resolve1, reject1) => {
|
|
200
|
+
const key = value.key;
|
|
201
|
+
delete value.key;
|
|
202
|
+
const request = Boolean(key)
|
|
203
|
+
? objectStore.add(value, key)
|
|
204
|
+
: objectStore.add(value);
|
|
205
|
+
request.onsuccess = (evt) => {
|
|
206
|
+
const result = evt.target.result;
|
|
207
|
+
resolve1(result);
|
|
208
|
+
};
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
resolve(Promise.all(results));
|
|
212
|
+
}).catch((reason) => reject(reason));
|
|
213
|
+
});
|
|
214
|
+
return from(promises);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Delete entries in the store and returns current entries in the store
|
|
218
|
+
* @param storeName The name of the store to add the item
|
|
219
|
+
* @param keys The keys to be deleted
|
|
220
|
+
*/
|
|
221
|
+
bulkDelete(storeName, keys) {
|
|
222
|
+
const promises = keys.map((key) => {
|
|
223
|
+
return new Promise((resolve, reject) => {
|
|
224
|
+
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
225
|
+
.then((db) => {
|
|
226
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, reject, resolve));
|
|
227
|
+
const objectStore = transaction.objectStore(storeName);
|
|
228
|
+
objectStore.delete(key);
|
|
229
|
+
transaction.oncomplete = () => {
|
|
230
|
+
this.getAll(storeName)
|
|
231
|
+
.pipe(take(1))
|
|
232
|
+
.subscribe((newValues) => {
|
|
233
|
+
resolve(newValues);
|
|
234
|
+
});
|
|
235
|
+
};
|
|
236
|
+
})
|
|
237
|
+
.catch((reason) => reject(reason));
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
return from(Promise.all(promises));
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Returns entry by key.
|
|
244
|
+
* @param storeName The name of the store to query
|
|
245
|
+
* @param key The entry key
|
|
246
|
+
*/
|
|
247
|
+
getByKey(storeName, key) {
|
|
248
|
+
return new Observable((obs) => {
|
|
249
|
+
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
250
|
+
.then((db) => {
|
|
251
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
|
|
252
|
+
const objectStore = transaction.objectStore(storeName);
|
|
253
|
+
const request = objectStore.get(key);
|
|
254
|
+
request.onsuccess = (event) => {
|
|
255
|
+
obs.next(event.target.result);
|
|
256
|
+
obs.complete();
|
|
257
|
+
};
|
|
258
|
+
request.onerror = (event) => {
|
|
259
|
+
obs.error(event);
|
|
260
|
+
};
|
|
261
|
+
})
|
|
262
|
+
.catch((error) => obs.error(error));
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Retrieve multiple entries in the store
|
|
267
|
+
* @param storeName The name of the store to retrieve the items
|
|
268
|
+
* @param keys The ids entries to be retrieve
|
|
269
|
+
*/
|
|
270
|
+
bulkGet(storeName, keys) {
|
|
271
|
+
const observables = keys.map((key) => this.getByKey(storeName, key));
|
|
272
|
+
return new Observable((obs) => {
|
|
273
|
+
combineLatest(observables).subscribe((values) => {
|
|
274
|
+
obs.next(values);
|
|
275
|
+
obs.complete();
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Returns entry by id.
|
|
281
|
+
* @param storeName The name of the store to query
|
|
282
|
+
* @param id The entry id
|
|
283
|
+
*/
|
|
284
|
+
getByID(storeName, id) {
|
|
285
|
+
return new Observable((obs) => {
|
|
286
|
+
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
287
|
+
.then((db) => {
|
|
288
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
289
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error, obs.next));
|
|
290
|
+
const objectStore = transaction.objectStore(storeName);
|
|
291
|
+
const request = objectStore.get(id);
|
|
292
|
+
request.onsuccess = (event) => {
|
|
293
|
+
obs.next(event.target.result);
|
|
294
|
+
};
|
|
295
|
+
})
|
|
296
|
+
.catch((error) => obs.error(error));
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Returns entry by index.
|
|
301
|
+
* @param storeName The name of the store to query
|
|
302
|
+
* @param indexName The index name to filter
|
|
303
|
+
* @param key The entry key.
|
|
304
|
+
*/
|
|
305
|
+
getByIndex(storeName, indexName, key) {
|
|
306
|
+
return new Observable((obs) => {
|
|
307
|
+
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
308
|
+
.then((db) => {
|
|
309
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
310
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
|
|
311
|
+
const objectStore = transaction.objectStore(storeName);
|
|
312
|
+
const index = objectStore.index(indexName);
|
|
313
|
+
const request = index.get(key);
|
|
314
|
+
request.onsuccess = (event) => {
|
|
315
|
+
obs.next(event.target.result);
|
|
316
|
+
obs.complete();
|
|
317
|
+
};
|
|
318
|
+
})
|
|
319
|
+
.catch((reason) => obs.error(reason));
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Return all elements from one store
|
|
324
|
+
* @param storeName The name of the store to select the items
|
|
325
|
+
*/
|
|
326
|
+
getAll(storeName) {
|
|
327
|
+
return new Observable((obs) => {
|
|
328
|
+
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
329
|
+
.then((db) => {
|
|
330
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
331
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error, obs.next));
|
|
332
|
+
const objectStore = transaction.objectStore(storeName);
|
|
333
|
+
const request = objectStore.getAll();
|
|
334
|
+
request.onerror = (evt) => {
|
|
335
|
+
obs.error(evt);
|
|
336
|
+
};
|
|
337
|
+
request.onsuccess = ({ target: { result: ResultAll } }) => {
|
|
338
|
+
obs.next(ResultAll);
|
|
339
|
+
obs.complete();
|
|
340
|
+
};
|
|
341
|
+
})
|
|
342
|
+
.catch((error) => obs.error(error));
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Returns all items from the store after update.
|
|
347
|
+
* @param storeName The name of the store to update
|
|
348
|
+
* @param value The new value for the entry
|
|
349
|
+
* @param key The key of the entry to update if exists
|
|
350
|
+
*/
|
|
351
|
+
update(storeName, value, key) {
|
|
352
|
+
return new Observable((obs) => {
|
|
353
|
+
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
354
|
+
.then((db) => {
|
|
355
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
356
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
|
|
357
|
+
const objectStore = transaction.objectStore(storeName);
|
|
358
|
+
transaction.oncomplete = () => {
|
|
359
|
+
this.getAll(storeName)
|
|
360
|
+
.pipe(take(1))
|
|
361
|
+
.subscribe((newValues) => {
|
|
362
|
+
obs.next(newValues);
|
|
363
|
+
obs.complete();
|
|
364
|
+
});
|
|
365
|
+
};
|
|
366
|
+
key ? objectStore.put(value, key) : objectStore.put(value);
|
|
367
|
+
})
|
|
368
|
+
.catch((reason) => obs.error(reason));
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Returns the item you updated from the store after the update.
|
|
373
|
+
* @param storeName The name of the store to update
|
|
374
|
+
* @param value The new value for the entry
|
|
375
|
+
* @param key The key of the entry to update
|
|
376
|
+
*/
|
|
377
|
+
updateByKey(storeName, value, key) {
|
|
378
|
+
return new Observable((obs) => {
|
|
379
|
+
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
380
|
+
.then((db) => {
|
|
381
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
382
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
|
|
383
|
+
const objectStore = transaction.objectStore(storeName);
|
|
384
|
+
transaction.oncomplete = () => {
|
|
385
|
+
this.getByKey(storeName, key)
|
|
386
|
+
.pipe(take(1))
|
|
387
|
+
.subscribe((newValue) => {
|
|
388
|
+
obs.next(newValue);
|
|
389
|
+
obs.complete();
|
|
390
|
+
});
|
|
391
|
+
};
|
|
392
|
+
objectStore.put(value, key);
|
|
393
|
+
})
|
|
394
|
+
.catch((reason) => obs.error(reason));
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Returns all items from the store after delete.
|
|
399
|
+
* @param storeName The name of the store to have the entry deleted
|
|
400
|
+
* @param key The key of the entry to be deleted
|
|
401
|
+
*/
|
|
402
|
+
delete(storeName, key) {
|
|
403
|
+
return new Observable((obs) => {
|
|
404
|
+
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
405
|
+
.then((db) => {
|
|
406
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
407
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
|
|
408
|
+
const objectStore = transaction.objectStore(storeName);
|
|
409
|
+
objectStore.delete(key);
|
|
410
|
+
transaction.oncomplete = () => {
|
|
411
|
+
this.getAll(storeName)
|
|
412
|
+
.pipe(take(1))
|
|
413
|
+
.subscribe((newValues) => {
|
|
414
|
+
obs.next(newValues);
|
|
415
|
+
obs.complete();
|
|
416
|
+
});
|
|
417
|
+
};
|
|
418
|
+
})
|
|
419
|
+
.catch((reason) => obs.error(reason));
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Returns true from the store after a successful delete.
|
|
424
|
+
* @param storeName The name of the store to have the entry deleted
|
|
425
|
+
* @param key The key of the entry to be deleted
|
|
426
|
+
*/
|
|
427
|
+
deleteByKey(storeName, key) {
|
|
428
|
+
return new Observable((obs) => {
|
|
429
|
+
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
430
|
+
.then((db) => {
|
|
431
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
432
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
|
|
433
|
+
const objectStore = transaction.objectStore(storeName);
|
|
434
|
+
transaction.oncomplete = () => {
|
|
435
|
+
obs.next(true);
|
|
436
|
+
obs.complete();
|
|
437
|
+
};
|
|
438
|
+
objectStore.delete(key);
|
|
439
|
+
})
|
|
440
|
+
.catch((reason) => obs.error(reason));
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Returns true if successfully delete all entries from the store.
|
|
445
|
+
* @param storeName The name of the store to have the entries deleted
|
|
446
|
+
*/
|
|
447
|
+
clear(storeName) {
|
|
448
|
+
return new Observable((obs) => {
|
|
449
|
+
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
450
|
+
.then((db) => {
|
|
451
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
452
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
|
|
453
|
+
const objectStore = transaction.objectStore(storeName);
|
|
454
|
+
objectStore.clear();
|
|
455
|
+
transaction.oncomplete = () => {
|
|
456
|
+
obs.next(true);
|
|
457
|
+
obs.complete();
|
|
458
|
+
};
|
|
459
|
+
})
|
|
460
|
+
.catch((reason) => obs.error(reason));
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Returns true if successfully delete the DB.
|
|
465
|
+
*/
|
|
466
|
+
deleteDatabase() {
|
|
467
|
+
return new Observable((obs) => {
|
|
468
|
+
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
469
|
+
.then(async (db) => {
|
|
470
|
+
await db.close();
|
|
471
|
+
const deleteDBRequest = this.indexedDB.deleteDatabase(this.dbConfig.name);
|
|
472
|
+
deleteDBRequest.onsuccess = () => {
|
|
473
|
+
obs.next(true);
|
|
474
|
+
obs.complete();
|
|
475
|
+
};
|
|
476
|
+
deleteDBRequest.onerror = (error) => obs.error(error);
|
|
477
|
+
deleteDBRequest.onblocked = () => {
|
|
478
|
+
throw new Error(`Unable to delete database because it's blocked`);
|
|
479
|
+
};
|
|
480
|
+
})
|
|
481
|
+
.catch((error) => obs.error(error));
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* Returns the open cursor event
|
|
486
|
+
* @param storeName The name of the store to have the entries deleted
|
|
487
|
+
* @param keyRange The key range which the cursor should be open on
|
|
488
|
+
*/
|
|
489
|
+
openCursor(storeName, keyRange) {
|
|
490
|
+
return new Observable((obs) => {
|
|
491
|
+
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
492
|
+
.then((db) => {
|
|
493
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
494
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
|
|
495
|
+
const objectStore = transaction.objectStore(storeName);
|
|
496
|
+
const request = keyRange === undefined ? objectStore.openCursor() : objectStore.openCursor(keyRange);
|
|
497
|
+
request.onsuccess = (event) => {
|
|
498
|
+
obs.next(event);
|
|
499
|
+
obs.complete();
|
|
500
|
+
};
|
|
501
|
+
})
|
|
502
|
+
.catch((reason) => obs.error(reason));
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Open a cursor by index filter.
|
|
507
|
+
* @param storeName The name of the store to query.
|
|
508
|
+
* @param indexName The index name to filter.
|
|
509
|
+
* @param keyRange The range value and criteria to apply on the index.
|
|
510
|
+
*/
|
|
511
|
+
openCursorByIndex(storeName, indexName, keyRange, mode = DBMode.readonly) {
|
|
512
|
+
const obs = new Subject();
|
|
513
|
+
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
514
|
+
.then((db) => {
|
|
515
|
+
validateBeforeTransaction(db, storeName, (reason) => {
|
|
516
|
+
obs.error(reason);
|
|
517
|
+
});
|
|
518
|
+
const transaction = createTransaction(db, optionsGenerator(mode, storeName, (reason) => {
|
|
519
|
+
obs.error(reason);
|
|
520
|
+
}, () => {
|
|
521
|
+
obs.next();
|
|
522
|
+
}));
|
|
523
|
+
const objectStore = transaction.objectStore(storeName);
|
|
524
|
+
const index = objectStore.index(indexName);
|
|
525
|
+
const request = index.openCursor(keyRange);
|
|
526
|
+
request.onsuccess = (event) => {
|
|
527
|
+
obs.next(event);
|
|
528
|
+
};
|
|
529
|
+
})
|
|
530
|
+
.catch((reason) => obs.error(reason));
|
|
531
|
+
return obs;
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Returns all items by an index.
|
|
535
|
+
* @param storeName The name of the store to query
|
|
536
|
+
* @param indexName The index name to filter
|
|
537
|
+
* @param keyRange The range value and criteria to apply on the index.
|
|
538
|
+
*/
|
|
539
|
+
getAllByIndex(storeName, indexName, keyRange) {
|
|
540
|
+
const data = [];
|
|
541
|
+
return new Observable((obs) => {
|
|
542
|
+
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
543
|
+
.then((db) => {
|
|
544
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
545
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
|
|
546
|
+
const objectStore = transaction.objectStore(storeName);
|
|
547
|
+
const index = objectStore.index(indexName);
|
|
548
|
+
const request = index.openCursor(keyRange);
|
|
549
|
+
request.onsuccess = (event) => {
|
|
550
|
+
const cursor = event.target.result;
|
|
551
|
+
if (cursor) {
|
|
552
|
+
data.push(cursor.value);
|
|
553
|
+
cursor.continue();
|
|
554
|
+
}
|
|
555
|
+
else {
|
|
556
|
+
obs.next(data);
|
|
557
|
+
obs.complete();
|
|
558
|
+
}
|
|
559
|
+
};
|
|
560
|
+
})
|
|
561
|
+
.catch((reason) => obs.error(reason));
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* Returns all primary keys by an index.
|
|
566
|
+
* @param storeName The name of the store to query
|
|
567
|
+
* @param indexName The index name to filter
|
|
568
|
+
* @param keyRange The range value and criteria to apply on the index.
|
|
569
|
+
*/
|
|
570
|
+
getAllKeysByIndex(storeName, indexName, keyRange) {
|
|
571
|
+
const data = [];
|
|
572
|
+
return new Observable((obs) => {
|
|
573
|
+
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
574
|
+
.then((db) => {
|
|
575
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
576
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
|
|
577
|
+
const objectStore = transaction.objectStore(storeName);
|
|
578
|
+
const index = objectStore.index(indexName);
|
|
579
|
+
const request = index.openKeyCursor(keyRange);
|
|
580
|
+
request.onsuccess = (event) => {
|
|
581
|
+
const cursor = event.target.result;
|
|
582
|
+
if (cursor) {
|
|
583
|
+
data.push({ primaryKey: cursor.primaryKey, key: cursor.key });
|
|
584
|
+
cursor.continue();
|
|
585
|
+
}
|
|
586
|
+
else {
|
|
587
|
+
obs.next(data);
|
|
588
|
+
obs.complete();
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
})
|
|
592
|
+
.catch((reason) => obs.error(reason));
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Returns the number of rows in a store.
|
|
597
|
+
* @param storeName The name of the store to query
|
|
598
|
+
* @param keyRange The range value and criteria to apply.
|
|
599
|
+
*/
|
|
600
|
+
count(storeName, keyRange) {
|
|
601
|
+
return new Observable((obs) => {
|
|
602
|
+
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
603
|
+
.then((db) => {
|
|
604
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
605
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
|
|
606
|
+
const objectStore = transaction.objectStore(storeName);
|
|
607
|
+
const request = objectStore.count(keyRange);
|
|
608
|
+
request.onerror = (e) => obs.error(e);
|
|
609
|
+
request.onsuccess = (e) => {
|
|
610
|
+
obs.next(e.target.result);
|
|
611
|
+
obs.complete();
|
|
612
|
+
};
|
|
613
|
+
})
|
|
614
|
+
.catch((reason) => obs.error(reason));
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Delete the store by name.
|
|
619
|
+
* @param storeName The name of the store to query
|
|
620
|
+
*/
|
|
621
|
+
deleteObjectStore(storeName) {
|
|
622
|
+
return DeleteObjectStore(this.dbConfig.name, ++this.dbConfig.version, storeName);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
NgxIndexedDBService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: NgxIndexedDBService, deps: [{ token: CONFIG_TOKEN }, { token: PLATFORM_ID }], target: i0.ɵɵFactoryTarget.Injectable });
|
|
626
|
+
NgxIndexedDBService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: NgxIndexedDBService });
|
|
627
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: NgxIndexedDBService, decorators: [{
|
|
628
|
+
type: Injectable
|
|
629
|
+
}], ctorParameters: function () { return [{ type: undefined, decorators: [{
|
|
630
|
+
type: Inject,
|
|
631
|
+
args: [CONFIG_TOKEN]
|
|
632
|
+
}] }, { type: undefined, decorators: [{
|
|
633
|
+
type: Inject,
|
|
634
|
+
args: [PLATFORM_ID]
|
|
635
|
+
}] }]; } });
|
|
636
|
+
|
|
637
|
+
class NgxIndexedDBModule {
|
|
638
|
+
static forRoot(dbConfig) {
|
|
639
|
+
return {
|
|
640
|
+
ngModule: NgxIndexedDBModule,
|
|
641
|
+
providers: [NgxIndexedDBService, { provide: CONFIG_TOKEN, useValue: dbConfig }]
|
|
642
|
+
};
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
NgxIndexedDBModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: NgxIndexedDBModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
|
|
646
|
+
NgxIndexedDBModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: NgxIndexedDBModule, imports: [CommonModule] });
|
|
647
|
+
NgxIndexedDBModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: NgxIndexedDBModule, imports: [[CommonModule]] });
|
|
648
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.2.0", ngImport: i0, type: NgxIndexedDBModule, decorators: [{
|
|
649
|
+
type: NgModule,
|
|
650
|
+
args: [{
|
|
651
|
+
declarations: [],
|
|
652
|
+
imports: [CommonModule]
|
|
653
|
+
}]
|
|
654
|
+
}] });
|
|
655
|
+
|
|
656
|
+
/**
|
|
657
|
+
* Generated bundle index. Do not edit.
|
|
658
|
+
*/
|
|
659
|
+
|
|
660
|
+
export { CONFIG_TOKEN, DBMode, NgxIndexedDBModule, NgxIndexedDBService };
|
|
661
|
+
//# sourceMappingURL=ngx-indexed-db.mjs.map
|