# koa-mon

> koa mongoose middleware

Latest version **0.4.6** (published 2018-08-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-mon
pnpm add koa-mon
yarn add koa-mon
bun add koa-mon
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.6 |
| Published | 2018-08-03 |
| First published | 2018-06-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Zhang Kai Yu |
| Maintainers | cheunghy |
| Keywords | koa, mongoose, connector, koa2-mongoose, koa2, mongodb |

## Links

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

## Dependencies (2)

- [glob](https://npm.io/package/glob.md) >=7 <8
- [chalk](https://npm.io/package/chalk.md) >=2 <3

## 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

- 0.4.6 (latest) — 2018-08-03
- 0.4.5 — 2018-07-25
- 0.4.4 — 2018-07-05
- 0.4.3 — 2018-07-05
- 0.4.2 — 2018-07-03
- 0.4.1 — 2018-06-27
- 0.4.0 — 2018-06-27
- 0.3.4 — 2018-06-27
- 0.3.3 — 2018-06-27
- 0.3.2 — 2018-06-27
- 0.3.1 — 2018-06-26
- 0.3.0 — 2018-06-26
- 0.2.3 — 2018-06-25
- 0.2.2 — 2018-06-25
- 0.2.1 — 2018-06-25
- … 1 more at https://npm.io/package/koa-mon/versions

## README

# koa-mon
[![NPM version][npm-image]][npm-url] [![Dependency Status][daviddm-image]][daviddm-url]

koa-mon is a koa middleware for working with mongoose. Features including

1. Handles model loading
2. Handles mongoose connection including auto reconnects
3. Passing mongoose and models to koa context
4. Handles multiple mongoose connections
5. Nice database connection status logging with local timestamps
6. Load mongoose global plugins

## Installation

``` bash
npm install koa-mon --save
```

## Usage

### Single connection

For single connection, don't need to specify `connectionName`. By default,
mongoose's default connection is used. You can export your compiled models
directly.

``` js
const mongoose = require('koa-mon');

app.use(mongoose({
  modelDir: __dirname + '/models', // Where you models are defined
  url: 'mongodb://127.0.0.1:27017/your-db', // Mongoose connect url
  options: {}, // Mongoose connect options, omit this if it's just empty,
  debug: false // Should mongoose output debug messages, omit if false
}));
```

### Multiple connections

For multiple connections, you need to specify `connectionName` explicitly.
In your model definitions, you need to name it like `'User.js'` and export a
schema. Koa-mon handles the relationship between model and connections for you.

``` js
const mongoose = require('koa-mon');

app.use(mongoose({
  modelDir: __dirname + '/models1',
  url: 'mongodb://127.0.0.1:27017/db1',
  connectionName: 'db1'
}));

app.use(mongoose({
  modelDir: __dirname + '/models2',
  url: 'mongodb://127.0.0.1:27017/db2',
  connectionName: 'db2'
}));
```

To retrive your models:

``` js
app.use((ctx, next) => {
  const mongoose = ctx.mongoose;
  const { User1 } = ctx.db1;
  const { User2 } = ctx.db2;
  const { User1 } = ctx.connections.db1.models;
  const { User2 } = ctx.connections.db2.models;
});
```

To define your model:
``` js
// Post.js
const mongoose = require('mongoose');
const { Schema } = mongoose;

// You need to export the schema, do not use mongoose.model('Post', postSchema)
module.exports = new Schema({
  title: String,
  content: String
});
```


[npm-image]: https://badge.fury.io/js/koa-mon.svg
[npm-url]: https://npmjs.org/package/koa-mon
[daviddm-image]: https://david-dm.org/zhangkaiyulw/koa-mon.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/zhangkaiyulw/koa-mon

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