Skip to main content

IndexedDB RxStorage

The IndexedDB RxStorage is based on plain IndexedDB and can be used in browsers, electron or hybrid apps. Compared to other browser based storages, the IndexedDB storage has the smallest write- and read latency, the fastest initial page load and the smallest build size. Only for big datasets (more then 10k documents), the OPFS storage is better suited.

While the IndexedDB API itself can be very slow, the IndexedDB storage uses many tricks and performance optimizations, some of which are described here. For example it uses custom index strings instead of the native IndexedDB indexes, batches cursor for faster bulk reads and many other improvements. The IndexedDB storage also operates on Write-ahead logging similar to SQLite, to improve write latency while still ensuring consistency on writes.

IndexedDB performance comparison

Here is some performance comparison with other storages. Compared to the non-memory storages like OPFS and Dexie.js, it has the smallest build size and fastest write speed. Only OPFS is faster on queries over big datasets. See performance comparison page for a comparison with all storages.

IndexedDB performance

Using the IndexedDB RxStorage

To use the indexedDB storage you import it from the RxDB Premium 👑 npm module and use getRxStorageIndexedDB() when creating the RxDatabase.

import {
createRxDatabase
} from 'rxdb';
import {
getRxStorageIndexedDB
} from 'rxdb-premium/plugins/storage-indexeddb';

const db = await createRxDatabase({
name: 'exampledb',
storage: getRxStorageIndexedDB({
/**
* For better performance, queries run with a batched cursor.
* You can change the batchSize to optimize the query time
* for specific queries.
* You should only change this value when you are also doing performance measurements.
* [default=300]
*/
batchSize: 300
})
});

Overwrite/Polyfill the native IndexedDB

Node.js has no IndexedDB API. To still run the IndexedDB RxStorage in Node.js, for example to run unit tests, you have to polyfill it. You can do that by using the fake-indexeddb module and pass it to the getRxStorageDexie() function.

import { createRxDatabase } from 'rxdb';
import { getRxStorageIndexedDB } from 'rxdb-premium/plugins/storage-indexeddb';

//> npm install fake-indexeddb --save
const fakeIndexedDB = require('fake-indexeddb');
const fakeIDBKeyRange = require('fake-indexeddb/lib/FDBKeyRange');

const db = await createRxDatabase({
name: 'exampledb',
storage: getRxStorageIndexedDB({
indexedDB: fakeIndexedDB,
IDBKeyRange: fakeIDBKeyRange
})
});

Storage Buckets

The Storage Buckets API provides a way for sites to organize locally stored data into groupings called "storage buckets". This allows the user agent or sites to manage and delete buckets independently rather than applying the same treatment to all the data from a single origin. Read More

To use different storage buckets with the RxDB IndexedDB Storage, you can use a function instead of a plain object when providing the indexedDB attribute:

import { createRxDatabase } from 'rxdb';
import { getRxStorageIndexedDB } from 'rxdb-premium/plugins/storage-indexeddb';

const db = await createRxDatabase({
name: 'exampledb',
storage: getRxStorageIndexedDB({
indexedDB: async(params) => {
const myStorageBucket = await navigator.storageBuckets.open('myApp-' + params.databaseName);
return myStorageBucket.indexedDB;
},
IDBKeyRange
})
});

Limitations of the IndexedDB RxStorage

  • It is part of the RxDB Premium 👑 plugin that must be purchased. If you just need a storage that works in the browser and you do not have to care about performance, you can use the Dexie.js storage instead.
  • The IndexedDB storage requires support for IndexedDB v2, it does not work on Internet Explorer.