@@ -142,14 +142,15 @@ object store and indexes. Finally, the opened connection is saved for
142
142
use in subsequent examples.
143
143
144
144
```js
145
- var request = indexedDB.open("library");
145
+ const request = indexedDB.open("library");
146
+ let db;
146
147
147
148
request.onupgradeneeded = function() {
148
149
// The database did not previously exist, so create object stores and indexes.
149
- var db = request.result;
150
- var store = db.createObjectStore("books", {keyPath: "isbn"});
151
- var titleIndex = store.createIndex("by_title", "title", {unique: true});
152
- var authorIndex = store.createIndex("by_author", "author");
150
+ const db = request.result;
151
+ const store = db.createObjectStore("books", {keyPath: "isbn"});
152
+ const titleIndex = store.createIndex("by_title", "title", {unique: true});
153
+ const authorIndex = store.createIndex("by_author", "author");
153
154
154
155
// Populate with initial data.
155
156
store.put({title: "Quarry Memories", author: "Fred", isbn: 123456});
@@ -165,8 +166,8 @@ request.onsuccess = function() {
165
166
The following example populates the database using a transaction.
166
167
167
168
```js
168
- var tx = db.transaction("books", "readwrite");
169
- var store = tx.objectStore("books");
169
+ const tx = db.transaction("books", "readwrite");
170
+ const store = tx.objectStore("books");
170
171
171
172
store.put({title: "Quarry Memories", author: "Fred", isbn: 123456});
172
173
store.put({title: "Water Buffaloes", author: "Fred", isbn: 234567});
@@ -181,13 +182,13 @@ The following example looks up a single book in the database by title
181
182
using an index.
182
183
183
184
```js
184
- var tx = db.transaction("books", "readonly");
185
- var store = tx.objectStore("books");
186
- var index = store.index("by_title");
185
+ const tx = db.transaction("books", "readonly");
186
+ const store = tx.objectStore("books");
187
+ const index = store.index("by_title");
187
188
188
- var request = index.get("Bedrock Nights");
189
+ const request = index.get("Bedrock Nights");
189
190
request.onsuccess = function() {
190
- var matching = request.result;
191
+ const matching = request.result;
191
192
if (matching !== undefined) {
192
193
// A match was found.
193
194
report(matching.isbn, matching.title, matching.author);
@@ -202,13 +203,13 @@ The following example looks up all books in the database by author
202
203
using an index and a cursor.
203
204
204
205
```js
205
- var tx = db.transaction("books", "readonly");
206
- var store = tx.objectStore("books");
207
- var index = store.index("by_author");
206
+ const tx = db.transaction("books", "readonly");
207
+ const store = tx.objectStore("books");
208
+ const index = store.index("by_author");
208
209
209
- var request = index.openCursor(IDBKeyRange.only("Fred"));
210
+ const request = index.openCursor(IDBKeyRange.only("Fred"));
210
211
request.onsuccess = function() {
211
- var cursor = request.result;
212
+ const cursor = request.result;
212
213
if (cursor) {
213
214
// Called for each matching record.
214
215
report(cursor.value.isbn, cursor.value.title, cursor.value.author);
@@ -224,9 +225,9 @@ The following example shows how errors could be handled when a request
224
225
fails.
225
226
226
227
```js
227
- var tx = db.transaction("books", "readwrite");
228
- var store = tx.objectStore("books");
229
- var request = store.put({title: "Water Buffaloes", author: "Slate", isbn: 987654});
228
+ const tx = db.transaction("books", "readwrite");
229
+ const store = tx.objectStore("books");
230
+ const request = store.put({title: "Water Buffaloes", author: "Slate", isbn: 987654});
230
231
request.onerror = function(event) {
231
232
// The uniqueness constraint of the "by_title" index failed.
232
233
report(request.error);
@@ -249,26 +250,27 @@ stores and indexes. The following example shows one way to handle
249
250
migrating from an older version of the database.
250
251
251
252
```js
252
- var request = indexedDB.open("library", 3); // Request version 3.
253
+ const request = indexedDB.open("library", 3); // Request version 3.
254
+ let db;
253
255
254
256
request.onupgradeneeded = function(event) {
255
- var db = request.result;
257
+ const db = request.result;
256
258
if (event.oldVersion < 1) {
257
259
// Version 1 is the first version of the database.
258
- var store = db.createObjectStore("books", {keyPath: "isbn"});
259
- var titleIndex = store.createIndex("by_title", "title", {unique: true});
260
- var authorIndex = store.createIndex("by_author", "author");
260
+ const store = db.createObjectStore("books", {keyPath: "isbn"});
261
+ const titleIndex = store.createIndex("by_title", "title", {unique: true});
262
+ const authorIndex = store.createIndex("by_author", "author");
261
263
}
262
264
if (event.oldVersion < 2) {
263
265
// Version 2 introduces a new index of books by year.
264
- var bookStore = request.transaction.objectStore("books");
265
- var yearIndex = bookStore.createIndex("by_year", "year");
266
+ const bookStore = request.transaction.objectStore("books");
267
+ const yearIndex = bookStore.createIndex("by_year", "year");
266
268
}
267
269
if (event.oldVersion < 3) {
268
270
// Version 3 introduces a new object store for magazines with two indexes.
269
- var magazines = db.createObjectStore("magazines");
270
- var publisherIndex = magazines.createIndex("by_publisher", "publisher");
271
- var frequencyIndex = magazines.createIndex("by_frequency", "frequency");
271
+ const magazines = db.createObjectStore("magazines");
272
+ const publisherIndex = magazines.createIndex("by_publisher", "publisher");
273
+ const frequencyIndex = magazines.createIndex("by_frequency", "frequency");
272
274
}
273
275
};
274
276
@@ -342,8 +344,8 @@ event fires if other clients still hold a connection to the database after their
342
344
<a event>`versionchange`</a> events have fired.
343
345
344
346
```js
345
- var request = indexedDB.open("library", 4); // Request version 4.
346
- var blockedTimeout;
347
+ const request = indexedDB.open("library", 4); // Request version 4.
348
+ let blockedTimeout;
347
349
348
350
request.onblocked = function() {
349
351
// Give the other clients time to save data asynchronously.
@@ -1754,10 +1756,10 @@ property is assigned a value of 1 because that is the next [=/key=]
1754
1756
generated by the [=key generator=] .
1755
1757
1756
1758
```js
1757
- var store = db.createObjectStore("store", { keyPath: "foo.bar",
1758
- autoIncrement: true });
1759
+ const store = db.createObjectStore("store", { keyPath: "foo.bar",
1760
+ autoIncrement: true });
1759
1761
store.put({ foo: {} }).onsuccess = function(e) {
1760
- var key = e.target.result;
1762
+ const key = e.target.result;
1761
1763
console.assert(key === 1);
1762
1764
};
1763
1765
```
@@ -1779,10 +1781,10 @@ store=] is "`foo.bar`". The actual object has a value of
1779
1781
key value.
1780
1782
1781
1783
```js
1782
- var store = db.createObjectStore("store", { keyPath: "foo.bar",
1783
- autoIncrement: true });
1784
+ const store = db.createObjectStore("store", { keyPath: "foo.bar",
1785
+ autoIncrement: true });
1784
1786
store.put({ foo: { bar: 10 } }).onsuccess = function(e) {
1785
- var key = e.target.result;
1787
+ const key = e.target.result;
1786
1788
console.assert(key === 10);
1787
1789
};
1788
1790
```
@@ -1804,13 +1806,13 @@ properties are created each as a child of the other until a value for
1804
1806
store.
1805
1807
1806
1808
```js
1807
- var store = db.createObjectStore("store", { keyPath: "foo.bar.baz",
1808
- autoIncrement: true });
1809
+ const store = db.createObjectStore("store", { keyPath: "foo.bar.baz",
1810
+ autoIncrement: true });
1809
1811
store.put({ zip: {} }).onsuccess = function(e) {
1810
- var key = e.target.result;
1812
+ const key = e.target.result;
1811
1813
console.assert(key === 1);
1812
1814
store.get(key).onsuccess = function(e) {
1813
- var value = e.target.result;
1815
+ const value = e.target.result;
1814
1816
// value will be: { zip: {}, foo: { bar: { baz: 1 } } }
1815
1817
console.assert(value.foo.bar.baz === 1);
1816
1818
};
@@ -1827,7 +1829,7 @@ the actual object is an array, `[10]`. Trying to define a
1827
1829
property on the array fails.
1828
1830
1829
1831
```js
1830
- var store = db.createObjectStore("store", { keyPath: "foo", autoIncrement: true });
1832
+ const store = db.createObjectStore("store", { keyPath: "foo", autoIncrement: true });
1831
1833
1832
1834
// The key generation will attempt to create and store the key path
1833
1835
// property on this primitive.
@@ -1980,7 +1982,7 @@ Various event handlers are registered for responding to various
1980
1982
situations.
1981
1983
1982
1984
```js
1983
- var request = indexedDB.open('AddressBook' , 15);
1985
+ const request = indexedDB.open('AddressBook' , 15);
1984
1986
request.onsuccess = function(evt) {...};
1985
1987
request.onerror = function(evt) {...};
1986
1988
```
@@ -3595,9 +3597,9 @@ must be used as error.
3595
3597
The asynchronous creation of indexes is observable in the following example:
3596
3598
3597
3599
```js
3598
- var request1 = objectStore.put({name: "betty"}, 1);
3599
- var request2 = objectStore.put({name: "betty"}, 2);
3600
- var index = objectStore.createIndex("by_name", "name", {unique: true});
3600
+ const request1 = objectStore.put({name: "betty"}, 1);
3601
+ const request2 = objectStore.put({name: "betty"}, 2);
3602
+ const index = objectStore.createIndex("by_name", "name", {unique: true});
3601
3603
```
3602
3604
3603
3605
At the point where {{createIndex()}} called, neither of the
0 commit comments