RxDB

repository·master·Indexed 12 days ago

https://github.com/pubkey/rxdb

A fast, local-first, reactive NoSQL database for JavaScript applications. Version 17.4.0 supports real-time, multiplayer applications across Browsers, Node.js, and mobile platforms, featuring support for Angular, Electron, Flutter, Ionic, and Capacitor.

Tokens
399.2K
Snippets
876
Records
1.4K
Agent score
97%

What's inside RxDB

  1. Key benefits of using RxDB as a browser database

    master

    RxDB offers several architectural advantages for web applications:

    • Offline Accessibility: Users can interact with the application without an active internet connection.
    • Caching & Low Latency: Storing data locally reduces server requests and eliminates network round-trips for queries.
    • Scalability: By distributing the query workload to the user's device, you reduce the burden on your central server.
    • Multi-tab Support: RxDB natively supports synchronization across different browser tabs or instances of the same application.
    • State Management: You can use the local database for state management, potentially replacing libraries like Redux or NgRx.
    • Encryption: Sensitive local data can be stored securely using RxDB's encryption capabilities.
  2. Benefits of using RxDB in React applications

    master

    RxDB is a client-side JavaScript database built on top of the IndexedDB standard. When integrated with React, it provides several key advantages for data management:

    • Improved Performance: Local data storage reduces the frequency of server requests, leading to faster retrieval and higher responsiveness.
    • Offline Capabilities: Enables users to interact with the application and access data even without an internet connection.
    • Real-Time Updates: Leverages reactive programming to observe data changes, allowing the UI to update automatically when data changes.
    • Reduced Server Load: By performing data operations locally on the client, it helps create a more scalable architecture by offloading work from the server.
  3. Limitations of Supabase for offline-first development

    master

    When building local-first applications, Supabase has several key limitations:

    • No Client-Side Database: Queries like supabase.from('table').select('*') fail when the network is unavailable because there is no local fallback.
    • No Sync Engine: Supabase Realtime is a streaming protocol. It does not track checkpoints or automatically fetch missed changes after a reconnection.
    • No Observable Queries: The SDK lacks a reactive query system. To update a UI, you must manually combine Realtime subscriptions with manual re-fetches, which is inefficient and complex.
    • Relational vs. Document Model: Supabase uses a relational PostgreSQL model, which may not map directly to the JSON document shapes often used in modern web UIs.
  4. Replicate RxDB with Microsoft OneDrive

    master

    The replication-microsoft-onedrive plugin enables cross-device synchronization for single users by replicating an RxDB database to a folder in their Microsoft OneDrive. This approach requires no backend server and works offline-first. It uses the Microsoft Graph API for data storage and WebRTC for near real-time peer-to-peer signaling between devices.

    import {
        replicateMicrosoftOneDrive
    } from 'rxdb/plugins/replication-microsoft-onedrive';
    
    const replicationState = await replicateMicrosoftOneDrive({
        replicationIdentifier: 'my-app-onedrive-sync',
        collection: myRxCollection,
        oneDrive: {
            authToken: 'USER_ACCESS_TOKEN',
            folderPath: 'my-app-data/user-1'
        },
        live: true,
        pull: {
            batchSize: 60,
            modifier: doc => doc
        },
        push: {
            batchSize: 60,
            modifier: doc => doc
        }
    });
  5. Compare IndexedDB wrappers

    master

    IndexedDB is the standard browser API for structured data, but it is low-level, callback-based, and requires manual transaction management. Developers typically use an IndexedDB wrapper to add promises, queries, schemas, and reactivity.

    Common wrappers include:

    • idb: A tiny (<1KB) promise-based mirror of the native API. Best for library authors wanting full control.
    • Dexie.js: A popular minimalist wrapper with a fluent query builder and schema declarations. Best for ergonomic, index-driven lookups.
    • localForage: A key-value wrapper that falls back to WebSQL or localStorage. Best for simple caching by key.
    • PouchDB: A full document database that syncs with CouchDB. Best for offline replication with CouchDB.
    • JsStore: An SQL-like query API that runs in a Web Worker. Best for SQL-style mental models.
    • LokiJS: An in-memory document store with optional IndexedDB persistence. Best for small datasets that fit in RAM.
    • RxDB: A full-featured, local-first NoSQL database. Best for apps requiring JSON Schema validation, Mango-style queries, reactivity, migrations, and multi-backend sync.
  6. Key characteristics of RxDB

    master

    RxDB is a JSON-focused, NoSQL database designed for client-side usage (browsers, mobile apps, or Node.js) with an offline-first philosophy. Its core features include:

    • Local JSON Storage: Stores records as JSON documents, matching front-end state management and eliminating complex transformations.
    • Reactive Queries: Uses JSON-based query definitions that allow UIs to subscribe to results and automatically refresh when data changes.
    • Offline-First Sync: Includes replication plugins to push/pull changes to/from remote servers, handling offline updates seamlessly.
    • Optional JSON-Schema: Supports JSON-Schema for field definition, required fields, data types, indexing, and type validation.
  7. Explore RxDB as a Firestore Alternative

    master

    RxDB serves as a local-first, real-time database solution that can replace Firestore by providing the following capabilities:

    • Fully Offline-First: Data is stored locally, ensuring the application works without a network connection.
    • Backend Freedom: Unlike Firestore, RxDB can sync with any backend you own or control.
    • Advanced Conflict Resolution: Provides customizable logic for handling data conflicts during synchronization.
    • Reduced Cloud Costs: By utilizing local storage and controlled synchronization, you can avoid the high costs associated with managed cloud database scaling.
    • No Query Limits: Offers advanced querying capabilities that are not restricted by the limitations of a managed service.
    • True Offline-Start: Applications can start and function immediately using local data before any network connection is established.
    • Cross-Platform Support: Runs in any JavaScript runtime (Browser, Node.js, React Native, Electron, etc.).
  8. Explore RxDB Third-Party Plugins

    master

    RxDB can be extended with various third-party plugins to add advanced functionality such as React hooks, full-text search, specific backend replication, and utility enhancements.

    Available plugins include:

    • UI Integration: rxdb-hooks for integrating RxDB into React applications.
    • Search: rxdb-flexsearch for full-text search using FlexSearch.
    • Replication:
      • rxdb-orion for Laravel Orion.
      • rxdb-supabase for Supabase.
      • rxdb-solid for Solid pods.
    • Utilities & Features:
      • rxdb-utils for models, timestamps, default values, and views.
      • rxdb-extra for timestamps, simple search, and strict schema (compatible with latest versions).
    • Storage Adapters: loki-async-reference-adapter for using LokiJS RxStorage in React Native environments.
  9. Key features of RxDB for frontend applications

    master

    RxDB is optimized for JavaScript-centric frontend development with several core capabilities:

    • NoSQL (JSON) Documents: Uses JSON as the primary data structure, aligning with native JavaScript objects.
    • TypeScript Support: Provides strong typing for better code quality and tooling.
    • EventReduce Algorithm: Optimizes observed queries to reduce redundant events and minimize unnecessary UI re-renders.
    • Compressed Storage: Option to store documents in a compressed format to reduce disk usage and improve I/O performance.
    • Multi-tab Support: Built-in synchronization and state management across multiple browser tabs.
    • Flexible Replication: A replication algorithm compatible with various backends including Firebase, CouchDB, NATS, and WebSockets.
    • Cross-Platform Storage: A flexible storage layer that allows reusing the same codebase across Web, Electron.js, React Native, and Capacitor.js.
  10. RxDB as a PowerSync Alternative

    master

    RxDB is a reactive, local-first JavaScript database that serves as an alternative to PowerSync, particularly for JavaScript-centric teams. Unlike PowerSync, which relies on a WASM SQLite build in the browser (introducing latency) and a server-authoritative model, RxDB is storage-agnostic and allows for client-side conflict resolution.

    Key advantages of RxDB include:

    • Lower Browser Latency: Runs query planning in JavaScript to avoid the WASM-to-JS boundary overhead found in WASM SQLite.
    • Flexible Backend: Uses a transport-agnostic replication protocol, allowing you to sync with Postgres, MongoDB, GraphQL, or custom REST APIs.
    • MongoDB-style Queries: Uses type-safe Mango queries (plain objects) instead of SQL strings.
    • Apache 2.0 License: No field-of-use restrictions, unlike PowerSync's FSL license.
    • Storage Agnostic: Easily switch between IndexedDB, OPFS, Dexie, or SQLite via configuration.
  11. Compare RxDB with real-time offline-first alternatives

    master

    When choosing a client-side JavaScript database for real-time, local-first applications, you may consider several alternatives to RxDB. These alternatives include:

    • Firebase
    • Meteor
    • Minimongo
    • WatermelonDB
    • AWS Amplify
    • AWS Datastore
    • RethinkDB
    • Horizon
    • Supabase
    • CouchDB

    Note that the RxDB documentation provides an opinionated view of these technologies. RxDB distinguishes itself by being backend-agnostic (allowing use with your own backend or no backend at all), being independently funded (not tied to a specific cloud provider), and having a long history of performance optimization and stability.

  12. Compare RxDB and WatermelonDB for offline-first applications

    master
    RxDB is positioned as a cross-platform, observable, and offline-first alternative to WatermelonDB. While WatermelonDB focuses on lazy loading and native thread execution for React Native, RxDB provides broader coverage for teams building across web and mobile. Key differentiators include RxDB's built-in replication protocols, flexible storage options (including modern browser APIs like OPFS), and framework-agnostic reactive queries that work beyond just React/React Native.