Meteor-Community-Packages / meteor-collection-hooks

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

Add properties to each using before or after find #137

Closed alexdmejias closed 9 years ago

alexdmejias commented 9 years ago

I'm trying to use collections-hooks to attach properties to each document on before or after(I'm not sure which one I should be using) but I can't seem to be able to return an array with the new properties. I did have success with findOne

If I use something like

Residences.after.findOne(function (userId, selector, options, doc) {
  doc.testAfter = 'wasd' ;
});

a testAfter property is added to my document. The closest that I have gotten to getting this to work is to do something like

Residences.after.find(function(userId, selector, options, cursor) {
  var docs = cursor.fetch();
  docs.forEach(function(doc, index, arr) {
    docs[index].wasd = 'wasd';
  });

  console.log(docs) // docs with new properties attached. 
  return docs;
});

But when I check at the resulting api (using Restivus) nothing is attached (new properties are attaching when using findOne)

mizzao commented 9 years ago

You're adding the properties in Javascript objects in memory, where you should be editing them in the database.

The best thing to do would be to modify the docs before, so that you don't do two database operations for each document.

alexdmejias commented 9 years ago

is it possible to only attach the properties whenever they are requested? as opposed to saving them to the DB

mizzao commented 9 years ago

I don't understand what you mean by "whenever they are requested". I think you might be looking for the transform option of a collection, which is not the same thing as what this package is providing.

https://docs.meteor.com/#/full/mongo_collection

matb33 commented 9 years ago

It sounds like you're looking to transform your doc. See http://docs.meteor.com/#/full/find and look for transform

alexdmejias commented 9 years ago

yup, looks like I was referring to transform