{"id":17977,"library":"test-rxdb","title":"RxDB: Local-First Reactive Database","description":"RxDB is a local-first, NoSQL database designed for JavaScript applications, supporting environments like websites, hybrid apps, Electron, PWAs, Deno, and Node.js. It operates on a reactive paradigm, built on RxJS, allowing developers to subscribe to real-time state changes for queries, documents, or individual fields, which simplifies UI development for real-time applications and enhances performance. The package is currently at version 15.36.1, with major releases occurring periodically, as seen with the recent version 15.0.0. Key differentiators include its local-first approach, robust JSON schema validation, and a flexible replication protocol that integrates with existing infrastructure or plugins for HTTP, GraphQL, and CouchDB, making it suitable for offline-first capabilities.","status":"active","version":"15.36.1","language":"javascript","source_language":"en","source_url":"https://github.com/Mihu89/test-rxdb","tags":["javascript","db","database","offline-first","local-first","local first","nosql","no-sql","jsonschema","typescript"],"install":[{"cmd":"npm install test-rxdb","lang":"bash","label":"npm"},{"cmd":"yarn add test-rxdb","lang":"bash","label":"yarn"},{"cmd":"pnpm add test-rxdb","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"RxDB's core reactivity and observable streams are built upon RxJS.","package":"rxjs","optional":false}],"imports":[{"note":"Since version 15, RxDB is primarily distributed as an ES Module. CommonJS users may need specific bundler configurations.","wrong":"const createRxDatabase = require('test-rxdb').createRxDatabase;","symbol":"createRxDatabase","correct":"import { createRxDatabase } from 'test-rxdb';"},{"note":"Used to extend RxDB's functionality with various plugins (e.g., replication, validation). This is typically imported directly from the main package.","wrong":"const addRxPlugin = require('test-rxdb').addRxPlugin;","symbol":"addRxPlugin","correct":"import { addRxPlugin } from 'test-rxdb';"},{"note":"Storage adapters are crucial for RxDB and must be explicitly imported and provided during database creation. For the actual `rxdb` package, many storage adapters are separate npm packages (e.g., `rxdb-storage-dexie`) or part of `@rxdb/premium`. This example assumes a `test-rxdb/plugins/storage-dexie` path for demonstration.","wrong":"import { getRxStorageDexie } from 'test-rxdb';","symbol":"getRxStorageDexie","correct":"import { getRxStorageDexie } from 'test-rxdb/plugins/storage-dexie';"},{"note":"RxDB ships with comprehensive TypeScript types, which are directly importable from the main package for type annotations.","symbol":"RxDatabase","correct":"import { RxDatabase, RxCollection, RxJsonSchema } from 'test-rxdb';"}],"quickstart":{"code":"import { createRxDatabase, addRxPlugin, RxDatabase, RxCollection, RxJsonSchema } from 'test-rxdb';\nimport { getRxStorageDexie } from 'test-rxdb/plugins/storage-dexie'; // Assumed path for 'test-rxdb' package\n\ninterface HeroDocType {\n  name: string;\n  color: string;\n  hp: number;\n  maxHP: number;\n}\n\ntype HeroCollection = RxCollection<HeroDocType>;\n\nconst heroSchema: RxJsonSchema<HeroDocType> = {\n  version: 0,\n  primaryKey: 'name',\n  type: 'object',\n  properties: {\n    name: {\n      type: 'string',\n      maxLength: 100 // Primary keys usually have a max length\n    },\n    color: {\n      type: 'string'\n    },\n    hp: {\n      type: 'number'\n    },\n    maxHP: {\n      type: 'number'\n    }\n  },\n  required: ['name', 'color', 'hp', 'maxHP']\n};\n\nasync function runQuickstart() {\n  console.log('Creating database...');\n  const db: RxDatabase = await createRxDatabase({\n    name: 'heroesdb',\n    storage: getRxStorageDexie() // Using Dexie storage adapter\n  });\n  console.log('Database created:', db.name);\n\n  // Add a collection to the database\n  console.log('Adding hero collection...');\n  await db.addCollections({\n    heroes: {\n      schema: heroSchema\n    }\n  });\n  console.log('Hero collection added.');\n\n  const heroCollection: HeroCollection = db.collections.heroes;\n\n  // Insert documents\n  console.log('Inserting heroes...');\n  await heroCollection.insert({\n    name: 'Batman',\n    color: 'black',\n    hp: 100,\n    maxHP: 100\n  });\n  await heroCollection.insert({\n    name: 'Superman',\n    color: 'blue',\n    hp: 120,\n    maxHP: 120\n  });\n  console.log('Heroes inserted.');\n\n  // Reactive query: subscribe to changes in the collection\n  console.log('Subscribing to all heroes...');\n  const subscription = heroCollection.find().$.subscribe(heroes => {\n    if (heroes) {\n      console.log('Current heroes:', heroes.map(h => h.toJSON()));\n    }\n  });\n\n  // Update a document, which will trigger the subscription\n  console.log('Updating Batman...');\n  const batman = await heroCollection.findOne({ selector: { name: 'Batman' } }).exec();\n  if (batman) {\n    await batman.patch({ hp: 90 });\n    console.log('Batman updated.');\n  }\n\n  // Clean up after some time\n  setTimeout(() => {\n    subscription.unsubscribe();\n    console.log('Subscription unsubscribed.');\n    db.destroy(); // Close the database connection\n    console.log('Database destroyed (closed).');\n  }, 3000);\n}\n\nrunQuickstart().catch(err => console.error('Quickstart failed:', err));","lang":"typescript","description":"This quickstart demonstrates how to set up an RxDB database, define a schema, add a collection, insert documents, perform a reactive query, and update data using the Dexie storage adapter."},"warnings":[{"fix":"Migrate your project to use ES Modules or ensure your build tools (Webpack, Rollup, Parcel) are configured to handle ESM. For Node.js, ensure `\"type\": \"module\"` is set in your `package.json`.","message":"RxDB v15+ is distributed as pure ES Modules (ESM). Projects using CommonJS (`require()`) must adapt their build setup (e.g., using a bundler with ESM support or configuring Node.js with `\"type\": \"module\"`).","severity":"breaking","affected_versions":">=15.0.0"},{"fix":"Remove the `storage.js` plugin and explicitly import your chosen storage adapter (e.g., `getRxStorageDexie`) and provide it to `createRxDatabase({ storage: getRxStorageDexie() })`. Storage adapters are typically in separate packages like `rxdb-storage-dexie` or within `@rxdb/premium`.","message":"The storage API was completely rewritten in v15. Storage adapters must now be explicitly imported and passed to `createRxDatabase()` via the `storage` option. The old `storage.js` plugin and implicit storage handling are removed.","severity":"breaking","affected_versions":">=15.0.0"},{"fix":"For browser-based applications, consider adding a polyfill for the Web Crypto API if your target environments do not fully support it, or provide a custom hash function if applicable.","message":"RxDB v15 no longer bundles `node:crypto` in browser builds. If your application relies on crypto functionalities (e.g., hashing), you might need to provide a polyfill for browser environments.","severity":"breaking","affected_versions":">=15.0.0"},{"fix":"Install `rxjs` explicitly: `npm install rxjs` or `yarn add rxjs`. Verify that the installed version meets RxDB's peer dependency requirements.","message":"RxDB has a peer dependency on RxJS (`^7.8.0`). Ensure you have a compatible version of RxJS installed, as mismatches can lead to runtime errors or unexpected behavior.","severity":"gotcha","affected_versions":">=15.0.0"},{"fix":"Migrate to a different storage adapter like Dexie.js, IndexedDB, or OPFS for browser environments, or Filesystem Node/SQLite for Node.js.","message":"The `LokiJS RxStorage` was deprecated in RxDB v15 and completely removed in v16, as the LokiJS library is no longer actively maintained.","severity":"deprecated","affected_versions":">=15.0.0 <16.0.0"},{"fix":"Update all calls from `db.destroy()` to `db.close()`. Also update related functions and attributes like `onDestroy()` to `onClose()`.","message":"The `.destroy()` method has been renamed to `.close()` in RxDB v16 to better reflect its function of closing the database connection without deleting data.","severity":"breaking","affected_versions":">=16.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Verify the correct import path for your chosen storage adapter. For official RxDB, you might need to install a separate package like `npm install rxdb-storage-dexie` and import from it: `import { getRxStorageDexie } from 'rxdb-storage-dexie';`","cause":"Attempting to import a storage adapter from an incorrect path or if the specific adapter is not included/exported by the `test-rxdb` package (or `rxdb` in general). In actual RxDB, many storages are separate npm packages.","error":"Error: Cannot find module 'test-rxdb/plugins/storage-dexie'"},{"fix":"Ensure your project is set up for ES Modules. Use `import { createRxDatabase } from 'test-rxdb';` and confirm your bundler/Node.js environment correctly processes ESM files.","cause":"This typically occurs when mixing CommonJS `require()` with an ESM-only library, or due to a bundler misconfiguration preventing correct ESM resolution. RxDB v15+ is pure ESM.","error":"TypeError: (0 , test_rxdb__WEBPACK_IMPORTED_MODULE_0__.createRxDatabase) is not a function"},{"fix":"Explicitly import and pass a storage adapter to the `storage` option when creating your database, e.g., `createRxDatabase({ name: 'mydb', storage: getRxStorageDexie() })`.","cause":"This error means `createRxDatabase` was called without providing a storage adapter, which is mandatory in RxDB v15+.","error":"RxError: MvRx_NoRxStorageFound: Cannot create database, no storage set"},{"fix":"If running in a browser, ensure it supports the Web Crypto API. For non-browser environments like some React Native setups or older Node.js versions, you may need to polyfill `crypto.subtle` or provide a custom hash function to RxDB.","cause":"RxDB v15+ utilizes `crypto.subtle.digest` for hashing. This error indicates that the Web Crypto API is unavailable in the current runtime environment.","error":"RxError: UT8: Crypto.subtle.digest is not available in your runtime."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}