0.2.5 • Published 6 years ago
mongoose-connection-config v0.2.5
mongoose-connection-config
Helper library to set mongoose connection configuration.
Installation
$ npm install mongoose-connection-config --saveUsage
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-dbOverride 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=majorityAPI
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 tolocalhost. See the mongodb connection string spec for more details.opts.port{Number}: The MongoDB port, defaults to27017. See the mongodb connection string spec for more details.opts.database{String}: The MongoDB database, defaults toadmin. 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.