# mongo-connection

> Connects to a mongo database

Latest version **1.0.11** (published 2017-11-07) · ISC license · 0 weekly downloads

## Install

```sh
npm install mongo-connection
pnpm add mongo-connection
yarn add mongo-connection
bun add mongo-connection
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.11 |
| Published | 2017-11-07 |
| First published | 2017-11-06 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Stéfano Damiano |
| Maintainers | stefuu |
| Keywords | mongo, mongodb, mongo, connection |

## Links

- npm: https://www.npmjs.com/package/mongo-connection
- npm.io page: https://npm.io/package/mongo-connection

## Dependencies (3)

- [debug](https://npm.io/package/debug.md) ^3.1.0
- [dotenv](https://npm.io/package/dotenv.md) ^4.0.0
- [mongodb](https://npm.io/package/mongodb.md) ^2.2.33

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 1.0.11 (latest) — 2017-11-07
- 1.0.10 — 2017-11-07
- 1.0.9 — 2017-11-07
- 1.0.8 — 2017-11-06
- 1.0.7 — 2017-11-06
- 1.0.6 — 2017-11-06
- 1.0.5 — 2017-11-06
- 1.0.4 — 2017-11-06
- 1.0.3 — 2017-11-06
- 1.0.2 — 2017-11-06
- 1.0.1 — 2017-11-06
- 1.0.0 — 2017-11-06

## README

# mongo-connection
An abstraction of a mongo connection using mongodb driver.

## GIT
https://github.com/Stefuu/mongo-connection

## Installation
```
npm install --save mongo-connection
```

## Usage
Instance the connection class like so:

```javascript
/* 
The 'config' parameter can be null if you prefer to
use the following node enviroment variables:
DATABASE_NAME=<NAME>
DATABASE_URI=<URI>
DATABASE_USER=<USER>
DATABASE_PASS=<PASS>
DATABASE_AUTH_MECHANISM=<AUTH>
DATABASE_REPLICASET=<REPLICASET>
*/
let config = {
  name: "<DATABASE_NAME>",
  user: "<DATABASE_USERNAME>",
  pass: "<DATABASE_PASSWORD>",
  auth_mechanism: "<DATABASE_AUTH_MECHANISM>",
  uri: "<DATABASE_URI>", //host and port -> "myhost.com:23691",
  replicaset: "<DATABASE_REPLICASET>"
}

// Mongo options (opitional)
config.options: {
  "readPreference": "ReadPreference.SECONDARY_PREFERRED",
  "keepAlive": 1000,
  "connectTimeoutMS": 30000,
  "poolSize": 5,
  "autoReconnect": true,
  "reconnectInterval": 30000,
  "reconnectTries": 240
}

let Db = require('mongo-connection')
let mongoInstance = new Db(config)

mongoInstance.connect()
.then(db => {  
  //now you can use it like so
  let collection = db.collection('<MY_COLLECTION>')  
  collection.find({})
  .toArray()
  .then(docs => {
    //do something cool with query result
  })
})
.catch(err => {
  console.log(err)
})
```

### Custom events
You can set custom events for your mongo connection instance like so:

```javascript
// custom code that will run after the event triggers
let events = {
  disconnected: function(){
    //some code
  },
  connecting: function(){
    //some code
  },
  open: function(){
    //some code
  },
  error: function(){
    //some code
  },
  reconnected: function(){
    //some code
  },
  close: function(){
    //some code
  }
}

mongoInstance.setEvents(events)
```

---
_Source: https://npm.io/package/mongo-connection · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
