forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
101 lines (78 loc) · 1.8 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
/**
* Module dependencies.
*/
var mongoose = require('../')
, Mongoose = mongoose.Mongoose
, Collection = mongoose.Collection
, assert = require('assert')
, startTime = Date.now()
, queryCount = 0
, opened = 0
, closed = 0;
/**
* 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 = process.env.MONGOOSE_TEST_URI ||
'mongodb://localhost/mongoose_test'
}
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;
};
/**
* Module exports.
*/
module.exports.mongoose = mongoose;