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: index.bs
+9-17Lines changed: 9 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -128,18 +128,13 @@ in-order traversal of very large numbers of data records.
128
128
129
129
130
130
<aside class=example id=example-open-connection>
131
-
In the following example, the API is used to access a "library"
132
-
database that holds books stored by their "isbn" attribute.
133
-
Additionally, an index is maintained on the "title" attribute of the
134
-
objects stored in the object store. This index can be used to look up
135
-
books by title, and enforces a uniqueness constraint. Another index is
136
-
maintained on the "author" attribute of the objects, and can be used
137
-
to look up books by author.
138
-
139
-
A connection to the database is opened. If the "library" database did
140
-
not already exist, it is created and an event handler creates the
141
-
object store and indexes. Finally, the opened connection is saved for
142
-
use in subsequent examples.
131
+
The following example uses the API to access a `"library"` database. It has a `"books"` object store that holds books records stored by their `"isbn"` property as the primary key.
132
+
133
+
Book records have a `"title"` property. This example artificially requires that book titles are unique. The code enforces this by creating an index named `"by_title"` with the {{IDBIndexParameters/unique}} option set. This index is used to look up books by title, and will prevent adding books with non-unique titles.
134
+
135
+
Book records also have an `"author"` property, which is not <span class=allow-2119>required</span> to be unique. The code creates another index named `"by_author"` to allow look-ups by this property.
136
+
137
+
The code first opens a connection to the database. The <a event>`upgradeneeded`</a> event handler code creates the object store and indexes, if needed. The <a event>`success`</a> event handler code saves the opened connection for use in later examples.
The following example shows how errors could be handled when a request
225
-
fails.
219
+
The following example shows one way to handle errors when a request fails.
226
220
227
221
```js
228
222
const tx = db.transaction("books", "readwrite");
@@ -245,9 +239,7 @@ The database connection can be closed when it is no longer needed.
245
239
db.close();
246
240
```
247
241
248
-
In the future, the database might have grown to contain other object
249
-
stores and indexes. The following example shows one way to handle
250
-
migrating from an older version of the database.
242
+
In the future, the database might have grown to contain other object stores and indexes. The following example shows one way to handle migrating from an older version.
251
243
252
244
```js
253
245
const request = indexedDB.open("library", 3); // Request version 3.
0 commit comments