RxDB

17.1.0 · active · verified Wed Apr 22

RxDB (Reactive Database) is a local-first, NoSQL database designed for JavaScript applications, including websites, hybrid apps, Electron, Progressive Web Apps, and Node.js. Currently stable at version 17.1.0, it follows a release cadence with frequent beta cycles leading to major versions and subsequent point releases for bug fixes. Its core differentiator is its reactive architecture, leveraging RxJS to provide observable queries and real-time data changes. RxDB supports offline-first operation, enforces data integrity through JSONSchema-based validation, and offers a highly pluggable synchronization engine for diverse backends like HTTP, GraphQL, WebRTC (P2P), CouchDB, MongoDB, Supabase, and even serverless options such as Google Drive and Microsoft OneDrive. Key features include conflict resolution (e.g., CRDTs), encryption, and robust data migration capabilities for evolving schemas and underlying storage adapters, ensuring data consistency across multiple client instances and browser tabs.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create an RxDB database, define a schema, add a collection, insert and query documents, and subscribe to real-time changes using the Dexie storage adapter. It also shows the conditional inclusion of the DevMode plugin for development.

import { createRxDatabase, addRxPlugin } from 'rxdb';
import { getRxStorageDexie } from 'rxdb/plugins/storage-dexie';
import { RxDBDevModePlugin } from 'rxdb/plugins/dev-mode';

// Add dev mode plugin in development for better error messages
if (process.env.NODE_ENV !== 'production') {
  addRxPlugin(RxDBDevModePlugin);
}

async function runRxDBQuickstart() {
  const mySchema = {
    version: 0,
    primaryKey: 'id',
    type: 'object',
    properties: {
      id: { type: 'string', maxLength: 100 },
      title: { type: 'string' },
      done: { type: 'boolean', default: false }
    },
    required: ['id', 'title', 'done']
  };

  const db = await createRxDatabase({
    name: 'mydatabase',
    storage: getRxStorageDexie(),
    multiInstance: true // Enable multi-tab synchronization
  });

  await db.addCollections({
    todos: {
      schema: mySchema
    }
  });

  // Insert a document
  await db.todos.insert({
    id: 'todo-1',
    title: 'Learn RxDB',
    done: false
  });

  // Query documents
  const allTodos = await db.todos.find().exec();
  console.log('All todos:', allTodos.map(doc => doc.toJSON()));

  // Subscribe to changes (reactive query)
  const subscription = db.todos.find({
    selector: { done: false }
  }).$.subscribe(undoneTodos => {
    console.log('Undone todos changed:', undoneTodos.map(doc => doc.toJSON()));
  });

  // Update a document
  const todoToUpdate = await db.todos.findOne('todo-1').exec();
  if (todoToUpdate) {
    await todoToUpdate.patch({ done: true });
  }

  // Wait a moment for subscription to react and then clean up
  setTimeout(async () => {
    subscription.unsubscribe();
    await db.destroy();
    console.log('Database destroyed.');
  }, 1000);
}

runRxDBQuickstart();

view raw JSON →