# koa-mongo-driver

> MongoDB middleware developed on top of official node-mongodb-native.

Latest version **1.0.2** (published 2018-06-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-mongo-driver
pnpm add koa-mongo-driver
yarn add koa-mongo-driver
bun add koa-mongo-driver
```

## 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.2 |
| Published | 2018-06-01 |
| First published | 2018-06-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | > 6 |
| Dependencies | 1 |
| Unpacked size | 13.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Caique Rodrigues |
| Maintainers | caiquerodrigues |
| Keywords | koa, middleware, mongo, mongodb, nosql |

## Links

- npm: https://www.npmjs.com/package/koa-mongo-driver
- Repository: https://github.com/caique/koa-mongo-driver
- Homepage: https://github.com/caique/koa-mongo-driver#readme
- Issues: https://github.com/caique/koa-mongo-driver/issues
- npm.io page: https://npm.io/package/koa-mongo-driver

## Dependencies (1)

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

## 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.2 (latest) — 2018-06-01
- 1.0.1 — 2018-06-01
- 1.0.0 — 2018-06-01
- 0.1.0 — 2018-06-01

## README

# koa-mongo-driver

[![Package Version][npm-badge]][npm-url]
[![Node Version][node-badge]][node-url]
[![Codecov][codecov-badge]][codecov-url]
[![Build][travis-badge]][travis-url]

>MongoDB middleware developed on top of official node-mongodb-native.

## Installation

#### NPM
```
npm install koa-mongo-driver --save
```

#### Yarn
```
yarn add koa-mongo-driver
```

*You will need a MongoDB instance running to be able to use this package.*

## Basic Usage
```js
const Koa = require('koa');
const app = new Koa();

const mongo = require('koa-mongo-driver');

app.use(mongo());
app.use(async (ctx) => {
  const collection = ctx.mongo.db('test').collection('objects');

  ctx.body = await collection.insertOne({ content: 'Just another object.' });
});

app.listen(3000);
```

See [spec/koa-example.js](./spec/koa-example.js) to check a simple Koa application.

## Custom URL Configurations
The `mongo()` function accepts as first argument an object that is used to define the connection URL for the MongoDB instance.

The `host`, `port`, and `databaseName` can be passed as described in the example below:

```js
const Koa = require('koa');
const app = new Koa();

const mongo = require('koa-mongo-driver');

const urlOptions = {
  url: 'my-mongodb-selfhosted.com',
  port: 12345,
  databaseName: 'myDatabase'
};

app.use(mongo(urlOptions));
app.use(async (ctx) => {
  const collection = ctx.mongo.db('test').collection('objects');

  ctx.body = await collection.insertOne({ content: 'Just another object.' });
});

app.listen(3000);
```

## Custom Connection Configurations
The `mongo()` function accepts as **second argument** an object that is used to define the *MongoClient* options.

Any configuration accepted by the `node-mongodb-native` can be passed. Check the available settings on [MongoClient API Docs by MongoDB](http://mongodb.github.io/node-mongodb-native/3.0/api/MongoClient.html#.connect).

The next example describes how to open an authenticated connection.

```js
const Koa = require('koa');
const app = new Koa();

const mongo = require('koa-mongo-driver');

const urlOptions = {
  url: 'my-mongodb-selfhosted.com',
  port: 12345,
  databaseName: 'myDatabase'
};

const connectionOptions = {
  auth: {
    user: 'root',
    password: 'toor'
  },
  authSource: 'admin',
  authMechanism: 'PLAIN'
};

app.use(mongo(urlOptions, connectionOptions));
app.use(async (ctx) => {
  const collection = ctx.mongo.db('test').collection('objects');

  ctx.body = await collection.insertOne({ content: 'Just another object.' });
});

app.listen(3000);
```

[npm-badge]: https://img.shields.io/npm/v/koa-mongo-driver.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/koa

[node-badge]: https://img.shields.io/node/v/koa-mongo-driver.svg?style=flat
[node-url]: http://nodejs.org/download/

[codecov-badge]: https://codecov.io/gh/caique/koa-mongo-driver/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/caique/koa-mongo-driver

[travis-badge]: https://travis-ci.org/caique/koa-mongo-driver.svg?branch=master
[travis-url]: https://travis-ci.org/caique/koa-mongo-driver

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