# ots-mongo-wrapper

> ots-mongo-wrapper =================

Latest version **0.4.0** (published 2015-09-01) · 0 weekly downloads

## Install

```sh
npm install ots-mongo-wrapper
pnpm add ots-mongo-wrapper
yarn add ots-mongo-wrapper
bun add ots-mongo-wrapper
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2015-09-01 |
| First published | 2013-08-13 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Clay Simmons |
| Maintainers | cletustboone |

## Links

- npm: https://www.npmjs.com/package/ots-mongo-wrapper
- Repository: https://github.com/cletustboone/ots-mongo-wrapper
- Issues: https://github.com/cletustboone/ots-mongo-wrapper/issues
- npm.io page: https://npm.io/package/ots-mongo-wrapper

## Dependencies (1)

- [mongodb](https://npm.io/package/mongodb.md) ^2.0.42

## Recent versions

- 0.4.0 (latest) — 2015-09-01
- 0.3.4 — 2014-03-07
- 0.3.3 — 2014-03-05
- 0.3.1 — 2014-03-04
- 0.2.0 — 2014-02-20
- 0.1.1 — 2013-08-23
- 0.1.0 — 2013-08-13

## README

ots-mongo-wrapper
=================

Wraps the node MongoDB native driver in an event emitter that emits `connected` when the database connection is established.

`npm install ots-mongo-wrapper`

Connection Options
------------------

The constructor expects a connection object like this:
```JSON
{
  "host": "localhost",
  "port": 27021,
  "server_options": {
    "auto_reconnect": false,
    "poolSize": 20
  },
  "database": "DBNAME"
}
```
For a replica set, a connection object would look like this:
```JSON
{
  "servers": [
    {
      "host": "localhost",
      "port": 27017
    },
    {
      "host": "localhost",
      "port": 27018
    },
    {
      "host": "localhost",
      "port": 27019
    },
  ],
  "server_options": {
    "auto_reconnect": false,
    "poolSize": 20
  },
  "connection_options": "?replicaSet=REPLSETNAME",
  "database": "DBNAME"
}
```

The options object is formatted to a [standard MongoDB URL connection string](http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html).

How to interact with it
-----------------------
```JavaScript
var
dbs = require("./path-to-connection-file"),
env = process.env.NODE_ENV || null,
db  = require("ots-mongo-wrapper")(dbs[env]);

db.collection("collectionName").find().toArray(function( err, docs ) {
  // Do stuff with docs.
});
```
There's a race condition above where you can start receiving queries before a database connection is established. The module will simply log to stdout that a query to a collection was received before the database connection was ready. Once the database connection is established, you'll start getting results as normal. If you want to avoid the race condition, do the following:

```JavaScript
var
dbs = require("./path-to-connection-file"),
env = process.env.NODE_ENV || null,
db  = require("ots-mongo-wrapper")( dbs[env] );

// Wait for connected event.
db.on( "connected", function( readyDb ) {
    readyDb.collection("collectionName").find().toArray(function( err, docs ) {
        // Do stuff with docs.
    });
});
```

Multiple Connections
--------------------

You can connect to multiple databases. The module keeps a hash of connections. The hash key is made up of the options you pass when you require. If you need the database connection you already established in another module, just pass in the identical options and you will get that client instance back. A new instance will not be created.

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