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
0 answers

service worker return data from indexedDB but React do not receive that data

i have a problem with using serviceWorker - React - indexedDB the problem is i can get data from indexedDB in serviceWorker.js and i return this data but the react code cant receive the data and returns error here is the code (the request method is…
shahriar
  • 1
  • 1
0
votes
0 answers

indexeddb promise -- wait for execution to finish

I have a function that retrieves data from an indexedDB: function dbGetProdData() { return new Promise((resolve)=>{ if (!indexedDBAvailable) { getDataByFetch(); createSizeFilterOptions(); resolve(false); } …
Chris
  • 650
  • 7
  • 20
0
votes
1 answer

HTML5 web storage abstraction libraries

From what I've read of web storage in HTML5, there are a number of different storage options with varying support across different browsers. Are there any popular libraries for abstraction of web storage in HTML5 applications?
Armand
  • 23,463
  • 20
  • 90
  • 119
0
votes
0 answers

Encrypting data stored on Indexeddb

I am making an offline Browser extension that stores some sensitive data (think of it like a password manager). How do I go about storing and retrieving this data while maintaining the ability to query it ? Do I store encrypted data and then decrypt…
Cheeze
  • 46
  • 1
  • 8
0
votes
0 answers

Chrome Extension: How to get object from DB when searching by value?

Im trying to create a chrome extension, and have encountered an annoying problem. I have this function, where I add an object to my indexedDB if the urlKeyValue of that object, does not already exist in the database. If the urlKeyValue does exist in…
Nick
  • 219
  • 4
  • 15
0
votes
1 answer

What is an efficient way to load a tensorFlow.js model (weights & biases) into a web worker context

I am working on loading a tensorFlow model into a multiple web-workers in vanilla JS I have tried using tf.loadLayersModel("indexeddb://model_name") but it gives a "ReferenceError: window is not defined" error. I am aware that the web-worker does…
0
votes
0 answers

How can I store an object within Indexed DB -- using an on click handler?

I'm trying to pass two properties of an object to store within the Indexed DB instance. The transaction is undefined when attempting to "add()" or "put()". The following is the initial database connection function const indexedDbInit = () =>…
Qwrkin
  • 1
  • 1
0
votes
1 answer

IndexedDB composite index partial match

I can't find an answer to this anywhere. I have an indexeddb composite index of a group id and a time, which I use to sort. let tmp_CREATEDTIMEindex = texts.index('GROUP_ID, CREATEDTIME'); This works great, except I need to result to reflect only…
JPD
  • 73
  • 4
0
votes
1 answer

How much information can I store in IndexedDB?

I've been searching but couldn't find an updated answer to 2022: what is the maximum size I can store in IndexedDB?
Jor
  • 1
0
votes
0 answers

reactjs redux persiste a huge amount of data

I'm working on a react redux project and persisting data with redux-persiste. since my data become huge : I get an error in navigator : Crash out of memory My persist use IndexedDb and my data is bigger than 500MB. How to be able to store big size…
Tania12
  • 323
  • 2
  • 16
0
votes
1 answer

Uncaught DOMException: Failed to execute 'put' on 'IDBObjectStore': Evaluating the object store's key path did not yield a value at request.onsuccess

I'm trying to Store some application data using indexedDB Here is my code function _getLocalApplicationCache(_, payload) { const indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.shimIndexedDB; if (!indexedDB)…
0
votes
1 answer

DOMException: Failed to execute 'getAll' on 'IDBObjectStore': The transaction is not active in angular

private get ctxMessage() { const messageTransaction = this.db.transaction('messages', 'readwrite'); const messageStore = messageTransaction.objectStore('messages'); return { messageTransaction, messageStore }; } private async…
0
votes
1 answer

Indexeddb javascript - Inserting more value in existing value

What is the simplest way to insert new value data in existing value, for example, lets say that i have already stored data numbers : "one", and i want to insert the word "two" at the end for numbers : "one,two". Thank you!
0
votes
1 answer

Cancelling IDBOpenDBRequest?

I have the following snippet of code: function open_db(dbname, dbversion, upgrade, onblocked) { if (upgrade === undefined) { upgrade = function basic_init(ev) { … }; } if (onblocked === undefined) { …
0
votes
1 answer

Using cached json file to read data instead of indexedDB in PWA

I need to read locally stored data served by a web app and I've been trying to understand how indexedDB works and I find it hostile for starters and a bit messy (I'm kind of new in webdev). So I've discovered a workaroud that works perfectly: store…
1 2 3
99
100