ngx-indexed-db 9.4.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/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ The MIT License (MIT)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,410 @@
1
+ # ngx-indexed-db
2
+
3
+ [![Known Vulnerabilities](https://snyk.io/test/github/assuncaocharles/ngx-indexed-db/badge.svg)](https://snyk.io/test/github/assuncaocharles/ngx-indexed-db) [![CodeFactor](https://www.codefactor.io/repository/github/assuncaocharles/ngx-indexed-db/badge/master)](https://www.codefactor.io/repository/github/assuncaocharles/ngx-indexed-db/overview/master) [![Build Status](https://travis-ci.com/assuncaocharles/ngx-indexed-db.svg?branch=master)](https://travis-ci.com/assuncaocharles/ngx-indexed-db) ![CI](https://github.com/assuncaocharles/ngx-indexed-db/workflows/CI/badge.svg)
4
+
5
+ `ngx-indexed-db` is a service that wraps IndexedDB database in an Angular service combined with the power of observables.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ $ npm install ngx-indexed-db
11
+ ```
12
+
13
+ OR
14
+
15
+ ```bash
16
+ $ yarn add ngx-indexed-db
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Import the `NgxIndexedDBModule` and initiate it:
22
+
23
+ ```js
24
+ import { NgxIndexedDBModule } from 'ngx-indexed-db';
25
+
26
+ const dbConfig: DBConfig = {
27
+ name: 'MyDb',
28
+ version: 1,
29
+ objectStoresMeta: [{
30
+ store: 'people',
31
+ storeConfig: { keyPath: 'id', autoIncrement: true },
32
+ storeSchema: [
33
+ { name: 'name', keypath: 'name', options: { unique: false } },
34
+ { name: 'email', keypath: 'email', options: { unique: false } }
35
+ ]
36
+ }]
37
+ };
38
+
39
+ @NgModule({
40
+ ...
41
+ imports: [
42
+ ...
43
+ NgxIndexedDBModule.forRoot(dbConfig)
44
+ ],
45
+ ...
46
+ })
47
+ ```
48
+
49
+ ### Migrations
50
+
51
+ ```js
52
+ import { NgxIndexedDBModule, DBConfig } from 'ngx-indexed-db';
53
+
54
+ // Ahead of time compiles requires an exported function for factories
55
+ export function migrationFactory() {
56
+ // The animal table was added with version 2 but none of the existing tables or data needed
57
+ // to be modified so a migrator for that version is not included.
58
+ return {
59
+ 1: (db, transaction) => {
60
+ const store = transaction.objectStore('people');
61
+ store.createIndex('country', 'country', { unique: false });
62
+ },
63
+ 3: (db, transaction) => {
64
+ const store = transaction.objectStore('people');
65
+ store.createIndex('age', 'age', { unique: false });
66
+ }
67
+ };
68
+ }
69
+
70
+ const dbConfig: DBConfig = {
71
+ name: 'MyDb',
72
+ version: 3,
73
+ objectStoresMeta: [{
74
+ store: 'people',
75
+ storeConfig: { keyPath: 'id', autoIncrement: true },
76
+ storeSchema: [
77
+ { name: 'name', keypath: 'name', options: { unique: false } },
78
+ { name: 'email', keypath: 'email', options: { unique: false } }
79
+ ]
80
+ }, {
81
+ // animals added in version 2
82
+ store: 'animals',
83
+ storeConfig: { keyPath: 'id', autoIncrement: true },
84
+ storeSchema: [
85
+ { name: 'name', keypath: 'name', options: { unique: true } },
86
+ ]
87
+ }],
88
+ // provide the migration factory to the DBConfig
89
+ migrationFactory
90
+ };
91
+
92
+ @NgModule({
93
+ ...
94
+ imports: [
95
+ ...
96
+ NgxIndexedDBModule.forRoot(dbConfig)
97
+ ],
98
+ ...
99
+ })
100
+ ```
101
+
102
+ ### NgxIndexedDB service
103
+
104
+ Import and inject the service:
105
+
106
+ ```js
107
+ import { NgxIndexedDBService } from 'ngx-indexed-db';
108
+
109
+ ...
110
+ export class AppComponent {
111
+ constructor(private dbService: NgxIndexedDBService){
112
+ }
113
+ }
114
+ ```
115
+
116
+ ### API
117
+
118
+ We cover several common methods used to work with the IndexedDB
119
+
120
+ ### add<T>(storeName: string, value: T, key?: any): Observable<T>
121
+
122
+ Adds new entry in the store and returns item added
123
+
124
+ - @param storeName The name of the store to add the item
125
+ - @param value The entry to be added
126
+ - @param key The optional key for the entry
127
+
128
+ It publishes in the observable the key value of the entry
129
+
130
+ ```js
131
+ this.dbService
132
+ .add('people', {
133
+ name: `Bruce Wayne`,
134
+ email: `bruce@wayne.com`,
135
+ })
136
+ .subscribe((key) => {
137
+ console.log('key: ', key);
138
+ });
139
+ ```
140
+
141
+ _In the previous example I'm using undefined as the key because the key is configured in the objectStore as auto-generated._
142
+
143
+ ### bulkAdd<T>(storeName: string, values: T & { key?: any }[]): Observable<number[]>
144
+
145
+ Adds new entries in the store and returns its key
146
+
147
+ - @param storeName The name of the store to add the item
148
+ - @param values The entries to be added containing optional key attribute
149
+
150
+ ```typescript
151
+ this.dbService
152
+ .bulkAdd('people', [
153
+ {
154
+ name: `charles number ${Math.random() * 10}`,
155
+ email: `email number ${Math.random() * 10}`,
156
+ },
157
+ {
158
+ name: `charles number ${Math.random() * 10}`,
159
+ email: `email number ${Math.random() * 10}`,
160
+ },
161
+ ])
162
+ .subscribe((result) => {
163
+ console.log('result: ', result);
164
+ });
165
+ ```
166
+
167
+
168
+ ### bulkDelete(storeName: string, keys: Key[]): Observable<number[]>
169
+
170
+ Delete multiple items in the store
171
+
172
+ - @param storeName The name of the store to delete the items
173
+ - @param keys The entries keys to be deleted
174
+
175
+ ```typescript
176
+ this.dbService.bulkDelete('people', [5, 6]).subscribe((result) => {
177
+ console.log('result: ', result);
178
+ });
179
+ ```
180
+
181
+ ### bulkGet<T>(storeName: string, keys: Array<IDBValidKey>): Observable<T[]>
182
+
183
+ Retrieve multiple entries in the store
184
+
185
+ - @param storeName The name of the store to retrieve the items
186
+ - @param keys The ids entries to be retrieve
187
+
188
+ ```typescript
189
+ this.dbService.bulkGet('people', [1, 3, 5]).subscribe((result) => {
190
+ console.log('results: ', result);
191
+ });
192
+ ```
193
+
194
+ ### update<T>(storeName: string, value: T, key?: any): Observable<T[]>
195
+
196
+ Updates the given value in the objectStore and returns all items from the store after update..
197
+
198
+ - @param storeName The name of the store to update
199
+ - @param value The new value for the entry
200
+ - @param key The key of the entry to update if exists
201
+
202
+ ```js
203
+ this.dbService
204
+ .update('people', {
205
+ id: 1,
206
+ email: 'luke@skywalker.com',
207
+ name: 'Luke Skywalker',
208
+ })
209
+ .subscribe((storeData) => {
210
+ console.log('storeData: ', storeData);
211
+ });
212
+ ```
213
+
214
+ ### updateByKey<T>(storeName: string, value: T, key: IDBValidKey): Observable<T>
215
+
216
+ Updates the given value in the objectStore and returns the item from the store after update..
217
+
218
+ - @param storeName The name of the store to update
219
+ - @param value The new value for the entry
220
+ - @param key The key of the entry to update
221
+
222
+ ```js
223
+ this.dbService
224
+ .updateByKey('people', {
225
+ id: 1,
226
+ email: 'luke@skywalker.com',
227
+ name: 'Luke Skywalker',
228
+ })
229
+ .subscribe((item) => {
230
+ console.log('item: ', item);
231
+ });
232
+ ```
233
+
234
+ ### getByKey<T>(storeName: string, key: IDBValidKey): Observable<T>
235
+
236
+ Returns entry by key.
237
+
238
+ - @param storeName The name of the store to query
239
+ - @param key The entry key
240
+
241
+ ```js
242
+ this.dbService.getByKey('people', 1).subscribe((people) => {
243
+ console.log(people);
244
+ });
245
+ ```
246
+
247
+ ### getAll<T>(storeName: string): Observable<T[]>
248
+
249
+ Return all elements from one store
250
+
251
+ - @param storeName The name of the store to select the items
252
+
253
+ ```js
254
+ this.dbService.getAll('people').subscribe((peoples) => {
255
+ console.log(peoples);
256
+ });
257
+ ```
258
+
259
+ ### getByIndex<T>(storeName: string, indexName: string, key: IDBValidKey): Observable<T>
260
+
261
+ Returns entry by index.
262
+
263
+ - @param storeName The name of the store to query
264
+ - @param indexName The index name to filter
265
+ - @param key The entry key.
266
+
267
+ ```js
268
+ this.dbService.getByIndex('people', 'name', 'Dave').subscribe((people) => {
269
+ console.log(people);
270
+ });
271
+ ```
272
+
273
+ ### createObjectStore(storeSchema: ObjectStoreMeta, migrationFactory?: () => { [key: number]: (db: IDBDatabase, transaction: IDBTransaction) => void }): void
274
+
275
+ Allows to crate a new object store ad-hoc
276
+
277
+ - @param storeName The name of the store to be created
278
+ - @param migrationFactory The migration factory if exists
279
+
280
+ ```js
281
+ const storeSchema: ObjectStoreMeta = {
282
+ store: 'people',
283
+ storeConfig: { keyPath: 'id', autoIncrement: true },
284
+ storeSchema: [
285
+ { name: 'name', keypath: 'name', options: { unique: false } },
286
+ { name: 'email', keypath: 'email', options: { unique: false } },
287
+ ],
288
+ };
289
+
290
+ this.dbService.createObjectStore(storeSchema);
291
+ ```
292
+
293
+ ### count(storeName: string, keyRange?: IDBValidKey | IDBKeyRange): Observable<number>
294
+
295
+ Returns the number of rows in a store.
296
+
297
+ - @param storeName The name of the store to query
298
+ - @param keyRange The range value and criteria to apply.
299
+
300
+ ```js
301
+ this.dbService.count('people').subscribe((peopleCount) => {
302
+ console.log(peopleCount);
303
+ });
304
+ ```
305
+
306
+ ### delete<T>(storeName: string, key: Key): Observable<T[]>
307
+
308
+ Returns all items from the store after delete.
309
+
310
+ - @param storeName The name of the store to have the entry deleted
311
+ - @param key The key of the entry to be deleted
312
+
313
+ ```js
314
+ this.dbService.delete('people', 3).subscribe((allPeople) => {
315
+ console.log('all people:', allPeople);
316
+ });
317
+ ```
318
+
319
+ ### deleteByKey(storeName: string, key: Key): Observable<boolean>
320
+
321
+ Returns true if the delete completes successfully.
322
+
323
+ - @param storeName The name of the store to have the entry deleted
324
+ - @param key The key of the entry to be deleted
325
+
326
+ ```js
327
+ this.dbService.deleteByKey('people', 3).subscribe((status) => {
328
+ console.log('Deleted?:', status);
329
+ });
330
+ ```
331
+
332
+ ### openCursor(storeName: string, keyRange?: IDBKeyRange): Observable<Event>
333
+
334
+ Returns the open cursor event
335
+
336
+ - @param storeName The name of the store to have the entries deleted
337
+ - @param keyRange The key range which the cursor should be open on
338
+
339
+ ```js
340
+ this.dbService.openCursor('people', IDBKeyRange.bound("A", "F")).subscribe((evt) => {
341
+ var cursor = (evt.target as IDBOpenDBRequest).result;
342
+ if(cursor) {
343
+ console.log(cursor.value);
344
+ cursor.continue();
345
+ } else {
346
+ console.log('Entries all displayed.');
347
+ }
348
+ });
349
+ ```
350
+
351
+ ### openCursorByIndex(storeName: string, indexName: string, keyRange: IDBKeyRange, mode?: DBMode): Observable<Event>
352
+
353
+ Open a cursor by index filter.
354
+
355
+ - @param storeName The name of the store to query.
356
+ - @param indexName The index name to filter.
357
+ - @param keyRange The range value and criteria to apply on the index.
358
+ - @param mode DB Mode to work with, default to `readonly`
359
+
360
+ ```js
361
+ this.dbService.openCursorByIndex('people', 'name', IDBKeyRange.only('john')).subscribe((evt) => {
362
+ var cursor = (evt.target as IDBOpenDBRequest).result;
363
+ if(cursor) {
364
+ console.log(cursor.value);
365
+ cursor.continue();
366
+ } else {
367
+ console.log('Entries all displayed.');
368
+ }
369
+ });
370
+ ```
371
+
372
+ ### getAllByIndex<T>(storeName: string, indexName: string, keyRange: IDBKeyRange): Observable<T[]>
373
+
374
+ Returns all items by an index.
375
+
376
+ - @param storeName The name of the store to query
377
+ - @param indexName The index name to filter
378
+ - @param keyRange The range value and criteria to apply on the index.
379
+
380
+ ```js
381
+ this.dbService.getAllByIndex('people', 'name', IDBKeyRange.only('john')).subscribe((allPeopleByIndex) => {
382
+ console.log('All: ', allPeopleByIndex);
383
+ });
384
+ ```
385
+
386
+ ### clear(storeName: string): Observable<boolean>
387
+
388
+ Returns true if successfully delete all entries from the store.
389
+
390
+ - @param storeName The name of the store to have the entries deleted
391
+
392
+ ```js
393
+ this.dbService.clear('people').subscribe((successDeleted) => {
394
+ console.log('success? ', successDeleted);
395
+ });
396
+ ```
397
+
398
+ ### deleteDatabase(): Observable<boolean>
399
+
400
+ Returns true if successfully delete the DB.
401
+
402
+ ```js
403
+ this.dbService.deleteDatabase().subscribe((deleted) => {
404
+ console.log('Database deleted successfully: ', deleted);
405
+ });
406
+ ```
407
+
408
+ ## License
409
+
410
+ Released under the terms of the [MIT License](LICENSE).