ngx-indexed-db 16.0.0 → 17.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 +33 -8
- package/dist/ngx-indexed-db/LICENSE +19 -0
- package/dist/ngx-indexed-db/README.md +402 -0
- package/dist/ngx-indexed-db/esm2022/lib/ngx-indexed-db.mjs +85 -0
- package/dist/ngx-indexed-db/esm2022/lib/ngx-indexed-db.service.mjs +601 -0
- package/dist/ngx-indexed-db/esm2022/lib/ngxindexeddb.module.mjs +29 -0
- package/{fesm2020 → dist/ngx-indexed-db/fesm2022}/ngx-indexed-db.mjs +69 -60
- package/dist/ngx-indexed-db/fesm2022/ngx-indexed-db.mjs.map +1 -0
- package/{lib → dist/ngx-indexed-db/lib}/ngx-indexed-db.d.ts +1 -1
- package/{lib → dist/ngx-indexed-db/lib}/ngx-indexed-db.meta.d.ts +2 -2
- package/{lib → dist/ngx-indexed-db/lib}/ngx-indexed-db.service.d.ts +4 -4
- package/dist/ngx-indexed-db/package.json +47 -0
- package/package.json +71 -40
- package/esm2020/lib/ngx-indexed-db.mjs +0 -77
- package/esm2020/lib/ngx-indexed-db.service.mjs +0 -601
- package/esm2020/lib/ngxindexeddb.module.mjs +0 -28
- package/fesm2015/ngx-indexed-db.mjs +0 -745
- package/fesm2015/ngx-indexed-db.mjs.map +0 -1
- package/fesm2020/ngx-indexed-db.mjs.map +0 -1
- /package/{esm2020 → dist/ngx-indexed-db/esm2022}/lib/ngx-indexed-db.meta.mjs +0 -0
- /package/{esm2020 → dist/ngx-indexed-db/esm2022}/ngx-indexed-db.mjs +0 -0
- /package/{esm2020 → dist/ngx-indexed-db/esm2022}/public-api.mjs +0 -0
- /package/{esm2020 → dist/ngx-indexed-db/esm2022}/utils/index.mjs +0 -0
- /package/{index.d.ts → dist/ngx-indexed-db/index.d.ts} +0 -0
- /package/{lib → dist/ngx-indexed-db/lib}/ngxindexeddb.module.d.ts +0 -0
- /package/{public-api.d.ts → dist/ngx-indexed-db/public-api.d.ts} +0 -0
- /package/{utils → dist/ngx-indexed-db/utils}/index.d.ts +0 -0
package/README.md
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
# ngx-indexed-db
|
|
2
|
+
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
3
|
+
[](#contributors-)
|
|
4
|
+
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
2
5
|
|
|
3
6
|
[](https://snyk.io/test/github/assuncaocharles/ngx-indexed-db) [](https://www.codefactor.io/repository/github/assuncaocharles/ngx-indexed-db/overview/master) [](https://travis-ci.com/assuncaocharles/ngx-indexed-db) 
|
|
4
7
|
|
|
@@ -18,10 +21,10 @@ $ yarn add ngx-indexed-db
|
|
|
18
21
|
|
|
19
22
|
## Usage
|
|
20
23
|
|
|
21
|
-
Import the `NgxIndexedDBModule` and
|
|
24
|
+
Import the `NgxIndexedDBModule` and set up it:
|
|
22
25
|
|
|
23
26
|
```js
|
|
24
|
-
import { NgxIndexedDBModule } from 'ngx-indexed-db';
|
|
27
|
+
import { NgxIndexedDBModule, DBConfig } from 'ngx-indexed-db';
|
|
25
28
|
|
|
26
29
|
const dbConfig: DBConfig = {
|
|
27
30
|
name: 'MyDb',
|
|
@@ -164,7 +167,6 @@ this.dbService
|
|
|
164
167
|
});
|
|
165
168
|
```
|
|
166
169
|
|
|
167
|
-
|
|
168
170
|
### bulkDelete(storeName: string, keys: Key[]): Observable<number[]>
|
|
169
171
|
|
|
170
172
|
Delete multiple items in the store
|
|
@@ -284,17 +286,16 @@ this.dbService.count('people').subscribe((peopleCount) => {
|
|
|
284
286
|
|
|
285
287
|
### deleteObjectStore(storeName: string): Observable<boolean>
|
|
286
288
|
|
|
287
|
-
Delete the store by name, return true or false.
|
|
289
|
+
Delete the store by name, return true or false.
|
|
288
290
|
|
|
289
291
|
- @param storeName The name of the store to query
|
|
290
292
|
|
|
291
293
|
```js
|
|
292
294
|
this.dbService.deleteObjectStore(this.storneNameToDelete);
|
|
293
295
|
```
|
|
294
|
-
|
|
296
|
+
|
|
295
297
|
### delete<T>(storeName: string, key: Key): Observable<T[]>
|
|
296
298
|
|
|
297
|
-
|
|
298
299
|
Returns all items from the store after delete.
|
|
299
300
|
|
|
300
301
|
- @param storeName The name of the store to have the entry deleted
|
|
@@ -319,12 +320,13 @@ this.dbService.deleteByKey('people', 3).subscribe((status) => {
|
|
|
319
320
|
});
|
|
320
321
|
```
|
|
321
322
|
|
|
322
|
-
### openCursor(storeName: string, keyRange?: IDBKeyRange): Observable<Event>
|
|
323
|
+
### openCursor(storeName: string, keyRange?: IDBKeyRange, direction?: IDBCursorDirection): Observable<Event>
|
|
323
324
|
|
|
324
325
|
Returns the open cursor event
|
|
325
326
|
|
|
326
327
|
- @param storeName The name of the store to have the entries deleted
|
|
327
328
|
- @param keyRange The key range which the cursor should be open on
|
|
329
|
+
- @param direction IDB Cursor Direction to work with, default to `next`
|
|
328
330
|
|
|
329
331
|
```js
|
|
330
332
|
this.dbService.openCursor('people', IDBKeyRange.bound("A", "F")).subscribe((evt) => {
|
|
@@ -338,13 +340,14 @@ this.dbService.openCursor('people', IDBKeyRange.bound("A", "F")).subscribe((evt)
|
|
|
338
340
|
});
|
|
339
341
|
```
|
|
340
342
|
|
|
341
|
-
### openCursorByIndex(storeName: string, indexName: string, keyRange: IDBKeyRange, mode?: DBMode): Observable<Event>
|
|
343
|
+
### openCursorByIndex(storeName: string, indexName: string, keyRange: IDBKeyRange, direction?: IDBCursorDirection, mode?: DBMode): Observable<Event>
|
|
342
344
|
|
|
343
345
|
Open a cursor by index filter.
|
|
344
346
|
|
|
345
347
|
- @param storeName The name of the store to query.
|
|
346
348
|
- @param indexName The index name to filter.
|
|
347
349
|
- @param keyRange The range value and criteria to apply on the index.
|
|
350
|
+
- @param direction IDB Cursor Direction to work with, default to `next`
|
|
348
351
|
- @param mode DB Mode to work with, default to `readonly`
|
|
349
352
|
|
|
350
353
|
```js
|
|
@@ -398,3 +401,25 @@ this.dbService.deleteDatabase().subscribe((deleted) => {
|
|
|
398
401
|
## License
|
|
399
402
|
|
|
400
403
|
Released under the terms of the [MIT License](LICENSE).
|
|
404
|
+
|
|
405
|
+
## Contributors ✨
|
|
406
|
+
|
|
407
|
+
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
|
|
408
|
+
|
|
409
|
+
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
|
|
410
|
+
<!-- prettier-ignore-start -->
|
|
411
|
+
<!-- markdownlint-disable -->
|
|
412
|
+
<table>
|
|
413
|
+
<tr>
|
|
414
|
+
<td align="center"><a href="http://www.angeloparziale.it"><img src="https://avatars.githubusercontent.com/u/16490359?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Angelo Parziale</b></sub></a><br /><a href="#maintenance-aparzi" title="Maintenance">🚧</a> <a href="https://github.com/assuncaocharles/ngx-indexed-db/commits?author=aparzi" title="Code">💻</a></td>
|
|
415
|
+
<td align="center"><a href="https://charlesassuncao.tech/"><img src="https://avatars.githubusercontent.com/u/8545105?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Charles Assunção</b></sub></a><br /><a href="https://github.com/assuncaocharles/ngx-indexed-db/commits?author=assuncaocharles" title="Code">💻</a> <a href="https://github.com/assuncaocharles/ngx-indexed-db/commits?author=assuncaocharles" title="Documentation">📖</a> <a href="#maintenance-assuncaocharles" title="Maintenance">🚧</a></td>
|
|
416
|
+
<td align="center"><a href="https://github.com/coolweb"><img src="https://avatars.githubusercontent.com/u/3740250?v=4?s=100" width="100px;" alt=""/><br /><sub><b>coolweb</b></sub></a><br /><a href="#maintenance-coolweb" title="Maintenance">🚧</a></td>
|
|
417
|
+
</tr>
|
|
418
|
+
</table>
|
|
419
|
+
|
|
420
|
+
<!-- markdownlint-restore -->
|
|
421
|
+
<!-- prettier-ignore-end -->
|
|
422
|
+
|
|
423
|
+
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
|
424
|
+
|
|
425
|
+
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
|
|
@@ -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.
|
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
# ngx-indexed-db
|
|
2
|
+
|
|
3
|
+
[](https://snyk.io/test/github/assuncaocharles/ngx-indexed-db) [](https://www.codefactor.io/repository/github/assuncaocharles/ngx-indexed-db/overview/master) [](https://travis-ci.com/assuncaocharles/ngx-indexed-db) 
|
|
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 & {id: any}>
|
|
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: Array<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): Observable<T[]>
|
|
195
|
+
|
|
196
|
+
Adds or updates a record in store with the given value and key. Return item updated
|
|
197
|
+
|
|
198
|
+
- @param storeName The name of the store to update
|
|
199
|
+
- @param value The new value for the entry
|
|
200
|
+
|
|
201
|
+
```js
|
|
202
|
+
this.dbService
|
|
203
|
+
.update('people', {
|
|
204
|
+
id: 1,
|
|
205
|
+
email: 'luke@skywalker.com',
|
|
206
|
+
name: 'Luke Skywalker',
|
|
207
|
+
})
|
|
208
|
+
.subscribe((storeData) => {
|
|
209
|
+
console.log('storeData: ', storeData);
|
|
210
|
+
});
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### getByKey<T>(storeName: string, key: IDBValidKey): Observable<T>
|
|
214
|
+
|
|
215
|
+
Returns entry by key.
|
|
216
|
+
|
|
217
|
+
- @param storeName The name of the store to query
|
|
218
|
+
- @param key The entry key
|
|
219
|
+
|
|
220
|
+
```js
|
|
221
|
+
this.dbService.getByKey('people', 1).subscribe((people) => {
|
|
222
|
+
console.log(people);
|
|
223
|
+
});
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### getAll<T>(storeName: string): Observable<T[]>
|
|
227
|
+
|
|
228
|
+
Return all elements from one store
|
|
229
|
+
|
|
230
|
+
- @param storeName The name of the store to select the items
|
|
231
|
+
|
|
232
|
+
```js
|
|
233
|
+
this.dbService.getAll('people').subscribe((peoples) => {
|
|
234
|
+
console.log(peoples);
|
|
235
|
+
});
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
### getByIndex<T>(storeName: string, indexName: string, key: IDBValidKey): Observable<T>
|
|
239
|
+
|
|
240
|
+
Returns entry by index.
|
|
241
|
+
|
|
242
|
+
- @param storeName The name of the store to query
|
|
243
|
+
- @param indexName The index name to filter
|
|
244
|
+
- @param key The entry key.
|
|
245
|
+
|
|
246
|
+
```js
|
|
247
|
+
this.dbService.getByIndex('people', 'name', 'Dave').subscribe((people) => {
|
|
248
|
+
console.log(people);
|
|
249
|
+
});
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### createObjectStore(storeSchema: ObjectStoreMeta, migrationFactory?: () => { [key: number]: (db: IDBDatabase, transaction: IDBTransaction) => void }): void
|
|
253
|
+
|
|
254
|
+
Allows to crate a new object store ad-hoc
|
|
255
|
+
|
|
256
|
+
- @param storeName The name of the store to be created
|
|
257
|
+
- @param migrationFactory The migration factory if exists
|
|
258
|
+
|
|
259
|
+
```js
|
|
260
|
+
const storeSchema: ObjectStoreMeta = {
|
|
261
|
+
store: 'people',
|
|
262
|
+
storeConfig: { keyPath: 'id', autoIncrement: true },
|
|
263
|
+
storeSchema: [
|
|
264
|
+
{ name: 'name', keypath: 'name', options: { unique: false } },
|
|
265
|
+
{ name: 'email', keypath: 'email', options: { unique: false } },
|
|
266
|
+
],
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
this.dbService.createObjectStore(storeSchema);
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
### count(storeName: string, keyRange?: IDBValidKey | IDBKeyRange): Observable<number>
|
|
273
|
+
|
|
274
|
+
Returns the number of rows in a store.
|
|
275
|
+
|
|
276
|
+
- @param storeName The name of the store to query
|
|
277
|
+
- @param keyRange The range value and criteria to apply.
|
|
278
|
+
|
|
279
|
+
```js
|
|
280
|
+
this.dbService.count('people').subscribe((peopleCount) => {
|
|
281
|
+
console.log(peopleCount);
|
|
282
|
+
});
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
### deleteObjectStore(storeName: string): Observable<boolean>
|
|
286
|
+
|
|
287
|
+
Delete the store by name, return true or false.
|
|
288
|
+
|
|
289
|
+
- @param storeName The name of the store to query
|
|
290
|
+
|
|
291
|
+
```js
|
|
292
|
+
this.dbService.deleteObjectStore(this.storneNameToDelete);
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
### delete<T>(storeName: string, key: Key): Observable<T[]>
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
Returns all items from the store after delete.
|
|
299
|
+
|
|
300
|
+
- @param storeName The name of the store to have the entry deleted
|
|
301
|
+
- @param key The key of the entry to be deleted
|
|
302
|
+
|
|
303
|
+
```js
|
|
304
|
+
this.dbService.delete('people', 3).subscribe((allPeople) => {
|
|
305
|
+
console.log('all people:', allPeople);
|
|
306
|
+
});
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
### deleteByKey(storeName: string, key: Key): Observable<boolean>
|
|
310
|
+
|
|
311
|
+
Returns true if the delete completes successfully.
|
|
312
|
+
|
|
313
|
+
- @param storeName The name of the store to have the entry deleted
|
|
314
|
+
- @param key The key of the entry to be deleted
|
|
315
|
+
|
|
316
|
+
```js
|
|
317
|
+
this.dbService.deleteByKey('people', 3).subscribe((status) => {
|
|
318
|
+
console.log('Deleted?:', status);
|
|
319
|
+
});
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
### openCursor(storeName: string, keyRange?: IDBKeyRange, direction?: IDBCursorDirection): Observable<Event>
|
|
323
|
+
|
|
324
|
+
Returns the open cursor event
|
|
325
|
+
|
|
326
|
+
- @param storeName The name of the store to have the entries deleted
|
|
327
|
+
- @param keyRange The key range which the cursor should be open on
|
|
328
|
+
- @param direction IDB Cursor Direction to work with, default to `next`
|
|
329
|
+
|
|
330
|
+
```js
|
|
331
|
+
this.dbService.openCursor('people', IDBKeyRange.bound("A", "F")).subscribe((evt) => {
|
|
332
|
+
var cursor = (evt.target as IDBOpenDBRequest).result;
|
|
333
|
+
if(cursor) {
|
|
334
|
+
console.log(cursor.value);
|
|
335
|
+
cursor.continue();
|
|
336
|
+
} else {
|
|
337
|
+
console.log('Entries all displayed.');
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
### openCursorByIndex(storeName: string, indexName: string, keyRange: IDBKeyRange, direction?: IDBCursorDirection, mode?: DBMode): Observable<Event>
|
|
343
|
+
|
|
344
|
+
Open a cursor by index filter.
|
|
345
|
+
|
|
346
|
+
- @param storeName The name of the store to query.
|
|
347
|
+
- @param indexName The index name to filter.
|
|
348
|
+
- @param keyRange The range value and criteria to apply on the index.
|
|
349
|
+
- @param direction IDB Cursor Direction to work with, default to `next`
|
|
350
|
+
- @param mode DB Mode to work with, default to `readonly`
|
|
351
|
+
|
|
352
|
+
```js
|
|
353
|
+
this.dbService.openCursorByIndex('people', 'name', IDBKeyRange.only('john')).subscribe((evt) => {
|
|
354
|
+
var cursor = (evt.target as IDBOpenDBRequest).result;
|
|
355
|
+
if(cursor) {
|
|
356
|
+
console.log(cursor.value);
|
|
357
|
+
cursor.continue();
|
|
358
|
+
} else {
|
|
359
|
+
console.log('Entries all displayed.');
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
### getAllByIndex<T>(storeName: string, indexName: string, keyRange: IDBKeyRange): Observable<T[]>
|
|
365
|
+
|
|
366
|
+
Returns all items by an index.
|
|
367
|
+
|
|
368
|
+
- @param storeName The name of the store to query
|
|
369
|
+
- @param indexName The index name to filter
|
|
370
|
+
- @param keyRange The range value and criteria to apply on the index.
|
|
371
|
+
|
|
372
|
+
```js
|
|
373
|
+
this.dbService.getAllByIndex('people', 'name', IDBKeyRange.only('john')).subscribe((allPeopleByIndex) => {
|
|
374
|
+
console.log('All: ', allPeopleByIndex);
|
|
375
|
+
});
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
### clear(storeName: string): Observable<boolean>
|
|
379
|
+
|
|
380
|
+
Returns true if successfully delete all entries from the store.
|
|
381
|
+
|
|
382
|
+
- @param storeName The name of the store to have the entries deleted
|
|
383
|
+
|
|
384
|
+
```js
|
|
385
|
+
this.dbService.clear('people').subscribe((successDeleted) => {
|
|
386
|
+
console.log('success? ', successDeleted);
|
|
387
|
+
});
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
### deleteDatabase(): Observable<boolean>
|
|
391
|
+
|
|
392
|
+
Returns true if successfully delete the DB.
|
|
393
|
+
|
|
394
|
+
```js
|
|
395
|
+
this.dbService.deleteDatabase().subscribe((deleted) => {
|
|
396
|
+
console.log('Database deleted successfully: ', deleted);
|
|
397
|
+
});
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
## License
|
|
401
|
+
|
|
402
|
+
Released under the terms of the [MIT License](LICENSE).
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Observable } from 'rxjs';
|
|
2
|
+
export function openDatabase(indexedDB, dbName, version, upgradeCallback) {
|
|
3
|
+
return new Promise((resolve, reject) => {
|
|
4
|
+
if (!indexedDB) {
|
|
5
|
+
reject('IndexedDB not available');
|
|
6
|
+
}
|
|
7
|
+
const request = indexedDB.open(dbName, version);
|
|
8
|
+
let db;
|
|
9
|
+
request.onsuccess = (event) => {
|
|
10
|
+
db = request.result;
|
|
11
|
+
resolve(db);
|
|
12
|
+
};
|
|
13
|
+
request.onerror = (event) => {
|
|
14
|
+
reject(`IndexedDB error: ${request.error}`);
|
|
15
|
+
};
|
|
16
|
+
if (typeof upgradeCallback === 'function') {
|
|
17
|
+
request.onupgradeneeded = (event) => {
|
|
18
|
+
upgradeCallback(event, db);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
export async function CreateObjectStore(indexedDB, dbName, version, storeSchemas, migrationFactory) {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
if (!indexedDB) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const request = indexedDB.open(dbName, version);
|
|
29
|
+
request.onupgradeneeded = async (event) => {
|
|
30
|
+
const database = event.target.result;
|
|
31
|
+
const storeCreationPromises = storeSchemas.map(async (storeSchema) => {
|
|
32
|
+
if (!database.objectStoreNames.contains(storeSchema.store)) {
|
|
33
|
+
const objectStore = database.createObjectStore(storeSchema.store, storeSchema.storeConfig);
|
|
34
|
+
for (const schema of storeSchema.storeSchema) {
|
|
35
|
+
objectStore.createIndex(schema.name, schema.keypath, schema.options);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
await Promise.all(storeCreationPromises);
|
|
40
|
+
const storeMigrations = migrationFactory && migrationFactory();
|
|
41
|
+
if (storeMigrations) {
|
|
42
|
+
const migrationKeys = Object.keys(storeMigrations)
|
|
43
|
+
.map((k) => parseInt(k, 10))
|
|
44
|
+
.filter((v) => v > event.oldVersion)
|
|
45
|
+
.sort((a, b) => a - b);
|
|
46
|
+
for (const v of migrationKeys) {
|
|
47
|
+
storeMigrations[v](database, request.transaction);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
database.close();
|
|
51
|
+
resolve();
|
|
52
|
+
};
|
|
53
|
+
request.onsuccess = (e) => {
|
|
54
|
+
e.target.result.close();
|
|
55
|
+
resolve();
|
|
56
|
+
};
|
|
57
|
+
request.onerror = (error) => {
|
|
58
|
+
reject(error);
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
export function DeleteObjectStore(dbName, version, storeName) {
|
|
63
|
+
if (!dbName || !version || !storeName) {
|
|
64
|
+
throw Error('Params: "dbName", "version", "storeName" are mandatory.');
|
|
65
|
+
}
|
|
66
|
+
return new Observable((obs) => {
|
|
67
|
+
try {
|
|
68
|
+
const newVersion = version + 1;
|
|
69
|
+
const request = indexedDB.open(dbName, newVersion);
|
|
70
|
+
request.onupgradeneeded = (event) => {
|
|
71
|
+
const database = event.target.result;
|
|
72
|
+
database.deleteObjectStore(storeName);
|
|
73
|
+
database.close();
|
|
74
|
+
console.log('onupgradeneeded');
|
|
75
|
+
obs.next(true);
|
|
76
|
+
obs.complete();
|
|
77
|
+
};
|
|
78
|
+
request.onerror = (e) => obs.error(e);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
obs.error(error);
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibmd4LWluZGV4ZWQtZGIuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi9wcm9qZWN0cy9uZ3gtaW5kZXhlZC1kYi9zcmMvbGliL25neC1pbmRleGVkLWRiLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUNBLE9BQU8sRUFBRSxVQUFVLEVBQWMsTUFBTSxNQUFNLENBQUM7QUFFOUMsTUFBTSxVQUFVLFlBQVksQ0FDMUIsU0FBcUIsRUFDckIsTUFBYyxFQUNkLE9BQWdCLEVBQ2hCLGVBQW9EO0lBRXBELE9BQU8sSUFBSSxPQUFPLENBQWMsQ0FBQyxPQUFPLEVBQUUsTUFBTSxFQUFFLEVBQUU7UUFDbEQsSUFBSSxDQUFDLFNBQVMsRUFBRTtZQUNkLE1BQU0sQ0FBQyx5QkFBeUIsQ0FBQyxDQUFDO1NBQ25DO1FBQ0QsTUFBTSxPQUFPLEdBQUcsU0FBUyxDQUFDLElBQUksQ0FBQyxNQUFNLEVBQUUsT0FBTyxDQUFDLENBQUM7UUFDaEQsSUFBSSxFQUFlLENBQUM7UUFDcEIsT0FBTyxDQUFDLFNBQVMsR0F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