-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongeese.js
63 lines (53 loc) · 1.8 KB
/
mongeese.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
'use strict';
var mongoose = require('mongoose'),
mongeese;
module.exports = mongeese = {
// Our list of current connections
connections: {},
// Closing an existing connection
closeConnection: function (name) {
if (name !== undefined) {
mongeese.connections[name].base.disconnect();
} else { throw new Error('Connection name required.'); }
},
// Get all current connections
getAllConnections: function () {
return mongeese.connections;
},
// Getting an already established connection
getConnection: function (name) {
if (name !== undefined) {
return mongeese.connections[name];
} else { throw new Error('Connection name required.'); }
},
// Check for connection existence
connectionExists: function (name) {
if (name !== undefined) {
return mongeese.connections[name] != null;
} else { throw new Error('Connection name required.'); }
},
// Creating and returning a new connection
createConnection: function (name, uri, options) {
if (name !== undefined) {
mongeese.connections[name] = mongoose.createConnection(uri || 'mongodb://localhost/test', options || {});
return mongeese.connections[name];
} else { throw new Error('Connection name required.'); }
},
// Remove connection from pool
removeConnection: function (name) {
if (name !== undefined) {
if (delete mongeese.connections[name]) {
return true;
} else { return false; }
} else { throw new Error('Connection name required.'); }
},
// Close all current connections
closeAllConnections: function () {
try {
for (var connection in mongoose.connections) {
mongeese.connections[connection].base.disconnect();
}
return true;
} catch (err) { throw new Error('Unable to close connections: ' + err); }
}
};