aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/idbobjectstore/index.html
blob: 62eb51e71ff984a9fb540bbc6b3c6c56a2e745d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
---
title: IDBObjectStore
slug: Web/API/IDBObjectStore
tags:
  - API
  - IDBObjectStore
  - IndexedDB
  - Storage
  - local storage & cache
translation_of: Web/API/IDBObjectStore
---
<p>{{APIRef("IndexedDB")}}</p>

<p> <a href="https://developer.mozilla.org/en/IndexedDB" title="en/IndexedDB">IndexedDB API</a>  的 <strong><code>IDBObjectStore </code></strong>接口表示数据库中的 一个 <a href="https://developer.mozilla.org/en/IndexedDB#gloss_object_store" title="en/IndexedDB#gloss object store">对象库(object store)</a> 。对象库中的记录根据其键值进行排序。这种排序可以实现快速插入,查找和有序检索。</p>

<p>{{AvailableInWorkers}}</p>

<p>注:为了方便理解,可以把“对象存储空间”想象成关系数据库的“表”结构,下文也会把对象存储空间称为表。</p>

<h2 id="方法预览">方法预览</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <td><code><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> <a href="#add()">add</a> (in any value, in optional any key) raises (<a href="/En/DOM/DOMException" title="en/DOM/DOMException">DOMException</a>);</code></td>
  </tr>
  <tr>
   <td><code><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> <a href="#clear()">clear</a> () raises (<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></td>
  </tr>
  <tr>
   <td><code><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> <a href="#count()">count</a> (in optional any key) raises</code><code>(<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></td>
  </tr>
  <tr>
   <td><code><a href="/en/IndexedDB/IDBIndex" title="en/IndexedDB/IDBIndex">IDBIndex</a> <a href="#createIndex()">createIndex</a>  (in <a href="/En/DOM/DOMString" title="en/DOM/DOMString">DOMString</a> name, in </code><code><a href="/En/DOM/DOMString" title="en/DOM/DOMString">DOMString</a></code><code> keyPath, in optional boolean unique) raises (<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></td>
  </tr>
  <tr>
   <td><code><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> <a href="#delete()">delete</a> (in any key) raises </code><code>(<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></td>
  </tr>
  <tr>
   <td><code>void <a href="#deleteIndex()">deleteIndex</a> (in any </code><code><a href="/En/DOM/DOMString" title="en/DOM/DOMString">DOMString</a></code><code> indexName) raises (<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></td>
  </tr>
  <tr>
   <td><code><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> <a href="#get()">get</a> (in any key) raises (<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></td>
  </tr>
  <tr>
   <td><code><a href="/en/IndexedDB/IDBIndex" title="en/IndexedDB/IDBIndex">IDBIndex</a> <a href="#index()">index</a> (in </code><code><a href="/En/DOM/DOMString" title="en/DOM/DOMString">DOMString</a></code><code> name) raises (<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></td>
  </tr>
  <tr>
   <td><code><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> <a href="#openCursor()">openCursor</a> (in optional <a href="/en/IndexedDB/IDBKeyRange" title="en/IndexedDB/IDBKeyRange">IDBKeyRange</a> range, in optional unsigned short direction) raises(<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></td>
  </tr>
  <tr>
   <td><code><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> <a href="#put()">put</a> (in any value, in optional any key) raises (<a href="/En/DOM/DOMException" title="en/DOM/DOMException">DOMException</a>);</code></td>
  </tr>
 </tbody>
</table>

<h2 id="属性">属性</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Attribute</th>
   <th scope="col">Type</th>
   <th scope="col">Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td id="attr_indexNames"><code>indexNames</code></td>
   <td><code>readonly DOMStringList</code></td>
   <td>表中对象的<a href="https://developer.mozilla.org/en/IndexedDB#gloss_index" title="https://developer.mozilla.org/en/IndexedDB#gloss_index">索引</a>名列表。</td>
  </tr>
  <tr>
   <td id="attr_keyPath"><code>keyPath</code></td>
   <td><code>readonly </code><code><a href="/En/DOM/DOMString" title="en/DOM/DOMString">DOMString</a></code></td>
   <td>表中的<a href="https://developer.mozilla.org/en/IndexedDB#gloss_key_path" title="https://developer.mozilla.org/en/IndexedDB#gloss_key_path">键路径</a>,如果该属性为null,每次操作表时必须提供一个键名。</td>
  </tr>
  <tr>
   <td id="attr_name"><code>name</code></td>
   <td><code>readonly </code><code><a href="/En/DOM/DOMString" title="en/DOM/DOMString">DOMString</a></code></td>
   <td>表名</td>
  </tr>
  <tr>
   <td id="transaction"><code>transaction</code></td>
   <td><code>readonly <a href="/en-US/docs/IndexedDB/IDBTransaction" title="/en-US/docs/IndexedDB/IDBTransaction">IDBTransaction</a></code></td>
   <td>事务的名称,该表属于此事务。</td>
  </tr>
  <tr>
   <td id="autoincrement"><code>autoIncrement</code></td>
   <td><code>readonly boolean</code></td>
   <td>表中自增字段的值</td>
  </tr>
 </tbody>
</table>

<h2 id="方法">方法</h2>

<h3 id="add">add()</h3>

<p>返回一个IDBRequest对象,并且在新线程中克隆一个值,该值存储在表中。</p>

<p>想知道是否成功添加数据,可以在事务的complete事件中进行监听,而不是success,因为事务在success事件之后还有可能失败。</p>

<p>add方法只能插入数据。如果以key参数作为某记录的关键字,并且该条记录已存在,则其所返回的请求对象会产生ConstrainError错误。</p>

<pre><a href="/en/IndexedDB/IDBRequest" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="en/IndexedDB/IDBRequest">IDBRequest</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> </span><a href="#add" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="#add">add</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> (in any value, in optional any key) raises (</span><a href="/En/DOM/DOMException" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="en/DOM/DOMException">DOMException</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;">);</span></pre>

<h5 id="参数">参数</h5>

<dl>
 <dt>value</dt>
 <dd>被存储的值。</dd>
 <dt>key</dt>
 <dd>标识某条记录的键,如果不指定,它会被设为null。</dd>
</dl>

<h5 id="返回">返回</h5>

<dl>
 <dt><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a></dt>
 <dd>一个请求对象,可以在其中绑定事件。</dd>
</dl>

<h5 id="异常">异常</h5>

<p>该方法会抛出DOMError类型的DOMException异常。</p>

<table class="standard-table" style="font-size: 14px;">
 <thead>
  <tr>
   <th scope="col">Exception</th>
   <th scope="col">Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>ReadOnlyError</td>
   <td>The transaction associated with this operation is in read-only <a href="/en-US/docs/IndexedDB/IDBTransaction#mode_constants" title="/en-US/docs/IndexedDB/IDBTransaction#mode_constants">mode</a>.</td>
  </tr>
  <tr>
   <td>TransactionInactiveError</td>
   <td>This IDBObjectStore's transaction is inactive.</td>
  </tr>
  <tr>
   <td><span style="line-height: normal;">DataError</span></td>
   <td>
    <p>Any of the following conditions apply:</p>

    <ul>
     <li>The object store uses in-line keys or has a key generator, and a key parameter was provided.</li>
     <li>The object store uses out-of-line keys and has no key generator, and no key parameter was provided.</li>
     <li>The object store uses in-line keys but no key generator, and the object store's key path does not yield a valid key.</li>
     <li>The key parameter was provided but does not contain a valid key.</li>
    </ul>
   </td>
  </tr>
  <tr>
   <td><code>InvalidStateError</code></td>
   <td>The IDBObjectStore has been deleted or removed.</td>
  </tr>
  <tr>
   <td><code>DataCloneError</code></td>
   <td>The data being stored could not be cloned by the internal structured cloning algorithm.</td>
  </tr>
 </tbody>
</table>

<h3 id="clear">clear()</h3>

<p>创建并立即返回一个 <a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> 对象, 并且在一个单独的线程中清除这个对象存储. 清除对象存储包括从对象存储中删除所有的记录,并删除对象存储引用的索引中的所有记录。</p>

<pre><a href="/en/IndexedDB/IDBRequest" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="en/IndexedDB/IDBRequest">IDBRequest</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> </span><a href="#clear" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="#clear">clear</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> () raises (</span><a href="/en-US/docs/DOM/DOMException" style="font-family: 'Courier New', 'Andale Mono', monospace;" title="/en-US/docs/DOM/DOMException">DOMException</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;">);</span></pre>

<h5 id="Returns">Returns</h5>

<dl>
 <dt><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a></dt>
 <dd>返回一个request对象,在其上触发与操作相关的事件。</dd>
</dl>

<h5 id="Exceptions">Exceptions</h5>

<p>此方法可能会引发domException,其中domError的类型如下:</p>

<table class="standard-table" style="font-size: 14px;">
 <thead>
  <tr>
   <th scope="col">Exception</th>
   <th scope="col">Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>ReadOnlyError</td>
   <td>The transaction associated with this operation is in read-only <a href="/en-US/docs/IndexedDB/IDBTransaction#mode_constants" title="/en-US/docs/IndexedDB/IDBTransaction#mode_constants">mode</a>.</td>
  </tr>
  <tr>
   <td>TransactionInactiveError</td>
   <td>This IDBObjectStore's transaction is inactive.</td>
  </tr>
 </tbody>
</table>

<h3 id="count">count()</h3>

<p>立即返回一个 <a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> 对象,并在新线程中计算符合条件的对象的数量,该方法的参数可以是键,或键范围(key range)。在 <a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> 对象中,source属性就是IDBObjectStore对象,result属性持有计算后的数量值。如果参数非法将会抛出异常。</p>

<pre><code style="font-size: 14px; color: rgb(51, 51, 51);"><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> <a href="/en/IndexedDB/IDBObjectStore#count" title="en/IndexedDB/IDBObjectStore#count">count</a> (in optional any key) raises</code><code style="font-size: 14px; color: rgb(51, 51, 51);">(<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></pre>

<h5 id="参数_2">参数</h5>

<dl>
 <dt>key</dt>
 <dd>计算被该键或键范围(key range)所标识的记录数。</dd>
</dl>

<h5 id="Returns_2">Returns</h5>

<dl>
 <dt><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a></dt>
 <dd>一个请求对象,可绑定事件。</dd>
</dl>

<h5 id="异常_2">异常</h5>

<p>该方法会引发如下异常:</p>

<table class="standard-table" style="font-size: 14px;">
 <thead>
  <tr>
   <th scope="col">Exception</th>
   <th scope="col">Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>TransactionInactiveError</td>
   <td>事务已闲置</td>
  </tr>
  <tr>
   <td>DataError</td>
   <td>
    <p>key参数非法</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">InvalidStateError</code></td>
   <td>IDBObjectStore对象已被删除</td>
  </tr>
 </tbody>
</table>

<h3 id="createIndex">createIndex()</h3>

<dl>
</dl>

<p>创建并返回新的IDBIndex对象,该方法只能从versionchange事务模式的回调方法中被调用。</p>

<pre><code style="font-size: 14px; color: rgb(51, 51, 51);"><a href="/en/IndexedDB/IDBIndex" title="en/IndexedDB/IDBIndex">IDBIndex</a> <a href="#createIndex" title="#createIndex">createIndex</a>  (in <a href="/En/DOM/DOMString" title="en/DOM/DOMString">DOMString</a> name, in </code><code style="font-size: 14px; color: rgb(51, 51, 51);"><a href="/En/DOM/DOMString" title="en/DOM/DOMString">DOMString</a></code><code style="font-size: 14px; color: rgb(51, 51, 51);"> keyPath, in optional boolean unique) raises (<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></pre>

<h5 id="Parameters">Parameters</h5>

<dl>
 <dt>name</dt>
 <dd>The name of the index to create.</dd>
 <dt>keyPath</dt>
 <dd>The key path for the index to use.</dd>
 <dt>optionalParameters</dt>
 <dd>
 <p class="warning">The IDBIndexParameters object whose attributes are optional parameters to the method. It includes the following properties:</p>

 <table class="standard-table">
  <thead>
   <tr>
    <th scope="col">Attribute</th>
    <th scope="col">Description</th>
   </tr>
  </thead>
  <tbody>
   <tr>
    <td><code>unique</code></td>
    <td>If true, the index will not allow duplicate values for a single key.</td>
   </tr>
   <tr>
    <td><code>multiEntry</code></td>
    <td>If true, the index will add an entry in the index for each array element when the <em>keypath</em> resolves to an Array. If false, it will add one single entry containing the Array.</td>
   </tr>
  </tbody>
 </table>
 </dd>
</dl>

<h5 id="Returns_3">Returns</h5>

<dl>
 <dt><a href="/en/IndexedDB/IDBIndex" title="en/IndexedDB/IDBIndex">IDBIndex</a></dt>
 <dd>The newly created index.</dd>
</dl>

<h5 id="Exceptions_2">Exceptions</h5>

<p>This method may raise a <a href="/en-US/docs/DOM/DOMException" title="/en-US/docs/DOM/DOMException">DOMException</a> with a <a href="/en-US/docs/DOM/DOMError" title="/en-US/docs/DOM/DOMError">DOMError</a> of the following types:</p>

<table class="standard-table" style="font-size: 14px;">
 <thead>
  <tr>
   <th scope="col">Exception</th>
   <th scope="col">Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code style="font-size: 14px;">InvalidStateError</code></td>
   <td>The IDBObjectStore has been deleted or removed or the method was not called from a <code><a href="/en-US/docs/IndexedDB/IDBTransaction#VERSION_CHANGE" title="/en-US/docs/IndexedDB/IDBTransaction#VERSION_CHANGE">versionchange</a></code> transaction mode callback.</td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">ConstraintError</code></td>
   <td>An index with the same name (case-sensitive) already exists in the database.<br>
     </td>
  </tr>
 </tbody>
</table>

<h3 id="delete">delete()</h3>

<dl>
</dl>

<p>Immediately returns an <code><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a></code> object, and removes the records specified by the given key or key range from this object store, and any indexes that reference it, in a separate thread.</p>

<pre><code style="font-size: 14px; color: rgb(51, 51, 51);"><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> <a href="#delete" title="#delete">delete</a> (in any key) raises </code><code style="font-size: 14px; color: rgb(51, 51, 51);">(<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></pre>

<h5 id="Parameters_2">Parameters</h5>

<dl>
 <dt>key</dt>
 <dd>The key or key range that identifies the records.</dd>
</dl>

<h5 id="Returns_4">Returns</h5>

<dl>
 <dt><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a></dt>
 <dd>A request object on which subsequent events related to this operation are fired. As <a href="http://www.w3.org/TR/IndexedDB/#object-store-deletion-operation">per spec</a> the result of the Object Store Deletion Operation algorithm is <code>undefined</code>, so it's not possible to know if some records were actually deleted by looking at the request result.</dd>
</dl>

<h5 id="Exceptions_3">Exceptions</h5>

<p>This method may raise a <a href="/en-US/docs/DOM/DOMException" title="/en-US/docs/DOM/DOMException">DOMException</a> with a <a href="/en-US/docs/DOM/DOMError" title="/en-US/docs/DOM/DOMError">DOMError</a> of the following types:</p>

<table class="standard-table" style="font-size: 14px;">
 <thead>
  <tr>
   <th scope="col">Exception</th>
   <th scope="col">Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>TransactionInactiveError</td>
   <td>This IDBObjectStore's transaction is inactive.</td>
  </tr>
  <tr>
   <td>ReadOnlyError</td>
   <td>
    <p>The transaction associated with this operation is in read-only <a href="/en-US/docs/IndexedDB/IDBTransaction#mode_constants" title="/en-US/docs/IndexedDB/IDBTransaction#mode_constants">mode</a>.</p>
   </td>
  </tr>
  <tr>
   <td><code>DataError</code></td>
   <td>The key or key range provided contains an invalid key.</td>
  </tr>
 </tbody>
</table>

<dl>
</dl>

<div class="note">
<p>If the key that identifies the record is a Number, the key passed to the delete method must be a Number too, and not a String. So for example you might need to do the following:</p>

<pre class="brush: js">var key_val = '42';
var key = Number(key_val);
objectstore.delete(key);</pre>
</div>

<h3 id="deleteIndex">deleteIndex()</h3>

<p>Destroys the index with the specified name in the connected database. Note that this method must be called only from a <code><a href="/en/IndexedDB/IDBTransaction#VERSION_CHANGE" title="en/IndexedDB/IDBTransaction#VERSION CHANGE">VersionChange</a></code> transaction mode callback. Note that this method synchronously modifies the <a href="#attr_indexNames" title="#attr_indexNames">indexNames</a> property.</p>

<pre><code style="font-size: 14px; color: rgb(51, 51, 51);">void <a href="#deleteIndex" title="#deleteIndex">deleteIndex</a> (in any </code><code style="font-size: 14px; color: rgb(51, 51, 51);"><a href="/En/DOM/DOMString" title="en/DOM/DOMString">DOMString</a></code><code style="font-size: 14px; color: rgb(51, 51, 51);"> indexName) raises (<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></pre>

<h5 id="Parameters_3">Parameters</h5>

<dl>
 <dt>indexName</dt>
 <dd>The name of the existing index to remove.</dd>
 <dt>Returns</dt>
 <dd>Void</dd>
</dl>

<h5 id="Exceptions_4">Exceptions</h5>

<p>This method may raise a <a href="/en-US/docs/DOM/DOMException" title="/en-US/docs/DOM/DOMException">DOMException</a> with a <a href="/en-US/docs/DOM/DOMError" title="/en-US/docs/DOM/DOMError">DOMError</a> of the following types:</p>

<table class="standard-table" style="font-size: 14px;">
 <thead>
  <tr>
   <th scope="col">Exception</th>
   <th scope="col">Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code style="font-size: 14px;">InvalidStateError</code></td>
   <td>The method was not called from a <code style="font-size: 14px;"><a href="/en-US/docs/IndexedDB/IDBTransaction#VERSION_CHANGE" title="/en-US/docs/IndexedDB/IDBTransaction#VERSION_CHANGE">versionchange</a></code> transaction mode callback.</td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">NotFoundError</code></td>
   <td>There is no index with the given name (case-sensitive) in the database.</td>
  </tr>
 </tbody>
</table>

<h3 id="get">get()</h3>

<p>Immediately returns an <a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> object, and retrieves the requested record from the object store in a separate thread. If the operation is successful, then a success event is fired on the returned object, with its <code><a href="/en/IndexedDB/IDBSuccessEvent#attr_result" title="en/IndexedDB/IDBSuccessEvent#attr result">result</a></code> set to the retrieved value, and <code><a href="/en/IndexedDB/IDBTransactionEvent#attr_transaction" title="en/IndexedDB/IDBTransactionEvent#attr transaction">transaction</a></code> set to the transaction in which this object store is opened. </p>

<pre><a href="/en/IndexedDB/IDBRequest" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="en/IndexedDB/IDBRequest">IDBRequest</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> </span><a href="#get" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="#get">get</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> (in any key) raises (</span><a href="/en-US/docs/DOM/DOMException" style="font-family: 'Courier New', 'Andale Mono', monospace;" title="/en-US/docs/DOM/DOMException">DOMException</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;">);</span></pre>

<p>{{ Note("This function produces the same result if no record with the given key exists in the database as when a record exists, but with an <code>undefined</code> value. To tell these situations apart, call the openCursor() method with the same key. That method provides a cursor if the record exists, and no cursor if it does not.") }}</p>

<h5 id="Parameters_4">Parameters</h5>

<dl>
 <dt>key</dt>
 <dd>The key or key range identifying the record to retrieve. In the case of a key range, the record returned is the first record associated with the first key in the range.</dd>
</dl>

<h5 id="Returns_5">Returns</h5>

<dl>
 <dt><code><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a></code></dt>
 <dd>A request object on which subsequent events related to this operation are fired.</dd>
</dl>

<h5 id="Exceptions_5">Exceptions</h5>

<p>This method may raise a <a href="/en-US/docs/DOM/DOMException" title="/en-US/docs/DOM/DOMException">DOMException</a> with a <a href="/en-US/docs/DOM/DOMError" title="/en-US/docs/DOM/DOMError">DOMError</a> of the following types:</p>

<table class="standard-table" style="font-size: 14px;">
 <thead>
  <tr>
   <th scope="col">Exception</th>
   <th scope="col">Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>TransactionInactiveError</td>
   <td>This IDBObjectStore's transaction is inactive.</td>
  </tr>
  <tr>
   <td>DataError</td>
   <td>
    <p>The key or key range provided contains and invalid key.</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">InvalidStateError</code></td>
   <td>The IDBObjectStore has been deleted or removed.<br>
     </td>
  </tr>
 </tbody>
</table>

<h3 id="index">index()</h3>

<p>Opens the named index in this object store.</p>

<pre><code style="font-size: 14px; color: rgb(51, 51, 51);"><a href="/en/IndexedDB/IDBIndex" title="en/IndexedDB/IDBIndex">IDBIndex</a> <a href="#index" title="#index">index</a> (in </code><code style="font-size: 14px; color: rgb(51, 51, 51);"><a href="/En/DOM/DOMString" title="en/DOM/DOMString">DOMString</a></code><code style="font-size: 14px; color: rgb(51, 51, 51);"> name) raises (<a href="/en-US/docs/DOM/DOMException" style="line-height: 18px;" title="/en-US/docs/DOM/DOMException">DOMException</a>);</code></pre>

<h5 id="Parameters_5">Parameters</h5>

<dl>
 <dt>name</dt>
 <dd>The name of the index to open.</dd>
</dl>

<h5 id="Returns_6">Returns</h5>

<dl>
 <dt><code><a href="/en/IndexedDB/IDBIndex" title="en/IndexedDB/IDBIndex">IDBIndex</a></code></dt>
 <dd>An object for accessing the index.</dd>
</dl>

<h5 id="Exceptions_6">Exceptions</h5>

<p>This method may raise a <a href="/en-US/docs/DOM/DOMException" title="/en-US/docs/DOM/DOMException">DOMException</a> with a <a href="/en-US/docs/DOM/DOMError" title="/en-US/docs/DOM/DOMError">DOMError</a> of the following types:</p>

<table class="standard-table" style="font-size: 14px;">
 <thead>
  <tr>
   <th scope="col">Exception</th>
   <th scope="col">Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code style="font-size: 14px;">InvalidStateError</code></td>
   <td>The source object store has been deleted, or the transaction for the object store has finished.</td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">NotFoundError</code></td>
   <td>There is no index with the given name (case-sensitive) in the database.<br>
     </td>
  </tr>
 </tbody>
</table>

<h3 id="openCursor">openCursor()</h3>

<p>Immediately returns an <a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> object, and creates a <a href="/en-US/docs/IndexedDB/IDBCursor" title="en/IndexedDB#gloss cursor">cursor</a> over the records in this object store, in a separate thread. If there is even a single record that matches the <a href="/en/IndexedDB#gloss_key_range" title="en/IndexedDB#gloss key range">key range</a>, then a success event is fired on the returned object, with its <code><a href="/en/IndexedDB/IDBSuccessEvent#attr_result" title="en/IndexedDB/IDBSuccessEvent#attr result">result</a></code> set to the <a href="/en/IndexedDB/IDBCursor" title="en/IndexedDB/IDBCursor">IDBCursor</a> object for the new cursor. If no records match the key range, then a success event is fired on the returned object, with its <code><a href="/en/IndexedDB/IDBSuccessEvent#attr_result" title="en/IndexedDB/IDBSuccessEvent#attr result">result</a></code> set to null.</p>

<pre><a href="/en/IndexedDB/IDBRequest" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="en/IndexedDB/IDBRequest">IDBRequest</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> </span><a href="#openCursor" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="#openCursor">openCursor</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> (in optional </span><a href="/en/IndexedDB/IDBKeyRange" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="en/IndexedDB/IDBKeyRange">IDBKeyRange</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> range, in optional unsigned short direction) raises(</span><a href="/en-US/docs/DOM/DOMException" style="font-family: 'Courier New', 'Andale Mono', monospace;" title="/en-US/docs/DOM/DOMException">DOMException</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;">);</span></pre>

<h5 id="Parameters_6">Parameters</h5>

<dl>
 <dt>range</dt>
 <dd>The key range to use as the cursor's range. If this parameter is unspecified or null, then the range includes all the records in the object store.</dd>
 <dt>direction</dt>
 <dd>The cursor's <a href="/en-US/docs/IndexedDB/IDBCursor" title="/en-US/docs/IndexedDB/IDBCursor">direction</a>.</dd>
</dl>

<h5 id="Returns_7">Returns</h5>

<dl>
 <dt><code><a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a></code></dt>
 <dd>A request object on which subsequent events related to this operation are fired.</dd>
</dl>

<h5 id="Exceptions_7">Exceptions</h5>

<p>This method may raise a <a href="/en-US/docs/DOM/DOMException" title="/en-US/docs/DOM/DOMException">DOMException</a> with a <a href="/en-US/docs/DOM/DOMError" title="/en-US/docs/DOM/DOMError">DOMError</a> of the following types:</p>

<table class="standard-table" style="font-size: 14px;">
 <thead>
  <tr>
   <th scope="col">Exception</th>
   <th scope="col">Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>TransactionInactiveError</td>
   <td>This IDBObjectStore's transaction is inactive.</td>
  </tr>
  <tr>
   <td>DataError</td>
   <td>
    <p>The key or key range provided contains and invalid key.</p>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">InvalidStateError</code></td>
   <td>The IDBObjectStore has been deleted or removed.</td>
  </tr>
  <tr>
   <td><code>TypeError</code></td>
   <td>The value of the <code>direction</code> parameter is invalid.</td>
  </tr>
 </tbody>
</table>

<h3 id="put">put()</h3>

<p>Returns an <a href="/en/IndexedDB/IDBRequest" title="en/IndexedDB/IDBRequest">IDBRequest</a> object, and, in a separate thread, creates a <a class="external" href="http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-interfaces.html#structured-clone">structured clone</a> of the <code>value</code>, and stores the cloned value in the object store. If the record is successfully stored, then a success event is fired on the returned request object with the <code><a href="/en-US/docs/IndexedDB/IDBRequest" title="en/IndexedDB/IDBSuccessEvent#attr result">result</a></code> set to the key for the stored record, and <code><a href="/en-US/docs/IndexedDB/IDBRequest" title="en/IndexedDB/IDBTransactionEvent#attr transaction">transaction</a></code> set to the transaction in which this object store is opened.</p>

<p>The put method is an<span class="database"> <em>update or insert</em> </span>method. See also the <a href="#add()">add()</a> method.</p>

<pre><a href="/en/IndexedDB/IDBRequest" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="en/IndexedDB/IDBRequest">IDBRequest</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> </span><a href="#put" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="#put">put</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;"> (in any value, in optional any key) raises (</span><a href="/En/DOM/DOMException" style="font-family: 'Courier New', 'Andale Mono', monospace; line-height: normal;" title="en/DOM/DOMException">DOMException</a><span style="font-family: 'Courier New','Andale Mono',monospace; line-height: normal;">);</span></pre>

<h5 id="Parameters_7">Parameters</h5>

<dl>
 <dt>value</dt>
 <dd>The value to be stored.</dd>
 <dt>key</dt>
 <dd>The key to use to identify the record. If unspecified, it results to null.</dd>
</dl>

<h5 id="Returns_8">Returns</h5>

<dl>
 <dt>IDBRequest</dt>
 <dd>A request object on which subsequent events related to this operation are fired.</dd>
</dl>

<h5 id="Exceptions_8">Exceptions</h5>

<p>This method may raise a <a href="/en-US/docs/DOM/DOMException" title="/en-US/docs/DOM/DOMException">DOMException</a> with a <a href="/en-US/docs/DOM/DOMError" title="/en-US/docs/DOM/DOMError">DOMError</a> of the following types:</p>

<table class="standard-table" style="font-size: 14px;">
 <thead>
  <tr>
   <th scope="col">Exception</th>
   <th scope="col">Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>ReadOnlyError</td>
   <td>The transaction associated with this operation is in read-only <a href="/en-US/docs/IndexedDB/IDBTransaction#mode_constants" title="/en-US/docs/IndexedDB/IDBTransaction#mode_constants">mode</a>.</td>
  </tr>
  <tr>
   <td>TransactionInactiveError</td>
   <td>This IDBObjectStore's transaction is inactive.</td>
  </tr>
  <tr>
   <td>DataError</td>
   <td>
    <p>Any of the following conditions apply:</p>

    <ul>
     <li>The object store uses in-line keys or has a key generator, and a key parameter was provided.</li>
     <li>The object store uses out-of-line keys and has no key generator, and no key parameter was provided.</li>
     <li>The object store uses in-line keys but no key generator, and the object store's key path does not yield a valid key.</li>
     <li>The key parameter was provided but does not contain a valid key.</li>
    </ul>
   </td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">InvalidStateError</code></td>
   <td>The IDBObjectStore has been deleted or removed.</td>
  </tr>
  <tr>
   <td><code style="font-size: 14px;">DataCloneError</code></td>
   <td>The data being stored could not be cloned by the internal structured cloning algorithm.</td>
  </tr>
 </tbody>
</table>

<h2 id="Example">Example</h2>

<dl>
</dl>

<dl>
</dl>

<p>This example shows a variety of different uses of object stores, from updating the data structure with {{domxref("IDBObjectStore.createIndex")}} inside an <code>onupgradeneeded</code>function, to adding a new item to our object store with {{domxref("IDBObjectStore.add")}}. For a full working example, see our <a href="https://github.com/mdn/to-do-notifications/">To-do Notifications</a> app (<a href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</p>

<pre><code>// Let us open our database
var DBOpenRequest = window.indexedDB.open("toDoList", 4);

DBOpenRequest.onsuccess = function(event) {
  note.innerHTML += '&lt;li&gt;Database initialised.&lt;/li&gt;';

  // store the result of opening the database in db.
  db = DBOpenRequest.result;
};

// This event handles the event whereby a new version of
// the database needs to be created Either one has not
// been created before, or a new version number has been
// submitted via the window.indexedDB.open line above
DBOpenRequest.onupgradeneeded = function(event) {
  var db = event.target.result;

  db.onerror = function(event) {
    note.innerHTML += '&lt;li&gt;Error loading database.&lt;/li&gt;';
  };

  // Create an objectStore for this database

  var objectStore = db.createObjectStore("toDoList", { keyPath: "taskTitle" });

  // define what data items the objectStore will contain

  objectStore.createIndex("hours", "hours", { unique: false });
  objectStore.createIndex("minutes", "minutes", { unique: false });
  objectStore.createIndex("day", "day", { unique: false });
  objectStore.createIndex("month", "month", { unique: false });
  objectStore.createIndex("year", "year", { unique: false });

  objectStore.createIndex("notified", "notified", { unique: false });

  note.innerHTML += '&lt;li&gt;Object store created.&lt;/li&gt;';
};

// Create a new item to add in to the object store
var newItem = [
  { taskTitle: "Walk dog", hours: 19, minutes: 30, day: 24, month: 'December', year: 2013, notified: "no" }
];

// open a read/write db transaction, ready for adding the data
var transaction = db.transaction(["toDoList"], "readwrite");

// report on the success of the transaction completing, when everything is done
transaction.oncomplete = function(event) {
  note.innerHTML += '&lt;li&gt;Transaction completed.&lt;/li&gt;';
};

transaction.onerror = function(event) {
  note.innerHTML += '&lt;li&gt;Transaction not opened due to error. Duplicate items not allowed.&lt;/li&gt;';
};

// create an object store on the transaction
var objectStore = transaction.objectStore("toDoList");
// make a request to add our newItem object to the object store
var objectStoreRequest = objectStore.add(newItem[0]);

objectStoreRequest.onsuccess = function(event) {
  note.innerHTML += '&lt;li&gt;Request successful .&lt;/li&gt;';
}</code></pre>

<h2 id="Specifications">Specifications</h2>

<table>
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('IndexedDB', '#idl-def-IDBObjectStore', 'IDBObjectStore')}}</td>
   <td>{{Spec2('IndexedDB')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName("IndexedDB 2", "#object-store-interface", "IDBObjectStore")}}</td>
   <td>{{Spec2("IndexedDB 2")}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">Browser compatibility</h2>

<p>The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a>and send us a pull request.</p>

<p>{{Compat("api.IDBObjectStore")}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">Using IndexedDB</a></li>
 <li>Starting transactions: {{domxref("IDBDatabase")}}</li>
 <li>Using transactions: {{domxref("IDBTransaction")}}</li>
 <li>Setting a range of keys: {{domxref("IDBKeyRange")}}</li>
 <li>Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}</li>
 <li>Using cursors: {{domxref("IDBCursor")}}</li>
 <li>Reference example: <a href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li>
</ul>

<div id="compat-mobile"></div>