How to detect if a new collection has been created?


#1

I use collection hooks to see if a new document has been inserted into a collection or if an existing one has been modified.

But is there any way to detect if I’ve created a new collection or removed a collection?

I’m thinking of an interval using https://github.com/dburles/mongo-collection-instances:

Mongo.Collection.getAll() // returns a list of all collections

Is there any better way than this? Like a collection created/deleted event?


#2

What’s the use case? In general you should not have to make a lot of collections automatically. But maybe you have an edge case.


#3

I keep my collections in a map and use a function get_collection to retrieve them

const collections = {}

export const get_collection = name => collections[name] || (collections[name] = new Mongo.Collection (name))

I also register them into a collection

export const register_collection = (name, permissions) => Collections.upsert(name, { permissions })

Which allows me to automatically publish them with the desired permissions

Collections.find().observe({
  added : c => {
    if (special.indexOf(c._id) < 0) {
      const access = c.permissions
      if (default_publish[access]) default_publish[access] (get_collection(c._id))
      else Meteor.Error (500, 'no_publish_function_defined', c._id)
    }
  }
})

special being the list of collections not to be auto-published


#4

@lucfranken I’m working on some automated analytics for Meteor.