RxDB Server

17.1.0 · active · verified Sun Apr 19

RxDB Server is a plugin for the RxDB (Reactive Database) ecosystem, enabling server-side capabilities for RxDB databases. It allows users to spawn a server on top of an RxDB instance, providing various endpoints such as CRUD REST APIs and real-time replication endpoints. This facilitates data synchronization between client devices and the server, supporting offline-first architectures and real-time data updates. The package is designed for Node.js, Deno, Bun, or Electron's main process and can operate as a standalone server or integrate with existing HTTP frameworks like Express. Currently at version 17.1.0, it generally follows the release cadence of the main RxDB library. A key differentiator is its Server Side Public License (SSPL), similar to MongoDB's, which aims to prevent large cloud vendors from monetizing the software without contributing back. The plugin is maintained separately from the core RxDB due to its specific dependencies and licensing model.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates setting up an in-memory RxDB, creating an RxDB server with Express adapter, and exposing replication and REST endpoints.

import { createRxDatabase, addRxPlugin } from 'rxdb';
import { getRxStorageMemory } from 'rxdb/plugins/storage-memory';
import { RxDBDevModePlugin } from 'rxdb/plugins/dev-mode';
import { createRxServer } from 'rxdb-server/plugins/server';
import { RxServerAdapterExpress } from 'rxdb-server/plugins/adapter-express';

const runServer = async () => {
  // Enable dev mode for helpful warnings in development
  addRxPlugin(RxDBDevModePlugin);

  // Define a simple schema for our data
  const mySchema = {
    version: 0,
    primaryKey: 'id',
    type: 'object',
    properties: {
      id: { type: 'string', maxLength: 100 },
      name: { type: 'string', maxLength: 100 },
      age: { type: 'number' }
    },
    required: ['id', 'name']
  };

  // Create a RxDB database in memory for demonstration
  const db = await createRxDatabase({
    name: 'heroesdb',
    storage: getRxStorageMemory()
  });

  // Add a collection
  const heroesCollection = await db.addCollections({
    heroes: { schema: mySchema }
  });

  // Insert some initial data
  await heroesCollection.heroes.insert({
    id: 'hero1',
    name: 'SuperMan',
    age: 40
  });
  console.log('Initial data inserted into RxDB.');

  // Create the RxDB Server
  const rxdbServer = await createRxServer({
    database: db,
    adapter: RxServerAdapterExpress,
    port: 3000,
    cors: true
  });

  // Add a replication endpoint for the 'heroes' collection
  await rxdbServer.addReplicationEndpoint({
    name: 'heroes-replication',
    collection: heroesCollection.heroes
  });

  // Add a REST endpoint for basic CRUD operations
  await rxdbServer.addRESTEndpoint({
    name: 'heroes-rest',
    collection: heroesCollection.heroes
  });

  // Start the server
  await rxdbServer.start();
  console.log('RxDB Server started on http://localhost:3000');
  console.log('Replication endpoint: http://localhost:3000/heroes-replication/0');
  console.log('REST endpoint (query): POST http://localhost:3000/heroes-rest/query');
};

runServer().catch(err => console.error('Error starting server:', err));

view raw JSON →