Meteor-Community-Packages / meteor-collection-hooks

Meteor Collection Hooks
https://atmospherejs.com/matb33/collection-hooks
MIT License
658 stars 90 forks source link

How i can extend ALL collections? #261

Closed MaxmaxmaximusGitHub closed 4 years ago

MaxmaxmaximusGitHub commented 4 years ago

Mongo.Collection.before.insert is not a function

SimonSimCity commented 4 years ago

Could you please explain with a bit more details what you're trying to do? Is this a bug, a feature request or rather a question about the package which would need some documentation?

MaxmaxmaximusGitHub commented 4 years ago

I try extend all collections.

evolross commented 4 years ago

Per the documentation, it looks like you can define a hook option globally to all collections, but not add an actual hook globally to all collections. Why would you want to add the same hook to every collection globally? Sounds like something more suited for the autovalue options of simpl-schema perhaps? Like adding default values (e.g. userId, timestamps, etc.) on inserts, updates, etc.

copleykj commented 4 years ago

You could extend Mongo.Collection directly and then use that to create your collection instances.

export Class MyCollection extends Mongo.Collection {
  insert(...args) {
    //do whatever before the insert happens
    super.insert(...args)
  }
}
import { MyCollection } from ' ./path/to/MyCollectionClass';

let extendedCollectionInstance = new MyCollection('test');

extendedCollectionInstance.insert({ test: true })
MaxmaxmaximusGitHub commented 4 years ago

@copleykj how i can extend existing collections, created not by me?

MaxmaxmaximusGitHub commented 4 years ago

solution:

for collection in Mongo.Collection.getAll()
  collection.before.insert # e.t.c.