meteormeteor-packages

How to get Collection using only collection name in Meteor 1.3?


In my project I'm using package which needs collection name to do some search inside collection. I just migrated into Meteor 1.3 and now this package doesn't work.

In the code package use something like:

const search = (collection_name) => {
   let collection = this[collection_name]
   ...
}

Now collection is not in global scope anymore. I could add my collection there doing global[collection_name] = Collection in my lib/collections.js, but I'd rather like to fix package to be more flexible and Meteor 1.3 compatible.

Are there any possibilities how to get Mongo Collection if you know only collection name?


Solution

  • Thanks to @sashko recommendation I looked at https://github.com/dburles/mongo-collection-instances and then to lai:collection-extensions and here is how I solved it:

    import { CollectionExtensions } from 'meteor/lai:collection-extensions'
    
    let registered_collections = {}
    CollectionExtensions.addExtension(function (name, options) {
      registered_collections[name] = {
        name: name,
        instance: this,
        options: options
      };
    });
    
    export function getCollectionByName(collecion_name) {
        return registered_collections[collecion_name].instance
    }