This is the Third Edition of Indexed Database API. The First Edition, simply titled "Indexed Database API", became a W3C Recommendation on 8 January 2015. The Second Edition, titled "Indexed Database API 2.0", became a W3C Recommendation on 30 January 2018.
Indexed Database API 3.0 is intended to supersede Indexed Database API 2.0.
1. Introduction
User agents need to store large numbers of objects locally in order to satisfy off-line data requirements of Web applications. [WEBSTORAGE] is useful for storing pairs of keys and their corresponding values. However, it does not provide in-order retrieval of keys, efficient searching over values, or storage of duplicate values for a key.
This specification provides a concrete API to perform advanced key-value data management that is at the heart of most sophisticated query processors. It does so by using transactional databases to store keys and their corresponding values (one or more per key), and providing a means of traversing keys in a deterministic order. This is often implemented through the use of persistent B-tree data structures that are considered efficient for insertion and deletion as well as in-order traversal of very large numbers of data records.
"library"
database. It has a "books"
object store that holds books records stored by their "isbn"
property as the primary key.
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 unique
option set. This index is used to look up books by title, and will prevent adding books with non-unique titles.
Book records also have an "author"
property, which is not required to be unique. The code creates another index named "by_author"
to allow look-ups by this property.
The code first opens a connection to the database. The upgradeneeded
event handler code creates the object store and indexes, if needed. The success
event handler code saves the opened connection for use in later examples.
const request= indexedDB. open( "library" ); let db; request. onupgradeneeded= function () { // The database did not previously exist, so create object stores and indexes. const db= request. result; const store= db. createObjectStore( "books" , { keyPath: "isbn" }); const titleIndex= store. createIndex( "by_title" , "title" , { unique: true }); const authorIndex= store. createIndex( "by_author" , "author" ); // Populate with initial data. store. put({ title: "Quarry Memories" , author: "Fred" , isbn: 123456 }); store. put({ title: "Water Buffaloes" , author: "Fred" , isbn: 234567 }); store. put({ title: "Bedrock Nights" , author: "Barney" , isbn: 345678 }); }; request. onsuccess= function () { db= request. result; };
The following example populates the database using a transaction.
const tx= db. transaction( "books" , "readwrite" ); const store= tx. objectStore( "books" ); store. put({ title: "Quarry Memories" , author: "Fred" , isbn: 123456 }); store. put({ title: "Water Buffaloes" , author: "Fred" , isbn: 234567 }); store. put({ title: "Bedrock Nights" , author: "Barney" , isbn: 345678 }); tx. oncomplete= function () { // All requests have succeeded and the transaction has committed. };
The following example looks up a single book in the database by title using an index.
const tx= db. transaction( "books" , "readonly" ); const store= tx. objectStore( "books" ); const index= store. index( "by_title" ); const request