forked from seriousManual/mockgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
128 lines (106 loc) · 3.33 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
var util = require('util')
var path = require('path')
var async = require('async')
var portfinder = require('portfinder')
var mongodb_prebuilt = require('mongodb-prebuilt');
var mongodb = require('mongodb')
var uid = require('uid')
var fs = require('fs')
var rmrf = require('rimraf')
var debug = require('debug')('mockgo')
var dbpath = path.join(__dirname, '.data-' + uid())
var dir = fs.mkdirSync(dbpath)
var connectionCache = {}
var serverConfig = null
var serverEmitter = null
const startServer = (callback) => {
portfinder.getPort((error, port) => {
if (error) {
return callback(error)
}
var config = {
host: '127.0.0.1',
port: port,
dbpath: dbpath
}
debug('startServer on port %d with data folder %s', port, dbpath)
serverEmitter = mongodb_prebuilt.start_server({
args: {
storageEngine: 'ephemeralForTest',
bind_ip: config.host,
port: config.port,
dbpath: config.dbpath
},
auto_shutdown: true
}, (error) => {
if (error === 'EADDRINUSE') {
setTimeout(() => startServer(callback), 100)
return
}
callback(error, config)
})
})
}
const createConnection = (config, callback) => {
var uri = util.format('mongodb://%s:%d/%s',
config.host,
config.port,
config.database
)
//we add the possibilty to override the version of the mongodb driver
//by exposing it via module.exports
module.exports.mongodb.connect(uri, callback)
}
const createServerSpecificConfiguration = (serverConfig, dbName, callback) => {
debug('creating connection for db "%s"', dbName)
var configCopy = Object.assign({}, serverConfig)
configCopy.database = dbName
createConnection(configCopy, (error, connection) => {
if (error) callback(error)
connectionCache[dbName] = connection
callback(null, connection)
})
}
const getConnection = (dbName, callback) => {
if (typeof dbName === 'function') {
callback = dbName
dbName = 'testDatabase'
}
var connection = connectionCache[dbName]
if (connection) {
debug('retrieve connection from connection cache for db "%s"', dbName)
return process.nextTick(() => callback(null, connection))
}
if (serverConfig) {
return createServerSpecificConfiguration(serverConfig, dbName, callback)
}
startServer((error, resultConfiguration) => {
if (error) return callback(error)
serverConfig = resultConfiguration
createServerSpecificConfiguration(serverConfig, dbName, callback)
})
}
const shutDown = callback => {
if (serverEmitter) {
debug('emit shutdown event')
serverEmitter.emit('mongoShutdown')
}
serverEmitter = null
serverConfig = null
connectionCache = {}
var cons = Object.keys(connectionCache).map(key => connectionCache[key])
if (cons.length > 0) {
debug('closing %d mongo connections', cons.length)
async.each(cons, (con, cb) => con.close(cb), callback)
} else {
process.nextTick(() => callback(null))
}
}
module.exports = {
getConnection,
shutDown,
mongodb: mongodb
}
process.on('exit', () => {
rmrf.sync(dbpath)
})