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
11
votes
1 answer

Can we use IndexedDB between two pages on different domains?

I have created a IndexdDB object store in my one page (Let object store name is "ShopStore"). Now I want to open the same object store from a diffirent page. Is it possible? My two web pages are on different domain.
rohit gora
  • 113
  • 1
  • 5
11
votes
1 answer

What data types does IndexedDb support?

I have searched, but can't seem to find anything about the data types that IndexedDB supports. I know that it supports basic Javascript objects, but what if I want to store an instance of an object or a blob of data? Does IndexedDB support anything…
tribute2ro
  • 340
  • 4
  • 14
11
votes
3 answers

Alternative to IndexedDB in not supported browsers? Safari/iOS Saf-Chrome

I have a working sample with IndexedDB that works perfect for my Desktop Chrome. Nevertheless, my main aim is to develop for iOS devices (Chrome-Safari) and this API isn't available there yet. What should I do? I have seen this Polyfill:…
the_moon
  • 553
  • 1
  • 7
  • 21
11
votes
3 answers

Indexed DB cursor ranges on multiple properties

I have a composite index of two properties on an indexeddb objectstore and wish to retrieve a cursor based on the range of both of these properties. Here's an example object in the store : {species: 'Oak',xcoord: 123456, ycoord: 654321} and index…
kes
  • 320
  • 3
  • 10
11
votes
4 answers

Check if IndexedDB objectStore already contains key

Adding an object to an IndexedDB objectStore will fail if the key already exists. How can I check for the existence of an object with a given key – preferably synchronously (no reason for another layer of callbacks) and without pulling the object. I…
Chris
  • 11,819
  • 19
  • 91
  • 145
10
votes
4 answers

How can I run offline database usage in Blazor WebAssembly-PWA?

I have a Blazor WebAssembly ASP.NET Core hosted - PWA application and want to run it offline. The database is currently built with SQLite and EF-Core. Is it possible to add offline functionality? I have read about IndexedDB but don't actually know…
brstkr
  • 1,423
  • 3
  • 18
  • 28
10
votes
1 answer

Microtasks inside Web Workers

The distinction between tasks and microtasks is important because IndexedDB transactions commit across tasks, but not microtasks. This is problematic when wrapping IndexedDB code in Promises, because in Firefox (and maybe other browsers), promise…
dumbmatter
  • 9,351
  • 7
  • 41
  • 80
10
votes
2 answers

Should I open an IDBDatabase each time or keep one instance open?

I have a SPA application that will make multiple reads/writes to IndexedDB. Opening the DB is an asynchronous operation with a callback: var db; var request = window.indexedDB.open("MyDB", 2); request.onupgradeneeded = function(event) { //…
Keith
  • 150,284
  • 78
  • 298
  • 434
10
votes
4 answers

How to apply expiry times to keys in HTML5 indexeddb?

In indexedDB in html5 api, I can use it to store key-value pairs. But how can I make sure that after adding a certain key-value, after 1 day, that key should automatically be deleted from the db. I was thinking of wrapping the value in an object…
omega
  • 40,311
  • 81
  • 251
  • 474
10
votes
1 answer

Why does calling indexedDB.deleteDatabase prevent me from making any further transactions?

If I go into my browser's console (I'm using Chrome) right now, on this very page, and type indexedDB.open("MyDB").onsuccess = function(e) { console.log("success"); }; I immediately get a "success" message in my console. I can do this as many times…
Jack M
  • 4,769
  • 6
  • 43
  • 67
10
votes
1 answer

IndexedDB User Permissions

Good day, all I read on this page [Using IndexedDB]: https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB that browser will prompt user to give permission to create indexedDB, and the user may deny permission. See quote…
BigName
  • 1,018
  • 2
  • 14
  • 29
10
votes
1 answer

IndexedDB Performance and IndexedDB v/s WebSQL performance comparison

WebSQL and IndexedDB are both DB API for accessing (CRUD) the underlying embedded database in the web browser. Which, if I am correct, is like SQL for accessing (CRUD) any client-server database like Oracle etc. (in many case support for both WebSQL…
hagrawal7777
  • 14,103
  • 5
  • 40
  • 70
10
votes
2 answers

What is the per-record size limit of indexedDB?

I am building a file storage for HTML5, and I am using indexedDB as the storage, I ask the files from the server via xmlHttpRequest with the response type as arrayBuffer (for chrome) and blob (for other browsers). Everything is fine even if the…
Eldon Lesley
  • 915
  • 6
  • 19
  • 38
10
votes
3 answers

Counting the number of records in an object store in indexedDB

I want to basic count the number of records in my indexedDB database. Currently my code looks like Javascript var transaction = db.transaction(["data"], "readonly"); var objectStore = transaction.objectStore("data"); var cursor =…
Brent
  • 2,385
  • 10
  • 39
  • 63
10
votes
1 answer

Is it bad to open several database connections in indexedDB?

I have been working with IndexedDB for a bit now and I can successfully create a new database, create a store, and add a value during the "on upgrade needed". What I don't understand is, does the database remain "open" or do you have to re-open it…
Garfonzo
  • 3,876
  • 6
  • 43
  • 78