mongoose-connection-config

0.2.5 • Public • Published

mongoose-connection-config

npm CircleCI Codecov

Helper library to set mongoose connection configuration.


Installation

$ npm install mongoose-connection-config --save

Usage

Build the connection string

const MongooseConnectionConfig = require('mongoose-connnection-config');
 
const opts = {
  host: process.env.MONGO_HOST || 'localhost',
  port: process.env.MONGO_PORT || 27017,
  database: 'my-db'
};
const mcc = new MongooseConnectionConfig(opts);
 
console.log(mcc.getMongoUri()); // => mongodb://localhost:27017/my-db

Override the connection string

If you want to ignore all options being passed in, then set the option connection_string, this value will then be returned, building the connection string will be skipped.

 
const MongooseConnectionConfig = require('mongoose-connnection-config');
 
const opts = {
  connection_string: 'mongodb+srv://dbUser:dbPassword@cluster-fra.mongodb.net/test?retryWrites=true&w=majority',
  host: process.env.MONGO_HOST || 'localhost',
  port: process.env.MONGO_PORT || 27017,
  database: 'my-db'
};
const mcc = new MongooseConnectionConfig(opts);
 
console.log(mcc.getMongoUri()); // => mongodb+srv://dbUser:dbPassword@cluster-fra.mongodb.net/test?retryWrites=true&w=majority
 

API

MongooseConnectionConfig

Configuration

Define a configuration object to pass to the constructor.

If no options are defined, the default options will be used: See index.js => DEFAULT_CONFIGURATION for more information about the current default options.

Params

  • opts {Object}: Options to pass in.
  • {String}: `opts.connection_string - Full connection string which will then be returned, ignoring all other options.
  • opts.debug {Boolean}: Whether MongoDB runs in debug mode or not.
  • opts.host {String}: The MongoDBhost, defaults to localhost. See the mongodb connection string spec for more details.
  • opts.port {Number}: The MongoDB port, defaults to 27017. See the mongodb connection string spec for more details.
  • opts.database {String}: The MongoDB database, defaults to admin. See the mongodb connection string spec for more details.
  • opts.connectOptions {Object}: The MongoDB connection properties, being passed through to the native MongoDB driver. See mongoose' documentation, resp. MongoDB's native driver for node.js' documentation for more details.

Example

// Default Options:
const defaultOpts = {
   debug: false,
   host: 'localhost',
   port: 27017,
   database: '',
   connectOptions: {
     db: {},
     server: {
       auto_reconnect: true
     },
     replset: {},
     user: {},
     pass: {},
     auth: {},
     mongos: {}
   }
};

.constructor()

Initialize a new MongooseConnectionConfig.

Basic Example:

Params

  • {Configuration}: config - Configuration options overriding the default ones.

Example

const MongooseConnectionConfig = require('./src');
 
const opts = {
  host: process.env.MONGO_HOST || 'localhost',
  port: process.env.MONGO_PORT || 27017,
  database: 'my-db'
};
const mcc = new MongooseConnectionConfig(opts);
 
console.log(mcc.getMongoUri()); // => mongodb://localhost:27017/my-db   *

Example:

  • returns {Object}

Example

const MongooseConnectionConfig = require('./../src');
 
let mongooseConnectionConfig = new MongooseConnectionConfig();
console.log(mongooseConnectionConfig.DEFAULT_CONFIG);

Get the connection string.

  • returns {string}

Author

Stefan Walther

License

MIT


This file was generated by verb-generate-readme, v0.6.0, on June 01, 2019.

Readme

Keywords

Package Sidebar

Install

npm i mongoose-connection-config

Weekly Downloads

2

Version

0.2.5

License

MIT

Unpacked Size

19 kB

Total Files

17

Last publish

Collaborators

  • stefanwalther