/
mongodb.js
208 lines (181 loc) · 5.71 KB
/
mongodb.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* Mobius-JS
* /\ \
* / \ \
* / \ \
* / \ \
* / /\ \ \
* / / \ \ \
* / / \ \ \
* / / / \ \ \
* / / / \ \ \
* / / /---------' \
* / / /_______________\
* \ / /
* \/_____________________/
*
* Benjamin Coe (BenjaminCoe.com) - MIT Licensed.
*
* Description: A MongoDB database connector for Mobius-JS.
*/
// Dependencies.
var sys = require('sys');
var mongo = require('mongodb');
/**
* This patch addresses a problem caused by an incompatibility
* between the current version of the mongodb native driver and Node.js.
* For some reason the error return value is populated when it shouldn't be.
*
* @param {object} type
* @type bool
* @public
*/
process.EventEmitter.prototype.emit = function (type) {
// If there is no 'error' event listener then throw.
if (type === 'error') {
if (!this._events || !this._events.error ||
(this._events.error instanceof Array && !this._events.error.length))
{
if (arguments[1] instanceof Error) {
throw arguments[1];
} else {
throw new Error("Uncaught, unspecfied 'error' event.");
}
return false;
}
}
if (!this._events) return false;
if (!this._events[type]) return false;
if (typeof this._events[type] == 'function') {
if (arguments.length < 3) {
// fast case
this._events[type].call( this
, arguments[1]
, arguments[2]
);
} else {
// slower
var args = Array.prototype.slice.call(arguments, 1);
// My ugly monkey patch.
if (args[0] && args[0]['ok']) {
args[0] = null; // My monkey patch.
}
this._events[type].apply(this, args);
}
return true;
} else if (this._events[type] instanceof Array) {
var args = Array.prototype.slice.call(arguments, 1);
var listeners = this._events[type].slice(0);
for (var i = 0, l = listeners.length; i < l; i++) {
listeners[i].apply(this, args);
}
return true;
} else {
return false;
}
};
// End of crazy monkey patch that should be removed A.S.A.P.
// This class is created for documentation purposes.
function MongoDBConnector() {};
/**
* Connet to the MongoDB database.
*
* @param {object} dbConfiguration database configuration loaded in from db.json
* @param {function} callback callback function returns control to the processing-stack.
* @type void
* @public
*/
MongoDBConnector.prototype.connect = function(dbConfiguration, callback) {
var host = dbConfiguration['host'];
var port = dbConfiguration['port'];
sys.puts("Connecting to " + host + ":" + port);
var db = new mongo.Db(dbConfiguration['database'], new mongo.Server(host, port, {'autoReconnect' : true}), {});
db.open(function(err, db) {
callback(db);
});
}
exports.connect = MongoDBConnector.prototype.connect;
/**
* Close the MongoDB connection.
*
* @param {object} self reference to synchronous processing stack.
* @param {object} params unused.
* @param {function} callback callback for returning control to processing stack.
* @type void
* @public
*/
MongoDBConnector.prototype.close = function(self, params, callback) {
self.db.close();
callback();
}
exports.close = MongoDBConnector.prototype.close;
/**
* Create a new index on a MongoDB document.
*
* @param {object} self reference to synchronous processing stack.
* @param {object} params object that describes the indexes on a MongoDB document.
* @param {function} callback callback for returning control to processing stack.
* @type void
* @public
*/
MongoDBConnector.prototype.createIndex = function(self, params, callback) {
self.db.collection(params['collectionName'], function(err, collection) {
collection.createIndex(params['indexes'], function(err, indexName) {
sys.puts(indexName);
callback();
});
});
}
exports.createIndex = MongoDBConnector.prototype.createIndex;
/**
* Create a new MongoDB document.
*
* @param {object} self reference to synchronous processing stack.
* @param {object} params parameters for creating new model.
* @param {function} callback callback for returning control to processing stack.
* @type void
* @public
*/
MongoDBConnector.prototype.create = function(self, params, callback) {
self.db.collection(params['collectionName'], function(err, collection) {
var results = {};
// Insert the model.
collection.insert(params['values'], function(err, docs) {
/* docs.forEach(function(doc) {
sys.puts(sys.inspect(doc));
results[doc.name] = doc;
});*/
callback();
});
});
}
exports.create = MongoDBConnector.prototype.create;
/**
* Query for a MongoDB document.
*
* @param {object} self reference to synchronous processing stack.
* @param {object} params parameters for finding the document.
* @param {function} callback callback for returning control to processing stack.
* @type void
* @public
*/
MongoDBConnector.prototype.find = function(self, params, callback) {
// Used to return results back to a controller.
var callbackTemp = function(results) {
callback(results)
}
self.db.collection(params['collectionName'], function(err, collection) {
var results = [];
// Return the results.
collection.find(params['query'], params['sort'], function(err, cursor) {
cursor.each(function(err, article) {
if(article != null) {
results.push(article);
} else {
callbackTemp(results);
}
});
});
});
}
exports.find = MongoDBConnector.prototype.find;