@springworks/mongoose-connection

5.2.1 • Public • Published

@springworks/mongoose-connection

Module that creates and manages a named mongoose connection to a MongoDB database.

The module exports a single function used to create a named wrapper around a mongoose connection.

Example:

const connection_name = 'my connection'; // Used to retrieve connection
const mongo_uri = 'mongodb://...';
const logger = null; // Optional Bunyan logger
const mongoose_connection = require('@springworks/mongoose-connection');
const wrapped_connection = mongoose_connection.getWrapper(connection_name, mongo_uri, logger);

// The connection:
wrapped_connection.connection();

// Connect to the DB. The connect method can be called multiple times.
// Callbacks will be queued until the connection is established (or it failes).
wrapped_connection.connect(function(err) {
  if (!err) {
    // Connected
  }
});

// Disconnect from the DB. Like the connect method, disconnect can be called
// multiple times and will queue callbacks until disconnected.
wrapped_connection.disconnect(function(err) {
  if (!err) {
    // Disconnected
  }
});

// Ping the db server (using the db command "ping")
wrapped_connection.ping(function(err) {
  console.log('Ping %s', err ? 'failed' : 'succeeded');
});

Mongoose

This module will export its own version of mongoose. Please use that export instead of having mongoose as a dependency in the project using this module. The motivation behind this is that we ran into errors when using different versions of mongoose in the project using this module.

const mongoose_connection = require('@springworks/mongoose-connection');
const mongoose = mongoose_connection.mongoose;

databaseUriFromConfig

Can be used to generate a connection string based on a config object.

const mongoose_connection = require('@springworks/mongoose-connection');
const uri = mongoose_connection.databaseUriFromConfig({
  primary_db,
  secondary_dbs,
  db_user,
  db_pass,
  db_name,
});

API

getWrapper(connection_name, mongo_uri, logger)

Create or retrieve a shared connection wrapper by name and uri.

Returns an object with the methods connection, connect and disconnect.

connection()

Get the connection object used by mongoose.

connect(callback)

Connect to the DB. The connect method can be called multiple times while connecting. Callbacks will be queued until the connection is established (or it failes).

The callback function will be invoked with a single err param.

disconnect(callback)

Disconnect from the DB. Like the connect method, disconnect can be called multiple times and will queue callbacks until disconnected.

The callback function will be invoked with a single err param.

ping(callback)

Ping db server using the ping command. The ping command is a no-op used to test whether a server is responding to commands. This command will return immediately even if the server is write-locked.

The callback function will be invoked with a single err param if ping fails.

Readme

Keywords

none

Package Sidebar

Install

npm i @springworks/mongoose-connection

Weekly Downloads

4

Version

5.2.1

License

MIT

Unpacked Size

14.8 kB

Total Files

9

Last publish

Collaborators

  • springworksdev
  • springworksprime