ShareDB Milestone MongoDB Adapter

2.1.0 · active · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

Initializes ShareDB with sharedb-milestone-mongo, connecting to a MongoDB instance. It demonstrates basic document fetching/creation using the configured milestone database.

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();

view raw JSON →