forked from seriousManual/mockgo
/
index.js
131 lines (112 loc) · 3.45 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
129
130
131
var util = require('util')
var path = require('path')
var async = require('async')
var portfinder = require('portfinder')
var mongodbPrebuilt = require('mongodb-prebuilt');
var mongodb = require('mongodb')
var debug = require('debug')('mockgo')
var connectionCache = {}
var maxRetries = 5
var serverConfig = null
var serverEmitter = null
var mongoClients = [];
const startServer = (callback, retries) => {
retries = retries || 0
portfinder.getPort((error, port) => {
if (error) return callback(error)
var config = {
host: '127.0.0.1',
port: port
}
debug('startServer on port %d', port)
serverEmitter = mongodbPrebuilt.start_server({
args: {
storageEngine: 'ephemeralForTest',
bind_ip: config.host,
port: config.port,
dbpath: path.join(__dirname, './.data')
},
auto_shutdown: true
}, error => {
if (error === 'EADDRINUSE' && retries < maxRetries) {
return setTimeout(() => startServer(callback, retries++), 200)
}
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.MongoClient.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, client) => {
mongoClients.push(client);
var db = client.db(dbName)
if (error) {
return callback(error)
}
connectionCache[dbName] = db
db.serverConfig = serverConfig
callback(null, db)
})
}
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 (typeof callback !== 'function') {
callback = () => {}
}
if (serverEmitter) {
debug('emit shutdown event')
serverEmitter.emit('mongoShutdown')
}
serverEmitter = null
serverConfig = null
connectionCache = {}
var closePromises = [];
var clients = mongoClients.splice(0);
for (var i=0; i<clients.length; i++) {
if (clients[i] && clients[i].close) {
closePromises.push(clients[i].close());
}
}
Promise.all(closePromises)
.then(() => callback(null))
.catch(err => callback(err))
}
module.exports = {
startServer,
serverConfig,
getConnection,
shutDown,
mongodb: mongodb
}