npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sharedb-milestone-mongo

v1.1.0

Published

MongoDB milestone snapshot database adapter for ShareDB

Downloads

4,817

Readme

sharedb-milestone-mongo

NPM Version Test Coverage Status

MongoDB milestone snapshot database adapter for sharedb. Milestone snapshots can be used to speed up the results of ShareDB's connection.fetchSnapshot method by providing points in time on top of which a smaller number of ops can be applied to reach the requested version.

Milestone snapshots will be stored in a collection called m_COLLECTION where COLLECTION is the name of your ShareDB collection.

Quick start

const MongoMilestoneDB = require('sharedb-milestone-mongo');
const ShareDB = require('sharedb');

const milestoneDb = new MongoMilestoneDB('mongodb://localhost:27017/test');
const shareDb = new ShareDB({ milestoneDb: milestoneDb });

Configuration

Mongo

The underlying Mongo database can be configured in a number of ways. This library uses the mongodb library, so any configuration that can be used there can be used in this library.

Mongo can be configured simply using a connection string and any desired options:

const milestoneDb = new MongoMilestoneDB('mongodb://localhost:27017/test', { loggerLevel: 'debug' });

It can also be configured with a callback that provides an instance of a MongoClient:

const mongodb = require('mongodb');

const milestoneDb = new MongoMilestoneDB((callback) => {
  mongodb.connect('mongodb://localhost:27017/test', callback);
});

The Mongo connection string or function can be used as the first argument of the constructor, or as part of an options object:

const milestoneDb = new MongoMilestoneDB({
  mongo: 'mongodb://localhost:27017/test',
  loggerLevel: 'debug',
});

Milestone snapshot saving

Intervals

By default, ShareDB will save a milestone snapshot with a given frequency. This library defaults to an interval of 1,000, saving milestones when the 1,000th, 2,000th, etc. versions are committed. That interval can be configured:

const milestoneDb = new MongoMilestoneDB({
  mongo: 'mongodb://localhost:27017/test',
  interval: 500,
});

Complex saving logic

If you need more complex saving logic (eg different intervals depending on collection), this can be achieved using ShareDB middleware:

const milestoneDb = new MongoMilestoneDB('mongodb://localhost:27017/test');
const shareDb = new ShareDB({ milestoneDb: milestoneDb });

shareDb.use('commit', (request, callback) => {
  switch (request.collection) {
    case 'foo':
      // Save every 100 versions for collection 'foo'
      request.saveMilestoneSnapshot = request.snapshot.v % 100 === 0;
      break;
    case 'bar':
    case 'baz':
      // Save every 500 versions for collections 'bar' and 'baz'
      request.saveMilestoneSnapshot = request.snapshot.v % 500 === 0;
      break;
    default:
      // Don't save any milestones for collections not named here.
      // IMPORTANT: We have to set this to false to actively disable milestones
      // If left to null, then the default interval will still apply
      request.saveMilestoneSnapshot = false;
  }

  callback();
});

Note that the default value of request.saveMilestoneSnapshot is null. If left to this value, it will use the default interval logic. If you want to actively disable snapshots, you must make sure to set it to false.

Indexing

By default, indexing is enabled on the milestone collections in order to speed up fetching. This can have an adverse impact on performance if being enabled on an existing collection that is missing the index. The indexing can be disabled:

const milestoneDb = new MongoMilestoneDB({
  mongo: 'mongodb://localhost:27017/test',
  disableIndexCreation: true,
});

Error codes

4100 - Bad request - DB

  • 4101 - Must provide valid collection name
  • 4102 - Must provide valid ID
  • 4103 - Must provide valid snapshot
  • 4104 - Must provide valid integer version or null
  • 4105 - Must provide valid integer timestamp or null

5100 - Internal error - DB

  • 5101 - Mongo closed