docs/rx-storage-localstorage.md
Discover how to quickly set up RxDB's LocalStorage-based storage as the recommended default. Learn its benefits, limitations, and why it’s perfect for demos, prototypes, and lightweight applications.
import {Steps} from '@site/src/components/steps';
RxDB can persist data in various ways. One of the simplest methods is using the browser’s built-in LocalStorage. This storage engine allows you to store and retrieve RxDB documents directly from the browser without needing additional plugins or libraries.
Recommended Default for using RxDB in the Browser
We highly recommend using LocalStorage for a quick and easy RxDB setup, especially when you want a minimal project configuration. For professional projects, the IndexedDB RxStorage is recommended in most cases.
getRxStorageLocalstorage() into createRxDatabase(). That’s it!While LocalStorage is the easiest way to get started, it does come with some constraints:
Despite these limitations, LocalStorage remains a great default option for smaller projects, prototypes, or cases where you need the absolute simplest way to persist data in the browser.
import { createRxDatabase } from 'rxdb/plugins/core';
import { getRxStorageLocalstorage } from 'rxdb/plugins/storage-localstorage';
const db = await createRxDatabase({
name: 'exampledb',
storage: getRxStorageLocalstorage()
});
await db.addCollections({
tasks: {
schema: {
title: 'tasks schema',
version: 0,
primaryKey: 'id',
type: 'object',
properties: {
id: { type: 'string', maxLength: 100 },
title: { type: 'string' },
done: { type: 'boolean' }
},
required: ['id', 'title', 'done']
}
}
});
await db.tasks.insert({ id: 'task-01', title: 'Get started with RxDB', done: false });
const nonDoneTasks = await db.tasks.find({
selector: {
done: {
$eq: false
}
}
}).exec();
While the localStorage API only exists in browsers, you can use the LocalStorage based storage in Node.js by using the mock that comes with RxDB.
This is intended to be used in unit tests or other test suites:
import { createRxDatabase } from 'rxdb/plugins/core';
import {
getRxStorageLocalstorage,
getLocalStorageMock
} from 'rxdb/plugins/storage-localstorage';
const db = await createRxDatabase({
name: 'exampledb',
storage: getRxStorageLocalstorage({
localStorage: getLocalStorageMock()
})
});