Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve collection lookup #30

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mongo-instances-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,9 @@ Tinytest.add('users - can Mongo.Collection.get Meteor.users instance', function
test.instanceOf(Mongo.Collection.get('users'), Mongo.Collection);
test.instanceOf(Mongo.Collection.get('users'), Meteor.Collection);
});

Tinytest.add('ref - alternative name for collections', function (test) {
var collectionName = 'test' + test.id;
var refTest = new Mongo.Collection(null, { ref: collectionName });
test.equal(refTest, Mongo.Collection.get(collectionName));
});
54 changes: 41 additions & 13 deletions mongo-instances.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,55 @@
var instances = [];
var namedInstances = {};
var nullInstances = [];

CollectionExtensions.addExtension(function (name, options) {
instances.push({
name: name,
instance: this,
options: options
});
if (options && typeof(options.ref) == 'string') var name = options.ref;
this._ref = name;
if (name) {
if (!(name in namedInstances)) namedInstances[name] = [];
namedInstances[name].push({
name: name,
instance: this,
options: options
});
} else {
nullInstances.push({
instance: this,
options: options
});
}
});

Mongo.Collection.get = function(name, options) {
options = options || {};
var collection = _.find(instances, function(instance) {
if (options.connection)
return instance.name === name &&
instance.options && instance.options.connection._lastSessionId === options.connection._lastSessionId;
return instance.name === name;
});
if (name && name in namedInstances) {
var collection = _.find(namedInstances[name], function(instance) {
if (options.connection)
return instance.name === name &&
instance.options && instance.options.connection._lastSessionId === options.connection._lastSessionId;
return instance.name === name;
});
} else {
var collection = _.find(nullInstances, function(instance) {
if (options.connection)
return instance.options && instance.options.connection._lastSessionId === options.connection._lastSessionId;
return true;
});
}

return collection && collection.instance;
};

Mongo.Collection.getAll = function() {
return instances;
var results = [];
_.each(namedInstances, function(instances) {
_.each(instances, function(instance) {
results.push(instance);
});
});
_.each(nullInstances, function(instance) {
results.push(instance);
});
return results;
};

// Meteor.Collection will lack ownProperties that are added back to Mongo.Collection
Expand Down