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

chpr-mongodb

v4.0.1-force-exit

Published

MongoDB driver

Downloads

131

Readme

chpr-mongodb

CircleCI codecov

MongoDB connector

MIGRATIONS GUIDELINES


This library is a wrapper on node-mongodb-native. The original documentation is available here. The purpose is to help having robust connection on MongoDb databases potentially on multiple databases, with models definitions and migration patterns.

Requirements

  • async / await is required (Node.js 7+)
  • MongoDB 3.2+ is required to use the document validation feature

Configuration

Overall configuration

The connector configuration object must look like this:

{
  databases: [
    // Database configuration
  ]
}

By default, all indexes are created in background. You can change this behavior by setting the property ensureIndexInBackground to false.

Database connection configuration

If you want to connect a specific database, please use the following object format:

{
  name: 'riders', // Database name
  url: 'mongodb://localhost:27017/riders', // Database connection string
  options: {
    // MongoDb connection options
  }
}

The options properties are documented in the official documentation.

Instanciation

Simple connection

const { MongoDbConnector } = require('chpr-mongodb');

async function start() {
  const client = new MongoDbConnector({
    databases: [{
      name: 'test',
      url: 'mongodb://localhost:27017/test'
    }]
  });

  await client.connect();
}

async function stop() {
  await client.disconnect();
}

Models definitions

Please have a look to the example.

const { MongoDbConnector } = require('chpr-mongodb');

async function start() {
  const client = new MongoDbConnector({
    databases: [{
      name: 'test',
      url: 'mongodb://localhost:27017/test'
    }]
  }, {
    users: {
      DATABASE: 'test',
      COLLECTION: 'users'
    }
  });

  await client.connect();

  // Example of models initialization (tests only):
  await client.migrate('update', 'from-models');
}

async function stop() {
  await client.disconnect();
}

Migrations definitions

Please have a look to the migrations documentation.

// migrations/index.js
const addUniqueIndexExample20171129 = require('./1.add-unique-index-example.js');

module.exports = new Map([
  ['1', addUniqueIndexExample20171129]
]);
// migrate.js
const { MongoDbConnector } = require('chpr-mongodb');

const migrations = require('./migrations');

async function start() {
  const client = new MongoDbConnector({
    databases: [{
      name: 'test',
      url: 'mongodb://localhost:27017/test'
    }]
  }, {
    users: {
      DATABASE: 'test',
      COLLECTION: 'users'
    }
  }, migrations);

  await client.connect();

  // Example of unique migration call (tests only):
  await client.migrate('update', '1');
}

async function stop() {
  await client.disconnect();
}

Installation

nvm i

npm install

Tests

# Run the mocha tests only
npm run mocha

# Run the eslint only
npm run eslint

# Run the coverage withuut eslint
npm run mocha

# Run all the tests
npm test

/!\ Deprecation warnings

migrate('update', 'init')

After 2018-03-01, no guarantee about its support is given

The keyword init has been deprecated in favor of from-models. It was unclear that this migration was special and was applying for a test purpose only migration of models from the models definitions themselves. The replacement will be:

migrate('update', 'from-models')

Or even more explicit:

applyModels('update')

migrate('update') with no second parameter

After 2018-03-01, no guarantee about its support is given

The absence of second parameter to the migrate method was unclear that it will apply all migrations from the very first one to the last one. The keyword all is prefered until now. The following syntax will apply the same result:

migrate('update', 'all')

Or even more explicit:

applyAllMigrations('update')