forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.js
130 lines (100 loc) · 2.37 KB
/
common.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
/**
* Module dependencies.
*/
var mongoose = require('../')
, Mongoose = mongoose.Mongoose
, Collection = mongoose.Collection
, assert = require('assert')
, startTime = Date.now()
, queryCount = 0
, opened = 0
, closed = 0;
if (process.env.D === '1')
mongoose.set('debug', true);
/**
* Override all Collection related queries to keep count
*/
[ 'ensureIndex'
, 'findAndModify'
, 'findOne'
, 'find'
, 'insert'
, 'save'
, 'update'
, 'remove'
, 'count'
, 'distinct'
, 'isCapped'
, 'options'
].forEach(function (method) {
var oldMethod = Collection.prototype[method];
Collection.prototype[method] = function () {
queryCount++;
return oldMethod.apply(this, arguments);
};
});
/**
* Override Collection#onOpen to keep track of connections
*/
var oldOnOpen = Collection.prototype.onOpen;
Collection.prototype.onOpen = function(){
opened++;
return oldOnOpen.apply(this, arguments);
};
/**
* Override Collection#onClose to keep track of disconnections
*/
var oldOnClose = Collection.prototype.onClose;
Collection.prototype.onClose = function(){
closed++;
return oldOnClose.apply(this, arguments);
};
/**
* Create a connection to the test database.
* You can set the environmental variable MONGOOSE_TEST_URI to override this.
*
* @api private
*/
module.exports = function (options) {
options || (options = {});
var uri;
if (options.uri) {
uri = options.uri;
delete options.uri;
} else {
uri = module.exports.uri;
}
var noErrorListener = !! options.noErrorListener;
delete options.noErrorListener;
var conn = mongoose.createConnection(uri, options);
if (noErrorListener) return conn;
conn.on('error', function (err) {
assert.ok(err);
});
return conn;
};
/*!
* testing uri
*/
module.exports.uri = process.env.MONGOOSE_TEST_URI || 'mongodb://localhost/mongoose_test';
/**
* expose mongoose
*/
module.exports.mongoose = mongoose;
/**
* expose mongod version helper
*/
module.exports.mongodVersion = function (cb) {
var db = module.exports();
db.on('error', cb);
db.on('open', function () {
db.db.admin(function (err, admin) {
if (err) return cb(err);
admin.serverStatus(function (err, info) {
if (err) return cb(err);
var version = info.version.split('.').map(function(n){return parseInt(n, 10) });
cb(null, version);
});
});
})
}