ngx-indexed-db 23.1.0 → 23.1.1
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 +68 -492
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,529 +1,105 @@
|
|
|
1
1
|
# ngx-indexed-db
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/ngx-indexed-db) [](https://assuncaocharles.github.io/ngx-indexed-db/) [](https://github.com/assuncaocharles/ngx-indexed-db/actions/workflows/buid.yml) [](https://snyk.io/test/github/assuncaocharles/ngx-indexed-db) [](https://www.codefactor.io/repository/github/assuncaocharles/ngx-indexed-db/overview/master)
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
IndexedDB for Angular, without the plumbing. Describe your database once and get a typed, Observable-based service that opens connections, creates stores and indexes, runs migrations and works with server-side rendering.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## 📖 Documentation
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
```bash
|
|
12
|
-
$ npm install ngx-indexed-db
|
|
13
|
-
```
|
|
9
|
+
**https://assuncaocharles.github.io/ngx-indexed-db/**
|
|
14
10
|
|
|
15
|
-
|
|
11
|
+
Guides, the full API reference (generated from the source) and a live playground that runs the library in your browser:
|
|
16
12
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
- [Getting started](https://assuncaocharles.github.io/ngx-indexed-db/guide/getting-started)
|
|
14
|
+
- [Configuration](https://assuncaocharles.github.io/ngx-indexed-db/guide/configuration): stores, indexes, waiting until the database is ready
|
|
15
|
+
- [Querying data](https://assuncaocharles.github.io/ngx-indexed-db/guide/querying): keys, indexes, key ranges, cursors
|
|
16
|
+
- [Migrations](https://assuncaocharles.github.io/ngx-indexed-db/guide/migrations)
|
|
17
|
+
- [Multiple databases](https://assuncaocharles.github.io/ngx-indexed-db/guide/multiple-databases)
|
|
18
|
+
- [SSR & testing](https://assuncaocharles.github.io/ngx-indexed-db/guide/ssr)
|
|
19
|
+
- [API reference](https://assuncaocharles.github.io/ngx-indexed-db/api)
|
|
20
|
+
- [Playground](https://assuncaocharles.github.io/ngx-indexed-db/playground)
|
|
20
21
|
|
|
21
|
-
|
|
22
|
+
## Features
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
- **Observable API**: every operation returns a cold `Observable`; writes emit once their transaction has committed.
|
|
25
|
+
- **Typed**: generic methods carry your record types through reads, writes, cursors and index keys.
|
|
26
|
+
- **Schema as config**: stores and indexes are created from your config, including indexes added to existing stores in a new version.
|
|
27
|
+
- **Migrations**: version your schema and run only the upgrades each user needs.
|
|
28
|
+
- **SSR safe**: renders on the server without IndexedDB, with an optional server-side implementation.
|
|
29
|
+
- **Standalone or NgModule**: `provideIndexedDb()` or `NgxIndexedDBModule.forRoot()`, with one or several databases.
|
|
26
30
|
|
|
27
|
-
##
|
|
28
|
-
|
|
29
|
-
### With Module
|
|
30
|
-
Import the `NgxIndexedDBModule` and initiate it:
|
|
31
|
-
|
|
32
|
-
```js
|
|
33
|
-
import { NgxIndexedDBModule } from 'ngx-indexed-db';
|
|
34
|
-
|
|
35
|
-
const dbConfig: DBConfig = {
|
|
36
|
-
name: 'MyDb',
|
|
37
|
-
version: 1,
|
|
38
|
-
objectStoresMeta: [{
|
|
39
|
-
store: 'people',
|
|
40
|
-
storeConfig: { keyPath: 'id', autoIncrement: true },
|
|
41
|
-
storeSchema: [
|
|
42
|
-
{ name: 'name', keypath: 'name', options: { unique: false } },
|
|
43
|
-
{ name: 'email', keypath: 'email', options: { unique: false } }
|
|
44
|
-
]
|
|
45
|
-
}]
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
@NgModule({
|
|
49
|
-
...
|
|
50
|
-
imports: [
|
|
51
|
-
...
|
|
52
|
-
NgxIndexedDBModule.forRoot(dbConfig)
|
|
53
|
-
],
|
|
54
|
-
...
|
|
55
|
-
})
|
|
56
|
-
```
|
|
57
|
-
### With Standalone API
|
|
58
|
-
|
|
59
|
-
Use `provideIndexedDb` and set it up:
|
|
60
|
-
|
|
61
|
-
```js
|
|
62
|
-
import { provideIndexedDb, DBConfig } from 'ngx-indexed-db';
|
|
63
|
-
|
|
64
|
-
const dbConfig: DBConfig = {
|
|
65
|
-
name: 'MyDb',
|
|
66
|
-
version: 1,
|
|
67
|
-
objectStoresMeta: [{
|
|
68
|
-
store: 'people',
|
|
69
|
-
storeConfig: { keyPath: 'id', autoIncrement: true },
|
|
70
|
-
storeSchema: [
|
|
71
|
-
{ name: 'name', keypath: 'name', options: { unique: false } },
|
|
72
|
-
{ name: 'email', keypath: 'email', options: { unique: false } }
|
|
73
|
-
]
|
|
74
|
-
}]
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
const appConfig: ApplicationConfig = {
|
|
78
|
-
providers: [...,provideIndexedDb(dbConfig),...]
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
OR
|
|
31
|
+
## Installation
|
|
82
32
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
providers:[
|
|
86
|
-
...
|
|
87
|
-
provideIndexedDb(dbConfig)
|
|
88
|
-
],
|
|
89
|
-
...
|
|
90
|
-
})
|
|
33
|
+
```bash
|
|
34
|
+
npm install ngx-indexed-db
|
|
91
35
|
```
|
|
92
|
-
### SSR
|
|
93
|
-
|
|
94
|
-
Starting from version 19.2.0, `ngx-indexed-db` fully supports **Server-Side Rendering (SSR)**. This enhancement prevents issues related to the absence of `window.indexedDB` in server environments.
|
|
95
36
|
|
|
96
|
-
|
|
37
|
+
| ngx-indexed-db | Angular |
|
|
38
|
+
| -------------- | ----------- |
|
|
39
|
+
| 23.x | 16.x – 22.x |
|
|
97
40
|
|
|
98
|
-
|
|
99
|
-
const SERVER_INDEXED_DB = new InjectionToken<IDBFactory>('Server Indexed Db');
|
|
100
|
-
```
|
|
41
|
+
## Quick start
|
|
101
42
|
|
|
102
|
-
|
|
43
|
+
Describe your database and register it:
|
|
103
44
|
|
|
104
|
-
```
|
|
105
|
-
import {
|
|
106
|
-
|
|
107
|
-
// Ahead of time compiles requires an exported function for factories
|
|
108
|
-
export function migrationFactory() {
|
|
109
|
-
// The animal table was added with version 2 but none of the existing tables or data needed
|
|
110
|
-
// to be modified so a migrator for that version is not included.
|
|
111
|
-
return {
|
|
112
|
-
1: (db, transaction) => {
|
|
113
|
-
const store = transaction.objectStore('people');
|
|
114
|
-
store.createIndex('country', 'country', { unique: false });
|
|
115
|
-
},
|
|
116
|
-
3: (db, transaction) => {
|
|
117
|
-
const store = transaction.objectStore('people');
|
|
118
|
-
store.createIndex('age', 'age', { unique: false });
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
}
|
|
45
|
+
```ts
|
|
46
|
+
import { ApplicationConfig } from '@angular/core';
|
|
47
|
+
import { DBConfig, provideIndexedDb } from 'ngx-indexed-db';
|
|
122
48
|
|
|
123
|
-
const dbConfig: DBConfig
|
|
49
|
+
const dbConfig: DBConfig = {
|
|
124
50
|
name: 'MyDb',
|
|
125
|
-
version:
|
|
126
|
-
objectStoresMeta: [
|
|
127
|
-
store: 'people',
|
|
128
|
-
storeConfig: { keyPath: 'id', autoIncrement: true },
|
|
129
|
-
storeSchema: [
|
|
130
|
-
{ name: 'name', keypath: 'name', options: { unique: false } },
|
|
131
|
-
{ name: 'email', keypath: 'email', options: { unique: false } }
|
|
132
|
-
]
|
|
133
|
-
}, {
|
|
134
|
-
// animals added in version 2
|
|
135
|
-
store: 'animals',
|
|
136
|
-
storeConfig: { keyPath: 'id', autoIncrement: true },
|
|
137
|
-
storeSchema: [
|
|
138
|
-
{ name: 'name', keypath: 'name', options: { unique: true } },
|
|
139
|
-
]
|
|
140
|
-
}],
|
|
141
|
-
// provide the migration factory to the DBConfig
|
|
142
|
-
migrationFactory
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
@NgModule({
|
|
146
|
-
...
|
|
147
|
-
imports: [
|
|
148
|
-
...
|
|
149
|
-
NgxIndexedDBModule.forRoot(dbConfig)
|
|
150
|
-
],
|
|
151
|
-
...
|
|
152
|
-
})
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
### NgxIndexedDB service
|
|
156
|
-
|
|
157
|
-
Import and inject the service:
|
|
158
|
-
|
|
159
|
-
```js
|
|
160
|
-
import { NgxIndexedDBService } from 'ngx-indexed-db';
|
|
161
|
-
|
|
162
|
-
...
|
|
163
|
-
export class AppComponent {
|
|
164
|
-
#dbService = inject(NgxIndexedDBService);
|
|
165
|
-
}
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
### API
|
|
169
|
-
|
|
170
|
-
We cover several common methods used to work with the IndexedDB
|
|
171
|
-
|
|
172
|
-
### add<T>(storeName: string, value: T, key?: any): Observable<T & {id: any}>
|
|
173
|
-
|
|
174
|
-
Adds new entry in the store and returns item added
|
|
175
|
-
|
|
176
|
-
- @param storeName The name of the store to add the item
|
|
177
|
-
- @param value The entry to be added
|
|
178
|
-
- @param key The optional key for the entry
|
|
179
|
-
|
|
180
|
-
It publishes in the observable the key value of the entry
|
|
181
|
-
|
|
182
|
-
```js
|
|
183
|
-
this.dbService
|
|
184
|
-
.add('people', {
|
|
185
|
-
name: `Bruce Wayne`,
|
|
186
|
-
email: `bruce@wayne.com`,
|
|
187
|
-
})
|
|
188
|
-
.subscribe((key) => {
|
|
189
|
-
console.log('key: ', key);
|
|
190
|
-
});
|
|
191
|
-
```
|
|
192
|
-
|
|
193
|
-
_In the previous example I'm using undefined as the key because the key is configured in the objectStore as auto-generated._
|
|
194
|
-
|
|
195
|
-
### bulkAdd<T>(storeName: string, values: Array<T & { key?: any }>): Observable<number[]>
|
|
196
|
-
|
|
197
|
-
Adds new entries in the store and returns its key
|
|
198
|
-
|
|
199
|
-
- @param storeName The name of the store to add the item
|
|
200
|
-
- @param values The entries to be added containing optional key attribute
|
|
201
|
-
|
|
202
|
-
```typescript
|
|
203
|
-
this.dbService
|
|
204
|
-
.bulkAdd('people', [
|
|
205
|
-
{
|
|
206
|
-
name: `charles number ${Math.random() * 10}`,
|
|
207
|
-
email: `email number ${Math.random() * 10}`,
|
|
208
|
-
},
|
|
51
|
+
version: 1,
|
|
52
|
+
objectStoresMeta: [
|
|
209
53
|
{
|
|
210
|
-
|
|
211
|
-
|
|
54
|
+
store: 'people',
|
|
55
|
+
storeConfig: { keyPath: 'id', autoIncrement: true },
|
|
56
|
+
storeSchema: [
|
|
57
|
+
{ name: 'name', keypath: 'name', options: { unique: false } },
|
|
58
|
+
{ name: 'email', keypath: 'email', options: { unique: true } },
|
|
59
|
+
],
|
|
212
60
|
},
|
|
213
|
-
])
|
|
214
|
-
.subscribe((result) => {
|
|
215
|
-
console.log('result: ', result);
|
|
216
|
-
});
|
|
217
|
-
```
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
### bulkDelete(storeName: string, keys: Key[]): Observable<number[]>
|
|
221
|
-
|
|
222
|
-
Delete multiple items in the store
|
|
223
|
-
|
|
224
|
-
- @param storeName The name of the store to delete the items
|
|
225
|
-
- @param keys The entries keys to be deleted
|
|
226
|
-
|
|
227
|
-
```typescript
|
|
228
|
-
this.dbService.bulkDelete('people', [5, 6]).subscribe((result) => {
|
|
229
|
-
console.log('result: ', result);
|
|
230
|
-
});
|
|
231
|
-
```
|
|
232
|
-
|
|
233
|
-
### bulkGet<T>(storeName: string, keys: Array<IDBValidKey>): Observable<T[]>
|
|
234
|
-
|
|
235
|
-
Retrieve multiple entries in the store
|
|
236
|
-
|
|
237
|
-
- @param storeName The name of the store to retrieve the items
|
|
238
|
-
- @param keys The ids entries to be retrieve
|
|
239
|
-
|
|
240
|
-
```typescript
|
|
241
|
-
this.dbService.bulkGet('people', [1, 3, 5]).subscribe((result) => {
|
|
242
|
-
console.log('results: ', result);
|
|
243
|
-
});
|
|
244
|
-
```
|
|
245
|
-
|
|
246
|
-
### bulkPut<T>(storeName: string, values: Array<T & { key?: any }>): Observable<number[]>
|
|
247
|
-
|
|
248
|
-
Adds or updates a record in store with the given value and key. Return all items present in the store
|
|
249
|
-
|
|
250
|
-
- @param storeName The name of the store to update
|
|
251
|
-
- @param items The values to update in the DB
|
|
252
|
-
|
|
253
|
-
@Return The return value is an Observable with the primary key of the object that was last in given array
|
|
254
|
-
|
|
255
|
-
@error If the call to bulkPut fails the transaction will be aborted and previously inserted entities will be deleted
|
|
256
|
-
|
|
257
|
-
```typescript
|
|
258
|
-
this.dbService.bulkPut('people', people).subscribe((result) => {
|
|
259
|
-
console.log('result: ', result);
|
|
260
|
-
});
|
|
261
|
-
```
|
|
262
|
-
|
|
263
|
-
### update<T>(storeName: string, value: T): Observable<T[]>
|
|
264
|
-
|
|
265
|
-
Adds or updates a record in store with the given value and key. Return item updated
|
|
266
|
-
|
|
267
|
-
- @param storeName The name of the store to update
|
|
268
|
-
- @param value The new value for the entry
|
|
269
|
-
|
|
270
|
-
```js
|
|
271
|
-
this.dbService
|
|
272
|
-
.update('people', {
|
|
273
|
-
id: 1,
|
|
274
|
-
email: 'luke@skywalker.com',
|
|
275
|
-
name: 'Luke Skywalker',
|
|
276
|
-
})
|
|
277
|
-
.subscribe((storeData) => {
|
|
278
|
-
console.log('storeData: ', storeData);
|
|
279
|
-
});
|
|
280
|
-
```
|
|
281
|
-
|
|
282
|
-
### getByKey<T>(storeName: string, key: IDBValidKey): Observable<T>
|
|
283
|
-
|
|
284
|
-
Returns entry by key.
|
|
285
|
-
|
|
286
|
-
- @param storeName The name of the store to query
|
|
287
|
-
- @param key The entry key
|
|
288
|
-
|
|
289
|
-
```js
|
|
290
|
-
this.dbService.getByKey('people', 1).subscribe((people) => {
|
|
291
|
-
console.log(people);
|
|
292
|
-
});
|
|
293
|
-
```
|
|
294
|
-
|
|
295
|
-
### getAll<T>(storeName: string): Observable<T[]>
|
|
296
|
-
|
|
297
|
-
Return all elements from one store
|
|
298
|
-
|
|
299
|
-
- @param storeName The name of the store to select the items
|
|
300
|
-
|
|
301
|
-
```js
|
|
302
|
-
this.dbService.getAll('people').subscribe((peoples) => {
|
|
303
|
-
console.log(peoples);
|
|
304
|
-
});
|
|
305
|
-
```
|
|
306
|
-
|
|
307
|
-
### getByIndex<T>(storeName: string, indexName: string, key: IDBValidKey): Observable<T>
|
|
308
|
-
|
|
309
|
-
Returns entry by index.
|
|
310
|
-
|
|
311
|
-
- @param storeName The name of the store to query
|
|
312
|
-
- @param indexName The index name to filter
|
|
313
|
-
- @param key The entry key.
|
|
314
|
-
|
|
315
|
-
```js
|
|
316
|
-
this.dbService.getByIndex('people', 'name', 'Dave').subscribe((people) => {
|
|
317
|
-
console.log(people);
|
|
318
|
-
});
|
|
319
|
-
```
|
|
320
|
-
|
|
321
|
-
### createObjectStore(storeSchema: ObjectStoreMeta, migrationFactory?: () => { [key: number]: (db: IDBDatabase, transaction: IDBTransaction) => void }): void
|
|
322
|
-
|
|
323
|
-
Allows to crate a new object store ad-hoc
|
|
324
|
-
|
|
325
|
-
- @param storeName The name of the store to be created
|
|
326
|
-
- @param migrationFactory The migration factory if exists
|
|
327
|
-
|
|
328
|
-
```js
|
|
329
|
-
const storeSchema: ObjectStoreMeta = {
|
|
330
|
-
store: 'people',
|
|
331
|
-
storeConfig: { keyPath: 'id', autoIncrement: true },
|
|
332
|
-
storeSchema: [
|
|
333
|
-
{ name: 'name', keypath: 'name', options: { unique: false } },
|
|
334
|
-
{ name: 'email', keypath: 'email', options: { unique: false } },
|
|
335
61
|
],
|
|
336
62
|
};
|
|
337
63
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
### count(storeName: string, query?: IDBValidKey | IDBKeyRange): Observable<number>
|
|
342
|
-
|
|
343
|
-
Returns the number of rows in a store.
|
|
344
|
-
|
|
345
|
-
- @param storeName The name of the store to query
|
|
346
|
-
- @param query The key or key range criteria to apply.
|
|
347
|
-
|
|
348
|
-
```js
|
|
349
|
-
this.dbService.count('people').subscribe((peopleCount) => {
|
|
350
|
-
console.log(peopleCount);
|
|
351
|
-
});
|
|
352
|
-
```
|
|
353
|
-
|
|
354
|
-
### countByIndex(storeName: string, indexName: string, query?: IDBValidKey | IDBKeyRange): Observable<number>
|
|
355
|
-
|
|
356
|
-
Returns the number of records within a key range.
|
|
357
|
-
|
|
358
|
-
- @param storeName The name of the store to query
|
|
359
|
-
- @param indexName The index name to filter
|
|
360
|
-
- @param query The key or key range criteria to apply.
|
|
361
|
-
|
|
362
|
-
```js
|
|
363
|
-
this.dbService.countByIndex('people', 'email').subscribe((peopleCount) => {
|
|
364
|
-
console.log(peopleCount);
|
|
365
|
-
});
|
|
366
|
-
```
|
|
367
|
-
|
|
368
|
-
### deleteObjectStore(storeName: string): Observable<void>
|
|
369
|
-
|
|
370
|
-
Delete the store by name.
|
|
371
|
-
|
|
372
|
-
- @param storeName The name of the store to query
|
|
373
|
-
|
|
374
|
-
```js
|
|
375
|
-
this.dbService.deleteObjectStore(this.storneNameToDelete);
|
|
376
|
-
```
|
|
377
|
-
|
|
378
|
-
### delete<T>(storeName: string, key: Key): Observable<T[]>
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
Returns all items from the store after delete.
|
|
382
|
-
|
|
383
|
-
- @param storeName The name of the store to have the entry deleted
|
|
384
|
-
- @param key The key of the entry to be deleted
|
|
385
|
-
|
|
386
|
-
```js
|
|
387
|
-
this.dbService.delete('people', 3).subscribe((allPeople) => {
|
|
388
|
-
console.log('all people:', allPeople);
|
|
389
|
-
});
|
|
390
|
-
```
|
|
391
|
-
|
|
392
|
-
### deleteByKey(storeName: string, key: Key): Observable<void>
|
|
393
|
-
|
|
394
|
-
Returns if the delete completes successfully.
|
|
395
|
-
|
|
396
|
-
- @param storeName The name of the store to have the entry deleted
|
|
397
|
-
- @param key The key of the entry to be deleted
|
|
398
|
-
|
|
399
|
-
```js
|
|
400
|
-
this.dbService.deleteByKey('people', 3).subscribe((status) => {
|
|
401
|
-
console.log('Deleted?:', status);
|
|
402
|
-
});
|
|
403
|
-
```
|
|
404
|
-
|
|
405
|
-
### openCursor<V = any, P extends IDBValidKey = IDBValidKey, K extends IDBValidKey = IDBValidKey>({
|
|
406
|
-
storeName: string,
|
|
407
|
-
query?: IDBValidKey | IDBKeyRange | null,
|
|
408
|
-
direction?: IDBCursorDirection,
|
|
409
|
-
mode: DBMode = DBMode.readonly
|
|
410
|
-
}): Observable<NgxIDBCursorWithValue<V, P, K>>
|
|
411
|
-
|
|
412
|
-
Opens a cursor.
|
|
413
|
-
If no matching data are present, the observable is completed immediately.
|
|
414
|
-
|
|
415
|
-
- @param options The options to open the cursor
|
|
416
|
-
- @param options.storeName The name of the store to have the entries deleted
|
|
417
|
-
- @param options.query The key or key range criteria to apply
|
|
418
|
-
- @param options.direction A string telling the cursor which direction to travel
|
|
419
|
-
- @param options.mode The transaction mode.
|
|
420
|
-
|
|
421
|
-
```js
|
|
422
|
-
this.dbService.openCursor('people', IDBKeyRange.bound("A", "F")).subscribe((cursor) => {
|
|
423
|
-
console.log(cursor.value);
|
|
424
|
-
});
|
|
425
|
-
```
|
|
426
|
-
|
|
427
|
-
### openCursorByIndex<V = any, P extends IDBValidKey = IDBValidKey, K extends IDBValidKey = IDBValidKey>({
|
|
428
|
-
storeName: string,
|
|
429
|
-
indexName: string,
|
|
430
|
-
query?: IDBValidKey | IDBKeyRange | null,
|
|
431
|
-
direction?: IDBCursorDirection,
|
|
432
|
-
mode: DBMode = DBMode.readonly
|
|
433
|
-
}): Observable<NgxIDBCursorWithValue<V, P, K>>
|
|
434
|
-
|
|
435
|
-
Open a cursor by index filter.
|
|
436
|
-
If no matching data are present, the observable is completed immediately.
|
|
437
|
-
|
|
438
|
-
- @param options The options to open the cursor
|
|
439
|
-
- @param options.storeName The name of the store to query
|
|
440
|
-
- @param options.indexName The index name to filter
|
|
441
|
-
- @param options.query The key or key range criteria to apply
|
|
442
|
-
- @param options.direction A string telling the cursor which direction to travel
|
|
443
|
-
- @param options.mode The transaction mode.
|
|
444
|
-
|
|
445
|
-
```js
|
|
446
|
-
this.dbService.openCursorByIndex('people', 'name', IDBKeyRange.only('john')).subscribe((cursor) => {
|
|
447
|
-
console.log(cursor.value);
|
|
448
|
-
});
|
|
449
|
-
```
|
|
450
|
-
|
|
451
|
-
### getAllByIndex<T>(storeName: string, indexName: string, query?: IDBValidKey | IDBKeyRange | null, direction?: IDBCursorDirection): Observable<T[]>
|
|
452
|
-
|
|
453
|
-
Returns all items by an index.
|
|
454
|
-
|
|
455
|
-
- @param storeName The name of the store to query
|
|
456
|
-
- @param indexName The index name to filter
|
|
457
|
-
- @param query The key or key range criteria to apply
|
|
458
|
-
- @param direction A string telling the cursor which direction to travel.
|
|
459
|
-
|
|
460
|
-
```js
|
|
461
|
-
this.dbService.getAllByIndex('people', 'name', IDBKeyRange.only('john')).subscribe((allPeopleByIndex) => {
|
|
462
|
-
console.log('All: ', allPeopleByIndex);
|
|
463
|
-
});
|
|
464
|
-
```
|
|
465
|
-
|
|
466
|
-
### getAllKeysByIndex<P extends IDBValidKey = IDBValidKey, K extends IDBValidKey = IDBValidKey>(storeName: string, indexName: string, query?: IDBValidKey | IDBKeyRange | null, direction?: IDBCursorDirection): Observable<IndexKey<P, K>[]>
|
|
467
|
-
|
|
468
|
-
Returns all items by an index.
|
|
469
|
-
|
|
470
|
-
- @param storeName The name of the store to query
|
|
471
|
-
- @param indexName The index name to filter
|
|
472
|
-
- @param query The range value and criteria to apply on the index
|
|
473
|
-
- @param direction A string telling the cursor which direction to travel.
|
|
474
|
-
|
|
475
|
-
```js
|
|
476
|
-
this.dbService.getAllKeysByIndex('people', 'name', IDBKeyRange.only('john')).subscribe((keys) => {
|
|
477
|
-
console.log(keys);
|
|
478
|
-
});
|
|
479
|
-
```
|
|
480
|
-
|
|
481
|
-
### getDatabaseVersion(): Observable<number>
|
|
482
|
-
|
|
483
|
-
Returns the current database version.
|
|
484
|
-
|
|
485
|
-
```js
|
|
486
|
-
this.dbService.getDatabaseVersion().pipe(
|
|
487
|
-
tap(response => console.log('Versione database => ', response)),
|
|
488
|
-
catchError(err => {
|
|
489
|
-
console.error('Error recover version => ', err);
|
|
490
|
-
return throwError(err);
|
|
491
|
-
})
|
|
492
|
-
).subscribe();
|
|
64
|
+
export const appConfig: ApplicationConfig = {
|
|
65
|
+
providers: [provideIndexedDb(dbConfig)],
|
|
66
|
+
};
|
|
493
67
|
```
|
|
494
68
|
|
|
495
|
-
|
|
69
|
+
With NgModules, use `NgxIndexedDBModule.forRoot(dbConfig)` in `imports` instead.
|
|
496
70
|
|
|
497
|
-
|
|
71
|
+
Then inject the service and use it:
|
|
498
72
|
|
|
499
|
-
|
|
73
|
+
```ts
|
|
74
|
+
import { Injectable, inject } from '@angular/core';
|
|
75
|
+
import { NgxIndexedDBService } from 'ngx-indexed-db';
|
|
500
76
|
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
77
|
+
interface Person {
|
|
78
|
+
id?: number;
|
|
79
|
+
name: string;
|
|
80
|
+
email: string;
|
|
81
|
+
}
|
|
506
82
|
|
|
507
|
-
|
|
83
|
+
@Injectable({ providedIn: 'root' })
|
|
84
|
+
export class PeopleService {
|
|
85
|
+
private readonly db = inject(NgxIndexedDBService);
|
|
508
86
|
|
|
509
|
-
|
|
87
|
+
add(person: Person) {
|
|
88
|
+
return this.db.add<Person>('people', person); // emits the stored record, with its id
|
|
89
|
+
}
|
|
510
90
|
|
|
511
|
-
|
|
512
|
-
this.
|
|
513
|
-
|
|
514
|
-
});
|
|
91
|
+
findByEmail(email: string) {
|
|
92
|
+
return this.db.getByIndex<Person>('people', 'email', email);
|
|
93
|
+
}
|
|
515
94
|
|
|
95
|
+
all() {
|
|
96
|
+
return this.db.getAll<Person>('people');
|
|
97
|
+
}
|
|
98
|
+
}
|
|
516
99
|
```
|
|
517
|
-
### getAllObjectStoreNames(): Observable<string[]>
|
|
518
100
|
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
```js
|
|
522
|
-
this.dbService.getAllObjectStoreNames().subscribe((storeNames) => {
|
|
523
|
-
console.log('storeNames: ', storeNames);
|
|
524
|
-
});
|
|
525
|
-
```
|
|
101
|
+
Every method and its options are in the [API reference](https://assuncaocharles.github.io/ngx-indexed-db/api).
|
|
526
102
|
|
|
527
103
|
## License
|
|
528
104
|
|
|
529
|
-
Released under the terms of the [MIT License](LICENSE).
|
|
105
|
+
Released under the terms of the [MIT License](https://github.com/assuncaocharles/ngx-indexed-db/blob/master/LICENSE).
|