You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 6-data-storage/03-indexeddb/article.md
+8-6
Original file line number
Diff line number
Diff line change
@@ -193,13 +193,12 @@ A key must be one of these types - number, date, string, binary, or array. It's
193
193
194
194

195
195
196
-
197
196
As we'll see very soon, we can provide a key when we add a value to the store, similar to `localStorage`. But when we store objects, IndexedDB allows setting up an object property as the key, which is much more convenient. Or we can auto-generate keys.
198
197
199
198
But we need to create an object store first.
200
199
201
-
202
200
The syntax to create an object store:
201
+
203
202
```js
204
203
db.createObjectStore(name[, keyOptions]);
205
204
```
@@ -214,6 +213,7 @@ Please note, the operation is synchronous, no `await` needed.
214
213
If we don't supply `keyOptions`, then we'll need to provide a key explicitly later, when storing an object.
215
214
216
215
For instance, this object store uses `id` property as the key:
That's a technical limitation. Outside of the handler we'll be able to add/remove/update the data, but object stores can only be created/removed/altered during a version update.
224
224
225
225
To perform a database version upgrade, there are two main approaches:
226
+
226
227
1. We can implement per-version upgrade functions: from 1 to 2, from 2 to 3, from 3 to 4 etc. Then, in `upgradeneeded` we can compare versions (e.g. old 2, now 4) and run per-version upgrades step by step, for every intermediate version (2 to 3, then 3 to 4).
227
228
2. Or we can just examine the database: get a list of existing object stores as `db.objectStoreNames`. That object is a [DOMStringList](https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#domstringlist) that provides `contains(name)` method to check for existance. And then we can do updates depending on what exists and what doesn't.
As we already know, a transaction auto-commits as soon as the browser is done with the current code and microtasks. So if we put a *macrotask* like `fetch` in the middle of a transaction, then the transaction won't wait for it to finish. It just auto-commits. So the next request in it would fail.
776
778
777
-
778
779
For a promise wrapper and `async/await` the situation is the same.
779
780
780
781
Here's an example of `fetch` in the middle of the transaction:
The next `inventory.add` after `fetch``(*)` fails with an "inactive transaction" error, because the transaction is already committed and closed at that time.
794
795
795
796
The workaround is the same as when working with native IndexedDB: either make a new transaction or just split things apart.
797
+
796
798
1. Prepare the data and fetch all that's needed first.
0 commit comments