-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
43 lines (37 loc) · 1.22 KB
/
index.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
var Future = Npm.require('fibers/future');
var originalOpen = MongoInternals.RemoteCollectionDriver.prototype.open;
MongoInternals.RemoteCollectionDriver.prototype.open = function(name) {
var self = this;
var ret = originalOpen.call(this, name);
ret._getDb = wrapWithDb(this.mongo);
return ret;
};
Mongo.Collection.prototype._getDb = function() {
if(typeof this._collection._getDb == 'function') {
return this._collection._getDb();
} else {
// if we can't find `_getDb()`, that means this is
// a collection created before initializing this collection
// if so, use the default mongo connection
// eg:- Meteor.users
var mongoConn = MongoInternals.defaultRemoteCollectionDriver().mongo;
return wrapWithDb(mongoConn);
}
};
Mongo.Collection.prototype._getCollection = function() {
var db = this._getDb();
return db.collection(this._name);
}
function wrapWithDb(mongoConn) {
// With Meteor 1.0.4, db creation is synchronous and wait in the connection
// constructor. So, we can get it like this.
if(mongoConn.db) {
return mongoConn.db;
}
// This is for Meteor versions older than 1.0.4
var f = new Future();
mongoConn._withDb(function(db) {
f.return(db);
});
return f.wait();
}