Skip to content

Latest commit

 

History

History
71 lines (50 loc) · 2.71 KB

rx-storage-dexie.md

File metadata and controls

71 lines (50 loc) · 2.71 KB

RxStorage Dexie.js

To store the data inside of IndexedDB in the browser, you can also use the Dexie.js RxStorage.

Dexie.js is a minimal wrapper around IndexedDB. For the Dexie based RxStorage, we use the mingo query handler.

Pros

Cons

Usage

import { createRxDatabase } from 'rxdb';
import { getRxStorageDexie } from 'rxdb/plugins/storage-dexie';

const db = await createRxDatabase({
    name: 'exampledb',
    storage: getRxStorageDexie()
});

Overwrite/Polyfill the native IndexedDB

Node.js has no IndexedDB API. To still run the Dexie 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 { getRxStorageDexie } from 'rxdb/plugins/storage-dexie';

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

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

Using addons

Dexie.js has its own plugin system with many plugins for encryption, replication or other use cases. With the Dexie.js RxStorage you can use the same plugins by passing them to the getRxStorageDexie() function.

const db = await createRxDatabase({
    name: 'exampledb',
    storage: getRxStorageDexie({
        addons: [ /* Your Dexie.js plugins */ ]
    })
});

Boolean Index

Dexie.js does not support boolean indexes because they are not a valid index key in IndexedDB itself. See w3c/IndexedDB#76. To index boolean fields, you can:

  • Substitute the index field with a non boolean field. For example you can use a number field and store 0 as false and 1 as true,
  • or use the the IndexedDB RxStorage which supports boolean indexes because it does not use the IndexedDB indexes but custom index string instead.