Questions tagged [indexeddb]

indexedDB provides a way to store data in the browser using Javascript. Unlike relational databases, indexedDB uses a key-value store that is conceptually similar to HTML5's local storage. However, indexedDB is better than local storage for storing large amounts of data and for querying data more quickly. indexedDB is supported in IE, Chrome, Firefox, and Microsoft Edge, although support for specific features varies.

Overview

The indexedDB standard was created to enable scalable, performant storage and retrieval of Javascript objects in a browser. When using indexedDB, you create an object store, which is essentially a named collection of objects, and then can put objects into the store and later retrieve objects from the store. The store is capable of storing a "large" number of objects. You can also use an index to speed up retrieval.

Prerequisites

Before getting started with indexedDB, there are a couple key points you should consider:

First, indexedDB's API makes significant use of asynchronous functions. You should be familiar with writing asynchronous JavaScript before using indexedDB. A substantial amount of the questions on stackoverflow under the indexedDB tag relate to inexperience with asynchronous functions.

Second, indexedDB serializes objects when saving them. When you save an object in the database, the object itself is not saved. Instead, a serialized representation of the object is saved. This is comparable to using JSON.stringify together with JSON.parse. Therefore, when you retrieve the object from the store, you are retrieving this serialized representation, and not your original object. There are several ramifications of this design. This copy is not your actual object. It is a plain JavaScript object, with properties. Member functions are discarded along with object type information.

Basic concepts

  • Database: a container of object stores and indices. Every database has a name and a version.
  • Object store: a container of objects. This is analogous to a table in a relational database. In indexedDB, records correspond to Javascript objects, and columns correspond to Javascript object properties. Objects added to the store are stored in the order added. Queries against the store retrieve objects in the same order. You can insert, update, or delete objects in an object store.
  • Index: a special container for specific objects contained within an object store. Indices are also analogous to tables, and can be understood as object stores with special constraints. When an object is inserted into an object store, it may, if it meets certain criteria, also be inserted into a corresponding index store. Objects in an index are stored in an order defined by the index. Queries against an index retrieve objects in the order defined by the index (although queries can be configured to work differently). You cannot insert, update, or delete objects in an index (you can only do so indirectly by inserting the object into the store upon which the index is based).
  • Cursor: cursors are analogous to queries. A cursor iterates over the objects in either an object store or an index. Cursors can move forwards or backwards, seek (jump or advance past objects), and jump to the next or previous 'unique' object in the underlying store/index.
  • Key path: key paths are analogous to primary keys (or compound primary keys) of a table in a relational database. In the general case, when you instruct indexedDB to create an object store in a particular database, you also define the key path for the store. You can use the key path to quickly get a particular object, which is similar to using a primary key to select a record in a relational table. You can, optionally, use keys to ensure that later attempts to insert an object into an object store that already contains an object with the same key will produce an error.
  • Transactions and requests: requests are analogous to individual SQL queries. There are specific API methods for inserting an object, deleting an object, updating an object, and iterating over one or more objects. Each method call corresponds to a single request. Each request occurs within the context of a transaction. In other words, multiple requests can occur in one transaction. Individual requests can fail for a variety of reasons. When performing multiple requests in a single transaction, the requests are not fully committed until all the requests are considered successful. In this manner, if a problem occurs in a later request, the entire transaction can be "rolled back" so that the state of the underlying object store is the same as it was before the occurrence of the first request in the transaction.

Support

Visit http://caniuse.com/#feat=indexeddb.

Learn More

2343 questions
13
votes
2 answers

How to create multiple object stores in IndexedDB

I don't know if I'm right or wrong. But as I know I can't create a version change transaction manually. The only way to invoke this is by changing the version number when opening the indexed DB connection. If this is correct, in example1 and…
user1598696
  • 550
  • 1
  • 4
  • 22
12
votes
3 answers

Accessing IndexedDB from multiple javascript threads

Overview: I am trying to avoid a race condition with accessing an IndexedDB from both a webpage and a web-worker. Setup: Webpage that is saving items to the local IndexedDB as the user works with the site. Whenever a user saves data to the local…
Shawn
  • 374
  • 2
  • 14
12
votes
5 answers

IndexedDB testing with Jest & enzyme - ReferenceError: indexedDB is not defined

I'm looking for help with unit tests for my app, where I'm using indexedDB. Before I implemented indexedDB functionality, tests were correct. But now, for all of them I see one error: ReferenceError: indexedDB is not defined Can someone give me an…
Mateusz Bielak
  • 123
  • 1
  • 6
12
votes
3 answers

Delete all indexedDB

I need to delete all my IndexedDB, currently I have: const indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; if (indexedDB.webkitGetDatabaseNames) { const bases =…
Felipe Pincheira
  • 442
  • 1
  • 6
  • 21
12
votes
1 answer

Improve client-server data sync functionality with deltas

The app I have a web app that currently uses AppCache for offline functionality since users of the system need to create documents offline. The document is first created offline and when internet access is available, the user can click "sync" which…
12
votes
5 answers

IndexedDB - What is Key, keyPath and indexName?

I am coming from MySQL and I am used to the conventional database table scheme. I am having trouble understanding IndexedDB and some of its terminology. I looked up these definitions in the documentation: Key A data value by which stored values…
Walter M
  • 4,993
  • 5
  • 22
  • 29
12
votes
1 answer

"An IndexedDB transaction that was not yet complete has been aborted due to page navigation"

I'm using IndexedDB to store some data. It appears to work, but if I refresh the page, I see: An IndexedDB transaction that was not yet complete has been aborted due to page navigation. in the browser console on Firefox (36.0.4). I'm using this…
Thomas Leonard
  • 7,068
  • 2
  • 36
  • 40
12
votes
3 answers

Inconsistent interplay between IndexedDB transactions and Promises

I saw sync-promise posted on Reddit and got into a discussion with the author. We noticed some weird inconsistencies in the relationship between IndexedDB transactions and promises. IndexedDB transactions auto-commit when all the onsuccess events…
dumbmatter
  • 9,351
  • 7
  • 41
  • 80
12
votes
3 answers

IndexedDB: how to reseed/reset auto-increment key to 0 (or 1)

Simple question, but I haven't been able to find an answer. How can I reset an object store in IndexedDB so the auto-increment key starts at 0 (or 1) again? I am using IDBWrapper currently, but could use an alternative library if it is easier. I am…
Adam Marshall
  • 3,010
  • 9
  • 42
  • 80
12
votes
3 answers

Sync indexedDB with mysql database

I am about to develop an application where employees go to service repair machines at customer premises. They need to fill up a service card using a tablet or any other mobile device. In case of no Internet connection, I am thinking about using…
blaise
  • 383
  • 1
  • 5
  • 16
12
votes
2 answers

Indexeddb in Chrome on IOS - when available?

Does anyone know when or how Indexeddb will be available in Chrome on IOS? Indexeddb works well in mobile Chrome on Android devices!
x3m
  • 533
  • 5
  • 16
12
votes
2 answers

Add Index to Pre-Existing ObjectStore In IndexedDB Using Javascript

I have seen multiple JavaScript examples of using createIndex to define an ObjectStore index directly after the ObjectStore has been created like this: var objectStore = ixDb.createObjectStore(osName, { keyPath: pkName, autoIncrement: autoIncrement…
Jake Drew
  • 2,230
  • 23
  • 29
11
votes
1 answer

HTML5 Video source as locally stored blob not working anymore

As of Chrome 80, something seems to have changed in either the way Blobs or IndexedDB work. Loading a video file as a blob and assigning it to an HTML5 Video element through createObjectURL, still works: // load the blob through…
resle
  • 2,254
  • 4
  • 19
  • 37
11
votes
2 answers

IndexedDB size keeps growing even though data saved doesn't change

I am using Redux Persist and LocalForage in my web application. I have a series of actions that are fired upon login that update some data about the user. The IndexedDB size after all actions are fired and the data is saved in the JSON format on…
11
votes
5 answers

Does Edge/Safari have a limit on Indexeddb size?

Currently looking into various browsers indexeddb limits. Found that Chrome didn't have a hard limit but permissions needed to be given (Source), that Firefox was 50% of local storage (Source) but couldn't find anything for Edge or Safari. I…
Sam
  • 794
  • 1
  • 7
  • 26