Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/stefanwalther/mongoose-connection-config

Helper library to set mongoose configurations.
https://github.com/stefanwalther/mongoose-connection-config

library mongodb mongoose

Last synced: about 5 hours ago
JSON representation

Helper library to set mongoose configurations.

Awesome Lists containing this project

README

        

# mongoose-connection-config

[![npm](https://img.shields.io/npm/v/mongoose-connection-config.svg)]()
[![CircleCI](https://img.shields.io/circleci/project/github/stefanwalther/mongoose-connection-config.svg)](https://circleci.com/projects/gh/stefanwalther/mongoose-connection-config)
[![Codecov](https://img.shields.io/codecov/c/github/stefanwalther/mongoose-connection-config.svg)](https://codecov.io/gh/stefanwalther/mongoose-connection-config)

> Helper library to set mongoose connection configuration.

---

## Installation

```
$ npm install mongoose-connection-config --save
```

## Usage

### Build the connection string

```js
const MongooseConnectionConfig = require('mongoose-connection-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.

```js

const MongooseConnectionConfig = require('mongoose-connection-config');

const opts = {
connection_string: 'mongodb+srv://dbUser:[email protected]/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:[email protected]/test?retryWrites=true&w=majority

```

## API

### [MongooseConnectionConfig](src/index.js#L10)

### [Configuration](src/index.js#L53)
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](index.js) 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](https://docs.mongodb.com/manual/reference/connection-string/) for more details.
* `opts.port` **{Number}**: The MongoDB port, defaults to `27017`. See the mongodb [connection string spec](https://docs.mongodb.com/manual/reference/connection-string/) for more details.
* `opts.database` **{String}**: The MongoDB database, defaults to `admin`. See the mongodb [connection string spec](https://docs.mongodb.com/manual/reference/connection-string/) for more details.
* `opts.connectOptions` **{Object}**: The MongoDB connection properties, being passed through to the native MongoDB driver. See [mongoose' documentation](http://mongoosejs.com/docs/connections.html), resp. [MongoDB's native driver for node.js' documentation](https://github.com/mongodb/node-mongodb-native) for more details.

**Example**

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

### [.constructor()](src/index.js#L77)
Initialize a new MongooseConnectionConfig.

Basic Example:

**Params**

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

**Example**

```js
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**

```js
const MongooseConnectionConfig = require('./../src');

let mongooseConnectionConfig = new MongooseConnectionConfig();
console.log(mongooseConnectionConfig.DEFAULT_CONFIG);
```

Get the connection string.

* `returns` **{string}**

## Author
**Stefan Walther**

* [github.com/stefanwalther](http://github.com/stefanwalther)
* [twitter](http://twitter.com/waltherstefan)

## License
MIT

***

_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on June 01, 2019._