forked from mccormicka/Mockgoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mockgoose.js
148 lines (137 loc) · 4.58 KB
/
Mockgoose.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
141
142
143
144
145
146
147
148
'use strict';
var _ = require('lodash');
var mock = require('./lib/Model');
var db = require('./lib/db');
var logger = require('./lib/Logger');
module.exports = function (mongoose, throwErrors) {
var Models = {};
if (!mongoose.originalCreateConnection) {
mongoose.originalCreateConnection = mongoose.createConnection;
mongoose.originalConnect = mongoose.connect;
mongoose.originalModel = mongoose.model;
}
mongoose.model = function (name, schema, collection, skipInit) {
// Mongoose is case sensitive!
// if (name) {
// name = name.toLowerCase();
// }
var model = mongoose.originalModel.call(mongoose, name, schema, collection, skipInit);
mock(model);
if(model.schema.options.autoIndex){
model.ensureIndexes();
}
Models[name] = model;
return model;
};
mongoose.createConnection = function (host, database, port, options, callback) {
if (_.isFunction(database)) {
callback = database;
database = null;
}
if (!_.isString(database) && _.isString(host)) {
database = host.slice(host.lastIndexOf('/') + 1);
}
if (_.isFunction(database)) {
callback = database;
options = {};
} else if (_.isFunction(port)) {
callback = port;
options = {};
} else if (_.isFunction(options)) {
callback = options;
options = {};
}
if (_.isObject(options)) {
if (_.isString(options.db)) {
database = options.db;
}
options = {};
}
if (_.isUndefined(options)) {
options = {};
}
logger.info('Creating Mockgoose database: CreateConnection', database, ' options: ', options);
var connection = mongoose.originalCreateConnection.call(mongoose, database, options, function (err) {
process.nextTick(function() {
handleConnection(callback, connection, err);
});
});
connection.model = mongoose.model;
connection.models = mongoose.models;
return connection;
};
function handleConnection(callback, connection, err) {
setMockReadyState(connection, 2);
connection.emit('connecting');
if (callback) {
//Always return true as we are faking it.
callback(null, connection);
}
if (throwErrors) {
setMockReadyState(connection, 0);
connection.emit('error', err);
} else {
setMockReadyState(connection, 1);
connection.emit('connected');
connection.emit('open');
}
}
mongoose.connect = function (host, database, port, options, callback) {
if (_.isFunction(database)) {
callback = database;
database = null;
}
if (!_.isString(database)) {
database = host.slice(host.lastIndexOf('/') + 1);
}
if (_.isFunction(database)) {
callback = database;
options = {};
} else if (_.isFunction(port)) {
callback = port;
options = {};
} else if (_.isFunction(options)) {
callback = options;
options = {};
}
if (_.isObject(options)) {
if (_.isString(options.db)) {
database = options.db;
}
options = {};
}
if (_.isUndefined(options)) {
options = {};
}
logger.info('Creating Mockgoose database: Connect ', database, ' options: ', options);
mongoose.originalConnect(database, options, function (err) {
handleConnection(callback, mongoose.connection, err);
});
mongoose.connection.model = mongoose.model;
return mongoose;
};
var setMockReadyState = module.exports.setMockReadyState = function(connection, state) {
/**
* mock version of Connection#readyState
* http://mongoosejs.com/docs/api.html#connection_Connection-readyState
*
* 0 = disconnected
* 1 = connected
* 2 = connecting
* 3 = disconnecting
*
*/
connection._mockReadyState = mongoose.connection._mockReadyState = state;
};
module.exports.reset = function (type) {
if (!type) {
_.map(Models, function (value, key) {
delete Models[key];
});
} else {
delete Models[type];
}
db.reset(type);
};
return mongoose;
};