Registry / database / livedb-mongo

livedb-mongo

JSON →
library0.4.1jsnpmunverified

livedb-mongo is a database adapter for `livedb` and its successor, `sharedb`, providing persistent storage and an oplog implementation using MongoDB. While historically named `livedb-mongo`, the project's development has transitioned to primarily support `sharedb`, with its GitHub repository now `sharedb-mongo`. The package is currently at v5.1.0 and maintains an active release cadence, frequently updating to support newer Node.js and MongoDB versions. It stores document snapshots directly in named collections and operations in `COLLECTION_ops`, enabling direct MongoDB queries against the unwrapped JSON documents, which include internal versioning fields (`_v`, `_type`). This adapter is crucial for enabling real-time collaborative applications built with `livedb` or `sharedb`, ensuring operational transformation (OT) works correctly with MongoDB as the backend. It explicitly warns against direct database manipulation outside of the `livedb`/`sharedb` API to prevent data corruption.

npm install livedb-mongo
INSTALL
IMPORT
SIG · LIVEDB-MONGO
L
livedb-mongo
databasejavascriptv0.4.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ShareDbMongo
import ShareDbMongo from 'livedb-mongo'; // For ESM // Or for CommonJS: const ShareDbMongo = require('livedb-mongo');
import { ShareDbMongo } from 'livedb-mongo';
The primary export is a default function/class, often aliased as `ShareDbMongo` or `livedbmongo`. CommonJS `require` is still widely used in existing `livedb`/`sharedb` applications.
SharedbMongoOptions
import type { SharedbMongoOptions } from 'livedb-mongo';
import { SharedbMongoOptions } from 'livedb-mongo';
This type definition is for configuring the adapter instance, including options like `disableIndexCreation`.
Db
import { Db } from 'mongodb'; // When passing an existing MongoDB Db instance
While `livedb-mongo` can take a connection string, it can also accept an already initialized `mongodb` `Db` instance for more control.

Initializes `livedb-mongo` as an adapter for `livedb`, then fetches or creates a document and applies an update operation.

const ShareDbMongo = require('livedb-mongo'); // Or import ShareDbMongo from 'livedb-mongo'; for ESM const livedb = require('livedb'); // This adapter is for livedb (and sharedb) // MongoDB connection string. Ensure MongoDB is running locally. // Replace with your actual MongoDB connection string in production. const mongoUrl = process.env.MONGO_URL || 'mongodb://localhost:27017/livedb_test_db'; // Initialize the livedb-mongo adapter (also known as sharedb-mongo) // The second argument is for MongoDB driver options. const mongoAdapter = new ShareDbMongo(mongoUrl, { // Modern driver options, crucial for recent MongoDB versions useUnifiedTopology: true, // replicaSet can be important for change streams if using sharedb's oplog features // replicaSet: 'rs0' }); // Initialize livedb client with the mongo adapter const db = livedb.client(mongoAdapter); const collection = 'documents'; const docId = 'exampleDoc'; console.log(`Attempting to connect to MongoDB at ${mongoUrl} via livedb-mongo...`); // Try to fetch a document. If it doesn't exist, create it. db.fetch(collection, docId, (err, snapshot) => { if (err) { console.error('Error fetching document:', err); mongoAdapter.close(); // Ensure connection is closed on error return; } if (snapshot.v === 0) { // Document does not exist (version is 0) console.log(`Document '${docId}' not found. Creating it...`); const initialData = { title: 'Hello World', content: 'This is the initial version.', counter: 0 }; db.create(collection, docId, 'json0', initialData, (createErr) => { if (createErr) { console.error('Error creating document:', createErr); } else { console.log(`Document '${docId}' created successfully with data:`, initialData); } mongoAdapter.close(); // Close connection after operation }); } else { console.log(`Document '${docId}' found (v${snapshot.v}):`, snapshot.data); // Example: Apply an operation to update the document (e.g., increment counter) const op = { p: ['counter'], na: 1 }; // Operational Transform: Increment 'counter' by 1 db.apply(collection, docId, op, { source: 'example_script', version: snapshot.v + 1 }, (applyErr) => { if (applyErr) { console.error('Error applying operation:', applyErr); } else { console.log('Operation applied successfully: counter incremented.'); // Fetch again to see the updated state db.fetch(collection, docId, (fetchUpdatedErr, updatedSnapshot) => { if (fetchUpdatedErr) console.error('Error fetching updated document:', fetchUpdatedErr); else console.log('Updated document data:', updatedSnapshot.data); mongoAdapter.close(); }); } }); } });
Debug
Known issues
breakingVersion 5.0.0 of `livedb-mongo` (which is effectively `sharedb-mongo`) dropped support for Node.js v16. Users on Node.js v16 or older must upgrade their Node.js environment to v18 or newer.
fix
Upgrade Node.js to a supported version (e.g., v18, v20, or newer LTS releases).
affects: >=5.0.0
breakingVersion 4.0.0 dropped support for Node.js v14. Subsequent versions require Node.js v16 or higher.
fix
Upgrade Node.js to v16 or newer. For v5.x and above, Node.js v18+ is required.
affects: >=4.0.0 <5.0.0
breakingVersion 3.0.0 deprecated and subsequently dropped support for `mongodb@2`. Ensure your MongoDB database and driver are at version 3 or newer.
fix
Upgrade your MongoDB server to version 3.x or newer and ensure your `mongodb` driver dependency is compatible.
affects: >=3.0.0
gotchaEditing documents directly in MongoDB outside of the `livedb` (or `sharedb`) API can lead to data corruption or 'weird behaviour' due to the operational transformation (OT) system's reliance on specific document fields and operation sequencing. The adapter adds internal fields like `_v` and `_type` that must be managed by the library.
fix
Always use the `livedb` or `sharedb` client API (`db.create`, `db.apply`, etc.) to modify documents managed by `livedb-mongo`.
affects: >=0.4.1
gotchaWhen using the `disableIndexCreation` option for the `src_seq_v` index (introduced in v4.2.0), existing indexes are not automatically removed. If you disable an index that previously existed, you must manually delete it from your MongoDB collection if it's no longer desired.
fix
Manually drop the `src_seq_v` index from your MongoDB collections using `db.collection.dropIndex()` if it's no longer needed after configuring `disableIndexCreation`.
affects: >=4.2.0
gotchaOlder versions (prior to v4.1.1) had a memory leak when using cursor operations like `$count` or `$explain` with `mongodb@4-6`. This could lead to resource exhaustion in long-running applications.
fix
Upgrade to `livedb-mongo@4.1.1` or newer to resolve the memory leak issue.
affects: >=4.0.0 <4.1.1
Errors
Common errors & fixes
MongoServerSelectionError: connect ECONNREFUSED
The MongoDB server is not running or is not accessible at the specified connection URL.
fix
Ensure your MongoDB instance is running and accessible from your application's environment. Verify the connection string and firewall rules.
Error: Document already exists. Cannot create.
Attempted to `db.create` a document with an `_id` that already exists in the collection.
fix
Before creating, use `db.fetch` to check if the document exists (`snapshot.v === 0`). If it does, use `db.apply` to update it, or choose a unique `_id`.
Error applying operation: Invalid op
The operational transform (OT) operation applied via `db.apply` is malformed or incompatible with the document's current type/data.
fix
Review the structure of your OT operation. Ensure it correctly targets document paths and uses valid transformations for the document type (e.g., 'json0' for JSON documents).
Application hangs when using $map query transform
A bug in older versions of the adapter (prior to v4.1.1) caused `$map` queries to hang indefinitely.
fix
Upgrade `livedb-mongo` to version 4.1.1 or newer. This issue was fixed in PR #154.
Upgrade
Version history
0.4.1latest on npm
Audit
Dependencies
sharedbrequiredThis package is an adapter for sharedb (and historically livedb). sharedb is a peer dependency.
mongodbrequiredThe underlying MongoDB driver used for database interaction. It is a peer dependency.
Agent activity
4 hits · last 30 days
node
4
Resources