-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
77 lines (68 loc) · 2.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict'
const MongoDB = require('mongodb')
const MongoClient = MongoDB.MongoClient
const debug = require('debug')('koa-mongo')
const genericPool = require('generic-pool')
const muri = require('muri')
const defaultOptions = {
host: 'localhost',
port: 27017,
db: 'test',
authSource: 'admin',
max: 100,
min: 1,
acquireTimeoutMillis: 100
}
function mongo (connOptions, confOptions = {}) {
connOptions = Object.assign({}, defaultOptions, connOptions)
let mongoUrl = connOptions.uri || connOptions.url
let dbName = connOptions.db
if (!mongoUrl) {
if (connOptions.user && connOptions.pass) {
mongoUrl = `mongodb://${connOptions.user}:${connOptions.pass}@${connOptions.host}:${connOptions.port}/${connOptions.db}?authSource=${connOptions.authSource}`
} else {
mongoUrl = `mongodb://${connOptions.host}:${connOptions.port}/${connOptions.db}`
}
} else {
dbName = muri(mongoUrl.replace('+srv', '')).db
}
const mongoPool = genericPool.createPool({
create: () => MongoClient.connect(mongoUrl, Object.assign({
useNewUrlParser: true,
useUnifiedTopology: true
}, confOptions))
.then(client => {
debug('Successfully connected to: ' + mongoUrl)
return client
})
.catch(err => {
debug('Failed to connect to: ' + mongoUrl)
throw err
}),
destroy: client => client.close()
}, connOptions)
async function acquire () {
const resource = await mongoPool.acquire()
debug('Acquire one connection (min: %s, max: %s, poolSize: %s)', connOptions.min, connOptions.max, mongoPool.size)
return resource
}
async function release (resource) {
if (resource && !resource.isConnected()) {
await mongoPool.destroy(resource)
} else {
await mongoPool.release(resource)
}
debug('Release one connection (min: %s, max: %s, poolSize: %s)', connOptions.min, connOptions.max, mongoPool.size)
}
return async function koaMongo (ctx, next) {
ctx.mongo = await acquire()
ctx.db = ctx.mongo.db(dbName)
try {
await next()
} finally {
await release(ctx.mongo)
}
}
}
module.exports = mongo
Object.assign(module.exports, MongoDB)