ShareDB Milestone MongoDB Adapter
sharedb-milestone-mongo is a database adapter for ShareDB that specifically targets MongoDB, enabling the use of 'milestone snapshots' to optimize the `connection.fetchSnapshot` method. These snapshots create specific points in time from which a smaller number of operations can be applied to reconstruct the requested document version, significantly improving performance for frequently accessed older versions. The current stable version is 2.1.0, and the package demonstrates a consistent release cadence, frequently updating to support newer versions of both Node.js (dropping older LTS versions) and its core dependencies, `mongodb` and `sharedb`. It stores these milestones in a dedicated `m_COLLECTION` in the MongoDB database, configurable for saving intervals or complex middleware-based logic.
Common errors
-
TypeError: MongoMilestoneDB is not a constructor
cause Attempting to use `new MongoMilestoneDB()` with an incorrect import statement, likely trying to destructure a default export.fixFor CommonJS: `const MongoMilestoneDB = require('sharedb-milestone-mongo');`. For ES Modules: `import MongoMilestoneDB from 'sharedb-milestone-mongo';`. -
Error: Mongo driver version 'X' is not supported. Please use a supported version.
cause Mismatched `mongodb` package version with the `sharedb-milestone-mongo` version. The library has frequent updates to support new `mongodb` versions and drops support for older ones.fixCheck the `sharedb-milestone-mongo` release notes (e.g., on GitHub or npm) for the specific version you are using and update your `mongodb` dependency to a compatible version (e.g., v2.1.0 supports `mongodb@7`). -
Error: Node.js version X is not supported by sharedb-milestone-mongo Y. Please upgrade Node.js.
cause Running `sharedb-milestone-mongo` on an unsupported Node.js version. Newer versions of the library frequently drop support for older Node.js LTS releases.fixUpgrade your Node.js environment to the minimum required version for your `sharedb-milestone-mongo` version (e.g., v2.0.0 requires Node.js v18+). -
MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
cause The MongoDB server is not running or is inaccessible at the specified connection string.fixEnsure your MongoDB server is running and accessible from the application's host, and verify the connection string (host, port, database name) is correct.
Warnings
- breaking Node.js v16 support was dropped in v2.0.0. Ensure your environment uses Node.js v18 or newer.
- breaking Node.js v14 support was dropped in v1.0.0. This aligns with Node.js LTS lifecycle.
- breaking Error handling was refactored in v1.0.0 to use string error codes and a dedicated error module. Direct comparison of error objects or properties might break.
- breaking Support for `mongodb@2` was explicitly dropped in v0.10.0. This means older MongoDB server versions or driver clients are no longer compatible.
- gotcha When configuring complex milestone saving logic via ShareDB middleware, explicitly set `request.saveMilestoneSnapshot = false` to disable milestones for certain collections. Leaving it `null` will revert to the default interval logic.
- gotcha Enabling indexing on existing, large milestone collections can have a significant adverse impact on performance during the index creation process.
Install
-
npm install sharedb-milestone-mongo -
yarn add sharedb-milestone-mongo -
pnpm add sharedb-milestone-mongo
Imports
- MongoMilestoneDB
import { MongoMilestoneDB } from 'sharedb-milestone-mongo';import MongoMilestoneDB from 'sharedb-milestone-mongo'; // or for CJS: const MongoMilestoneDB = require('sharedb-milestone-mongo'); - ShareDB
import ShareDB from 'sharedb'; // or for CJS: const ShareDB = require('sharedb'); - MongoClient
import mongodb from 'mongodb'; const MongoClient = mongodb.MongoClient;
import { MongoClient } from 'mongodb';
Quickstart
import ShareDB from 'sharedb';
import MongoMilestoneDB from 'sharedb-milestone-mongo';
const mongoUrl = process.env.MONGO_URL ?? 'mongodb://localhost:27017/test';
async function startServer() {
try {
const milestoneDb = new MongoMilestoneDB(mongoUrl);
const shareDb = new ShareDB({ milestoneDb: milestoneDb });
// Example of using ShareDB with the milestone adapter
const connection = shareDb.connect();
const doc = connection.get('mycollection', 'mydocument');
await new Promise((resolve, reject) => {
doc.fetch(err => {
if (err) return reject(err);
if (doc.type === null) {
doc.create({ content: 'Hello initial world!' }, resolve);
} else {
resolve();
}
});
});
console.log('ShareDB with milestone-mongo initialized and document fetched/created.');
// Cleanup (in a real app, this would be part of graceful shutdown)
// await milestoneDb.close(); // Note: MongoMilestoneDB doesn't expose a direct close method, it relies on underlying MongoClient close.
// shareDb.close();
} catch (error) {
console.error('Failed to start ShareDB server:', error);
process.exit(1);
}
}
startServer();