forked from seriousManual/mockgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
140 lines (119 loc) · 3.66 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
132
133
134
135
136
137
138
139
140
var util = require('util')
var path = require('path')
var async = require('async')
var portfinder = require('portfinder')
var { MongodHelper } = require('mongodb-prebuilt');
var mongodb = require('mongodb')
var debug = require('debug')('mockgo')
var connectionCache = {}
var maxRetries = 5
var serverConfig = null
var serverEmitter = null
var mongodHelper = null
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
}
mongodHelper = new MongodHelper(['--port', `${port}`]);
console.log('startServer on port %d', port)
mongodHelper.run().then((started) => {
console.log('mongod is running');
callback(null, config);
}, (e) => {
console.log('error starting', e);
callback(e, config);
});
/*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.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) {
return 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 (typeof callback !== 'function') {
callback = () => {}
}
callback();
/*mongodHelper.stop().
if (serverEmitter) {
debug('emit shutdown event')
serverEmitter.emit('mongoShutdown')
}
serverEmitter = null
serverConfig = null
var connections = Object.keys(connectionCache).map(key => connectionCache[key])
if (connections.length > 0) {
debug('closing %d mongo connections', connections.length)
async.each(connections, (con, cb) => con.close(cb), error => {
connectionCache = {}
callback(error)
})
} else {
process.nextTick(() => callback(null))
}
*/
}
module.exports = {
getConnection,
shutDown,
mongodb: mongodb
}