OpenReplay
Navigate back to the homepage
BLOG
Browse Repo
Back

How to Use IndexDB to Manage State in JavaScript

Craig Buckler
July 8th, 2021 · 5 min read

My previous article, Getting Started with IndexedDB for Big Data Storage, demonstrated how to use the browser’s IndexedDB NoSQL database to store data. IndexedDB has good cross-browser support and offers at least 1GB of storage.

This article explains how to use IndexedDB to store state in a typical client-side JavaScript application.

The code is available from Github. It provides an example to-do app which you can use or adapt for your own projects.

What do we mean by “state”?

All applications store state. For a to-do app, it’s a list of items. For a game, it’s the current score, weapons available, power-up time remaining, etc. Variables store state but these can become unwieldly as complexity increases.

State management systems such as Redux and Vuex provide centralized data stores. Any JavaScript component can read, update, or delete data. Some systems permit components to subscribe to change events. For example, when a user toggles light/dark mode, all components update their styles accordingly.

Most state management systems store values in memory although techniques and plugins are available to transfer data to localStorage, cookies, etc.

Is IndexedDB suitable for storing state?

As always: it depends.

IndexedDB offers some benefits:

  1. It can typically store 1GB of data which makes it suitable for large objects, files, images etc. Moving these items out of memory can make an application faster and more efficient.

  2. Unlike cookies and Web Storage (localStorage and sessionStorage), IndexedDB stores native JavaScript object data. There’s no need to serialize into JSON strings or deserialize back again.

  3. IndexedDB access is asynchronous so it has minimal impact on the main JavaScript processing thread.

Note that Web Storage is synchronous: your JavaScript code pauses execution while it accesses data. This can cause performance issues when saving larger datasets.

Asynchronous data access has some drawbacks:

  • The IndexedDB API uses older callback and event methods so a Promise-based wrapper library is practical.

  • async class constructors and Proxy get/set handlers are not possible in JavaScript. This poses some challenges for state management systems.

Creating an IndexedDB-based state management system

The example code below implements a simple State management system in 35 lines of JavaScript. It offers the following features:

  • You can define a state with a name (string) and a value (primitive, array, object, etc). An IndexedDB object store saves these values using the name as an index.

  • Any JavaScript component can set or get a value by its name.

  • When a value is set, the state manager alerts all subscribed components about the change. A component subscribes itself via the State constructor, or by setting or getting a named value.

A to-do list project demonstrates the State manager. It defines two Web Components which both access the same todolist array of tasks managed by State objects:

  1. todo-list.js: renders the todolist HTML and removes an item when the user clicks it’s ‘done’ button.
  2. todo-add.js: shows an “add new item” form which appends new tasks to the todolist array.

Note: A single todo list component would be more practical, but this demonstrates how two isolated classes can share the same state.

Creating an IndexedDB wrapper class

The Getting Started article provided a Promise-based IndexedDB wrapper. We require a similar class, but it can be simpler because it fetches single records by name.

The js/lib/indexeddb.js script defines an IndexedDB class with a constructor. It accepts a database name, version, and upgrade function. It returns the instantiated object following a successful connection to the IndexedDB database:

1// IndexedDB wrapper class
2export class IndexedDB {
3
4 // connect to IndexedDB database
5 constructor(dbName, dbVersion, dbUpgrade) {
6
7 return new Promise((resolve, reject) => {
8
9 // connection object
10 this.db = null;
11
12 // no support
13 if (!('indexedDB' in window)) reject('not supported');
14
15 // open database
16 const dbOpen = indexedDB.open(dbName, dbVersion);
17
18 if (dbUpgrade) {
19
20 // database upgrade event
21 dbOpen.onupgradeneeded = e => {
22 dbUpgrade(dbOpen.result, e.oldVersion, e.newVersion);
23 };
24 }
25
26 dbOpen.onsuccess = () => {
27 this.db = dbOpen.result;
28 resolve( this );
29 };
30
31 dbOpen.onerror = e => {
32 reject(`IndexedDB error: ${ e.target.errorCode }`);
33 };
34
35 });
36
37 }

The asynchronous set method stores a value with the name identifier in the storeName object store. IndexedDB handles all operations in a transaction which triggers events that resolve or reject the Promise:

1// store item
2 set(storeName, name, value) {
3
4 return new Promise((resolve, reject) => {
5
6 // new transaction
7 const
8 transaction = this.db.transaction(storeName, 'readwrite'),
9 store = transaction.objectStore(storeName);
10
11 // write record
12 store.put(value, name);
13
14 transaction.oncomplete = () => {
15 resolve(true); // success
16 };
17
18 transaction.onerror = () => {
19 reject(transaction.error); // failure
20 };
21
22 });
23
24 }

Similarly, the asynchronous get method retrieves the value with the name identifier in the storeName object store:

1// get named item
2 get(storeName, name) {
3
4 return new Promise((resolve, reject) => {
5
6 // new transaction
7 const
8 transaction = this.db.transaction(storeName, 'readonly'),
9 store = transaction.objectStore(storeName),
10
11 // read record
12 request = store.get(name);
13
14 request.onsuccess = () => {
15 resolve(request.result); // success
16 };
17
18 request.onerror = () => {
19 reject(request.error); // failure
20 };
21
22 });
23
24 }
25
26
27}

Open Source Session Replay

Whether you’re using React, Vue or just vanillaJS, debugging a web application in production may be challenging and time-consuming. OpenReplay is an Open-source alternative to FullStory, LogRocket and Hotjar. It allows you to monitor and replay everything your users do and shows how your app behaves for every issue. It’s like having your browser’s inspector open while looking over your user’s shoulder. OpenReplay is the only open-source alternative currently available.

OpenReplay

Happy debugging, for modern frontend teams - Start monitoring your web app for free.

Creating the State manager class

A js/lib/state.js script imports IndexedDB and defines a State class. It shares five static property values across all instantiations:

  1. dbName: the name of the IndexedDB database used for state storage ("stateDB")
  2. dbVersion: the database version number (1)
  3. storeName: the name of the object store used to store all name/value pairs ("state")
  4. DB: a reference to a single IndexedDB object used to access the database, and
  5. target: an EventTarget() object which can dispatch and receive events across all State objects.
1// simple state handler
2import { IndexedDB } from './indexeddb.js';
3
4export class State {
5
6 static dbName = 'stateDB';
7 static dbVersion = 1;
8 static storeName = 'state';
9 static DB = null;
10 static target = new EventTarget();

The constructor accepts two optional parameters:

  1. an array of observed names, and
  2. an updateCallback function. This function receives the name and value whenever a state updates.

A handler listens for set events called when state changes. It runs the updateCallback function when the passed name is being observed.

1// object constructor
2 constructor(observed, updateCallback) {
3
4 // state change callback
5 this.updateCallback = updateCallback;
6
7 // observed properties
8 this.observed = new Set(observed);
9
10 // subscribe to set events
11 State.target.addEventListener('set', e => {
12
13 if (this.updateCallback && this.observed.has( e.detail.name )) {
14 this.updateCallback(e.detail.name, e.detail.value);
15 }
16
17 });
18
19 }

The class does not connect to the IndexedDB database until it’s required. The dbConnect method establishes a connection and reuses it across all State objects. On the first run, it creates a new object store named state (as defined in the static storeName property):

1// connect to IndexedDB database
2 async dbConnect() {
3
4 State.DB = State.DB || await new IndexedDB(
5 State.dbName,
6 State.dbVersion,
7 (db, oldVersion, newVersion) => {
8
9 // upgrade database
10 switch (oldVersion) {
11 case 0: {
12 db.createObjectStore( State.storeName );
13 }
14 }
15
16 });
17
18 return State.DB;
19
20 }

The asynchronous set method updates a named value. It adds the name to the observed list, connects to the IndexedDB database, sets the new value, and triggers a set CustomEvent which all State objects receive:

1// set value in DB
2 async set(name, value) {
3
4 // add observed property
5 this.observed.add(name);
6
7 // database update
8 const db = await this.dbConnect();
9 await db.set( State.storeName, name, value );
10
11 // raise event
12 const event = new CustomEvent('set', { detail: { name, value } });
13 State.target.dispatchEvent(event);
14
15 }

The asynchronous get method returns a named value. It adds the name to the observed list, connects to the IndexedDB database, and retrieves the indexed data:

1// get value from DB
2 async get(name) {
3
4 // add observed property
5 this.observed.add(name);
6
7 // database fetch
8 const db = await this.dbConnect();
9 return await db.get( State.storeName, name );
10
11 }
12
13}

You can retrieve and update state values using a new State object, e.g.

1import { State } from './state.js';
2
3(async () => {
4
5 // instantiate
6 const state = new State([], stateUpdated);
7
8 // get latest value and default to zero
9 let myval = await state.get('myval') || 0;
10
11 // set a new state value
12 await state.set('myval', myval + 1);
13
14 // callback runs when myval updates
15 function stateUpdated(name, value) {
16 console.log(`${ name } is now ${ value }`)
17 }
18
19})()

Other code can receive state update notifications about the same item, e.g.

1new State(['myval'], (name, value) => {
2 console.log(`I also see ${ name } is now set to ${ value }!`)
3});

Creating a state-managed todo list

A simple to-do list app demonstrates the state management system:

todo list app

The index.html file defines two custom elements:

1<!DOCTYPE html>
2<html lang="en">
3<head>
4<meta charset="UTF-8">
5<title>IndexedDB state management to-do list</title>
6<meta name="viewport" content="width=device-width,initial-scale=1" />
7<link rel="stylesheet" href="./css/main.css" />
8<script type="module" src="./js/main.js"></script>
9</head>
10<body>
11
12 <h1>IndexedDB state management to-do list</h1>
13
14 <todo-list></todo-list>
15
16 <todo-add></todo-add>
17
18</body>
19</html>
  • <todo-list> - the todo list controlled by ./js/components/todo-list.js which updates the list when tasks are added and removed, and
  • <todo-add> - a form to add items to the todo list controlled by ./js/components/todo-list.js.

./js/main.js loads both component modules:

1// load components
2import './components/todo-add.js';
3import './components/todo-list.js';

The scripts define frameworkless Web Components which get and set a shared todolist state. Web Components are beyond the scope of this article, but the basics are:

  1. You can define a custom HTML element (such as <todo-list>). The name must contain a dash (-) to avoid clashes with current or future HTML elements.
  2. A JavaScript class which extends HTMLElement defines the functionality. The constructor must call super().
  3. The browser calls a connectedCallback() method when it’s ready to update the DOM. The method can append content, optionally using an encapsulated Shadow DOM which is not accessible to other scripts.
  4. customElements.define registers the class with the custom element.

The <todo-list> component

./js/components/todo-list.js defines the TodoList class for the <todo-list> component. It shows a list of tasks and handles deletion when the user clicks a ‘done’ button. The class sets static HTML strings and creates a new State object. This monitors the a todolist variable and runs the object’s render() method when its value changes:

`

1import { State } from '../lib/state.js';
2
3class TodoList extends HTMLElement {
4
5 static style = `
6 <style>
7 ol { padding: 0; margin: 1em 0; }
8 li { list-style: numeric inside; padding: 0.5em; margin: 0; }
9 li:hover, li:focus-within { background-color: #eee; }
10 button { width: 4em; float: right; }
11 </style>
12 `;
13 static template = `<li>$1 <button type="button" value="$2">done</button></li>`;
14
15 constructor() {
16 super();
17 this.state = new State(['todolist'], this.render.bind(this));
18 }

The render() method receives the updated name and value (only todolist will arrive). It stores the list as a local object property then appends HTML to the Shadow DOM (created by the connectedCallback() method):

1// show todo list
2 render(name, value) {
3
4 // update state
5 this[name] = value;
6
7 // create new list
8 let list = '';
9 this.todolist.map((v, i) => {
10 list += TodoList.template.replace('$1', v).replace('$2', i);
11 });
12
13 this.shadow.innerHTML = `${ TodoList.style }<ol>${ list }</ol>`;
14
15 }

The connectedCallback() method runs when the DOM is ready. It:

  1. creates a new Shadow DOM and passes the latest todolist state to the render() method, and
  2. attaches a click event handler which removes an item from the todolist state. The render() method will automatically execute because the state changed.
1// initialise
2 async connectedCallback() {
3
4 this.shadow = this.attachShadow({ mode: 'closed' });
5 this.render('todolist', await this.state.get('todolist') || []);
6
7 // remove item event
8 this.shadow.addEventListener('click', async e => {
9
10 if (e.target.nodeName !== 'BUTTON') return;
11 this.todolist.splice(e.target.value, 1);
12 await this.state.set('todolist', this.todolist);
13
14 });
15
16 }

The TodoList class is then registered for the <todo-list> component:

1}
2
3// register component
4customElements.define( 'todo-list', TodoList );

The <todo-add> component

./js/components/todo-add.js defines the TodoAdd class for the <todo-add> component. It shows a form that can append new tasks to the todolist state. It sets a static HTML string and creates a new State object. This monitors the todolist state and retains it as a local object property:

1class TodoAdd extends HTMLElement {
2
3 static template = `
4 <style>
5 form { display: flex; justify-content: space-between; padding: 0.5em; }
6 input { flex: 3 1 10em; font-size: 1em; padding: 6px; }
7 button { width: 4em; }
8 </style>
9 <form method="post">
10 <input type="text" name="add" placeholder="add new item" required />
11 <button>add</button>
12 </form>
13 `;
14
15 constructor() {
16 super();
17 this.state = new State(['todolist'], (name, value) => this[name] = value );
18 }

The connectedCallback() method runs when the DOM is ready. It:

  1. fetches the latest todolist state into a local property which defaults to an empty array
  2. appends the form HTML to a Shadow DOM, and
  3. attaches a submit event handler which adds an new item to the todolist state (which, in turn, updates the <todo-list> component). It then clears the input field so you can add another task.
1// initialise
2 async connectedCallback() {
3
4 // get latest todo list
5 this.todolist = await this.state.get('todolist') || [];
6
7 const shadow = this.attachShadow({ mode: 'closed' });
8 shadow.innerHTML = TodoAdd.template;
9
10 const add = shadow.querySelector('input');
11
12 shadow.querySelector('form').addEventListener('submit', async e => {
13
14 e.preventDefault();
15
16 // add item to list
17 await this.state.set('todolist', this.todolist.concat(add.value));
18
19 add.value = '';
20 add.focus();
21
22 });
23
24 }

The TodoAdd class is then registered for the <todo-add> component:

1}
2
3// register component
4customElements.define( 'todo-add', TodoAdd );

Conclusion

Projects often avoid IndexedDB because its API is clunky. It’s not an obvious choice for state management, but the indexed database and large storage allocation could make it a good option for complex projects which store significant volumes of data.

More articles from OpenReplay Blog

Solid vs React - the Fastest VS the Most Popular UI Library

Learn about Solid, a performant alternative to React

June 28th, 2021 · 6 min read

The Art of Writing Good Code Comments

Writing code comments is easy, adding value through them is definitely not. Learn what makes a great code comment with this insightful article.

June 28th, 2021 · 4 min read
© 2021 OpenReplay Blog
Link to $https://twitter.com/OpenReplayHQLink to $https://github.com/openreplay/openreplayLink to $https://www.linkedin.com/company/18257552