Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nswbmw/koa-mongo
MongoDB middleware for koa, support connection pool.
https://github.com/nswbmw/koa-mongo
Last synced: 25 days ago
JSON representation
MongoDB middleware for koa, support connection pool.
- Host: GitHub
- URL: https://github.com/nswbmw/koa-mongo
- Owner: nswbmw
- Created: 2014-08-05T06:45:56.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2021-01-18T02:02:57.000Z (almost 4 years ago)
- Last Synced: 2024-05-19T04:49:44.049Z (7 months ago)
- Language: JavaScript
- Size: 35.2 KB
- Stars: 146
- Watchers: 10
- Forks: 31
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Changelog: changelog.md
Awesome Lists containing this project
- awesome-koa - koa-mongo - MongoDB中间件,支持连接池。![](https://img.shields.io/github/stars/MangroveTech/koa-mongo.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/koa-mongo.svg?style=flat-square) (仓库 / 中间件)
README
## koa-mongo
koa-mongo is a mongodb middleware for koa@2, support connection pool.
### Install
```sh
npm i koa-mongo --save
```### Usage
```js
app.use(mongo({
host: 'localhost',
port: 27017,
user: 'admin',
pass: '123456',
db: 'test',
authSource: 'admin',
max: 100,
min: 1,
...
}));
```or
```js
app.use(mongo({
uri: 'mongodb://admin:123456@localhost:27017/test?authSource=admin', //or url
max: 100,
min: 1
...
}));
```or
```js
app.use(mongo({
uri: 'mongodb://admin:123456@localhost:27017/test?authSource=admin', //or url
max: 100,
min: 1
...
}, {
useUnifiedTopology: true
...
}
));
```defaultOptions:
```js
{
host: 'localhost',
port: 27017,
db: 'test',
authSource: 'admin',
max: 100,
min: 1,
acquireTimeoutMillis: 100
}
```More options see [generic-pool](https://github.com/coopernurse/node-pool).
### ctx.mongo & ctx.db
`ctx.mongo` is an instance of MongoClient.
```js
ctx.db === ctx.mongo.db(dbName)
```### Example
```js
const Koa = require('koa')
const mongo = require('koa-mongo')const app = new Koa()
app.use(mongo())
app.use(async (ctx, next) => {
// ctx.db === ctx.mongo.db('test')
const result = await ctx.db.collection('users').insert({ name: 'haha' })
const userId = result.ops[0]._id.toString()
ctx.body = await ctx.db.collection('users').find().toArray()
ctx.db.collection('users').remove({
_id: mongo.ObjectId(userId)
})
})
app.listen(3000, () => {
console.log('listening on port 3000')
})
```### [Mongolass](https://github.com/mongolass/mongolass)
Mongolass is a elegant MongoDB driver for Node.js.
### License
MIT