Collections not in global scope anymore, what would be proper pattern to get them in Meteor 1.3


#1

How to get collection in some meteor package when I know only collection name?

In app:

import {doIt} from 'meteor/some:package'

doIt('CoolCollection')

In package:

export const doIt = (collection_name) => {
   // This worked before, but now collection is not in global scope
   const collection = global[collection_name] 
   ...
}

I know I could add collection into global but it doesn’t look right for me.

// /lib/collections.js
import { Mongo } from 'meteor/mongo'

export const CoolCollection = new Mongo.Collection('CoolCollection')

global.CoolCollection = CoolCollection  // This solves problem, but doesn't sound right :/

My question on StackOverflow:


#2

Thanks to @sashko recommendation I looked at https://github.com/dburles/mongo-collection-instances and here is how I soled 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
}
```

#3

Is there a way to do this on the client using just built in methods?