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

JavaScript Promise not resolving event.target.result

I need to open transaction, read objectstore data and check if data is not undefined and if data is defined at indexedDB use it or if not fetch new data using network_ip() function. But problem is that when i use Promises, the "const r" variable is…
redevil
  • 155
  • 7
0
votes
1 answer

Uncaught TypeError: store.put is not a function at store.onsuccess

I can't update any data in IndexedDb. When I run the code it says "store.put is not a function at store.onsuccess". I tried from many browsers, same error in all I also tried .update instead of .put function but it didn't work and gave the same…
ozgurbyk
  • 75
  • 7
0
votes
1 answer

JS - How to retrieve variable after IndexedDB transaction.oncomplete() executes?

My problem is simple, but incredibly frustrating as I'm now on my second week of trying to figure this out and on the verge of giving up. I would like to retrieve my 'notesObject' variable outside my getAllNotes() function when after the…
0
votes
0 answers

Save files Web Application best practices

We are building a Chat Web Application (using the Blazor Wasm .Net 6.0) and we need to have some files either for upload or download. The storage process of the app is using indexedDb where we hold all the messages and all the other data that the…
james04
  • 1,580
  • 2
  • 20
  • 46
0
votes
1 answer

Why is only one of my Dexie queries updating in Svelte?

I have a svelte application that uses Dexie for data persistence. In my main component I have a child component that runs two liveQuerys to get the previous fixture and next fixture for a sports team. However, when advancing through time (it's a…
NickH
  • 107
  • 1
  • 10
0
votes
0 answers

IndexedDB getting all data with keys as a promise

This is the code I have that wraps IndexedDb API in promises. const asyncStorage = function(databaseName, storeName, version) { return new Promise((resolve, reject) => { const openRequest = window.indexedDB.open(databaseName, version) …
live627
  • 397
  • 4
  • 18
0
votes
0 answers

Inconsistent performance of IndexedDB initialization

I'm playing with IndexedDB. My application has 5 DBs: Each DB contains from 1000 to 1000000 items in it. When the application starts I measure how long it takes to initialize the first DB. The result is inconsistent, it varies from 50ms to…
George Chernov
  • 268
  • 3
  • 7
0
votes
0 answers

How to manage recources with IndexedDB in JS?

I'm new to using IndexedDB. Currently I am writing a small vocabulary learning app and I want it to remember the progress offline. IndexedDB emphasises that everything gets done in a separate thread. That was already a steep learning curve to deal…
JasonTS
  • 2,479
  • 4
  • 32
  • 48
0
votes
2 answers

Persist IndexedDB in cordova app when app updates

I have an Android app built using Cordova. Version 1, which has been released for a while, stores some data using IndexedDB. I’m ready to release the next version but updating the app wipes all the data stored in IndexedDB. What I noticed is that on…
klaudiusnaban
  • 196
  • 1
  • 10
0
votes
0 answers

Multiple Delete from IndexDB by an array of values

I have a store in IndexDB which called PendingFile var pendingFileTable = db.createObjectStore("pendingfile", {keyPath: ['serverid', 'hash', 'segment']}); Now, i have 50 PendingFiles that they have the same (ServerId, Hash) and segment is from…
james04
  • 1,580
  • 2
  • 20
  • 46
0
votes
0 answers

Jquery commands don't work when inserting elements using indexeddb

My jquery script is working fine when it's in html tag element, but on my website i had to use a databases to save the information of book like title,author, published year and also and the main thing is pages readed and total pages, i wanted to…
0
votes
0 answers

Dexie how to modify a Collection and get a Collection of the modified objects

Collection.modify() is a "terminal" operation, so it doesn't return a Collection but a Promise of the number of modified objects. But what if I want to have the modified objects? Something like await db.foos .filter(somePredicate) …
Ricola
  • 2,621
  • 12
  • 22
0
votes
0 answers

Why idb openDB with upgrade does not create my storage?

Question from a newbie. I'm using the IDB library. I already had a working version of my storage (on localhost), but the changes uploaded to dev do not work properly on it. An error appeared: ERROR DOMException: Failed to execute 'transaction' on…
kampai35
  • 11
  • 2
0
votes
1 answer

Write a file with Emscripten IDBFS/IndexedDB

I'm currently porting a C project to the web using Emscripten, but the one issue I haven't managed to figure out is why file writing using IDBFS doesn't work. This is the simplest possible C code example of what I'm trying to do currently, but it…