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
23
votes
2 answers

IndexedDB: Store file as File or Blob or ArrayBuffer. What is the best option?

Now most of browsers are supporting IndexedDB to store data/file directly as File, Blob or ArrayBuffer. This code saves a IDB key 'File1' as File var a = document.getElementById("userfile"); var b =…
Ankit_Shah55
  • 799
  • 2
  • 9
  • 17
23
votes
3 answers

How to make offline maps(using leaflet OSM) , by caching?

I am trying to make offline maps through caching (IndexedDB) in browser. I understand the concept is that I download and store the tiles of map first when connected to internet. Then I have to load the tiles logically offline. However, I'm not able…
Abhi Ram A
  • 305
  • 2
  • 4
  • 10
22
votes
1 answer

Preventing UI flicker when loading async data in React.js

I have some data in IndexedDB, which can only be accessed asynchronously. I want to build a React.js UI using that data. The general idea is that I'll have multiple React components that load data from IndexedDB and display some UI based on that…
dumbmatter
  • 9,351
  • 7
  • 41
  • 80
22
votes
4 answers

IndexedDB view all Databases and Object Stores

I'm using IndexedDB in a Windows 8 app and I'm very new to both. I've been able to successfully create, read, update, delete objects from object stores, and have created a couple databases and a few object stores. My question is how can I list all…
skinneejoe
  • 3,921
  • 5
  • 30
  • 45
21
votes
4 answers

Uncaught InvalidStateError: Failed to execute 'transaction' on 'IDBDatabase': A version change transaction is running

i must admit that i am very new to indexedDB I wrote a simple code of indexedDB and it is as followed: function go() { var req = window.indexedDB.open("Uploader", 1), db; req.onerror = function (e) { console.log("Error"); }; …
anni saini
  • 363
  • 1
  • 2
  • 8
21
votes
4 answers

Synchronizing MongoDB server data to an IndexedDB local store

I'm trying to evaluate using IndexedDB to solve the offline issue. It would be populated with data currently stored in a MongoDB database (as is). Once data is stored in IndexedDB, it may be changed on the MongoDB server and I need to propagate…
Philippe Girolami
  • 1,876
  • 1
  • 13
  • 15
21
votes
4 answers

Inserting large quantities in IndexedDB's objectstore blocks UI

I want to save some ~35000 objects in my IndexedDB's objectstore. I am using below code to insert. AddListings = function (x2j_list_new, callback) { var transaction = db.transaction(["listings"], IDBTransaction.READ_WRITE); var count =…
surya
  • 1,351
  • 1
  • 13
  • 29
21
votes
4 answers

How do you keep an indexeddb transaction alive?

Instead of opening several transactions (read a table, write to a table, write to another table, etc) is it possible to do this all from a single transaction as long as you are using an appropriate IDBTransaction? Mozilla says: "The only way to keep…
anonymous
  • 1,259
  • 2
  • 10
  • 15
20
votes
3 answers

Exporting and importing IndexedDB data

I'm making a tool for my own use that needs a simple database. This seems like a good chance to learn the HTML5 IndexedDB API, but it's important that I don't lose data at any point. I suppose backing up the browser's profile directory would do for…
JJJ
  • 32,902
  • 20
  • 89
  • 102
20
votes
3 answers

Why does indexedDB use a "version"?

I'm just learning about indexedDB, and this is my understanding of setting up a database. You call .open(dbName) to get a DB instance. If no database of this name exists on the user's computer (for instance if this is their first visit to the site)…
Jack M
  • 4,769
  • 6
  • 43
  • 67
20
votes
1 answer

Why IndexedDB is not available in node.js?

IndexedDB API on node.js will be very useful for some app. Why IndexedDB is not available in node.js? IndexedDB API does not depend on DOM, even though it does use some DOM error and event style.
Kyaw Tun
  • 12,447
  • 10
  • 56
  • 83
19
votes
1 answer

Locking model for IndexedDB?

How does IndexedDB handle multiple tabs each with asynchronous transactions in-flight? Do transactions lock all of the related object stores entirely? How can I guarantee that if one tab is working on a piece of data that another isn't doing the…
Ben Dilts
  • 10,535
  • 16
  • 54
  • 85
19
votes
1 answer

Electron IndexedDb limit?

In this Electron issue, @zcbenz commented: We have the same size limitation with Chrome browser, which is '1/3 of the of available disk space'. That response was from early 2016. I've run this code: const estimation = await…
TimTheEnchanter
  • 3,370
  • 1
  • 26
  • 47
19
votes
1 answer

indexedDB Creating a database and adding content Failed to execute 'transaction' on 'IDBDatabase'

This is the first time for me to use indexDB, I've created a database and now trying to add content to it. But I'm getting the following error. Uncaught NotFoundError: Failed to execute 'transaction' on 'IDBDatabase': One of the specified object…
Ryan
  • 271
  • 2
  • 3
  • 7
19
votes
3 answers

Deleting multiple records in IndexedDB based on index

I am using IndexedDB and I have two object stores: equip (represents different equipment, primary key tagNo) and equipParts (represents the parts of a piece of equipment and has an index which is based on the tag number/serial number, primary key…
user2727708
  • 193
  • 1
  • 1
  • 4