Adding methods to the Query object from a plugin

125 views
Skip to first unread message

Charlie

unread,
Oct 31, 2012, 1:31:58 PM10/31/12
to mongoo...@googlegroups.com
I've been working on a couple of mongoose plugins where it would be great to be able to modify the query object by adding methods to it.  So it could be used as so with chaining custom methods:

Post.find({ author: 'John'})
  .limit(10)
  .customModifier('foo')
  .sort('created')
  .exec();

Is there a way of doing this currently, other than by adding methods directly to the Query.prototype?  That works but means that the query object is then modified for all models rather than just the models that have the plugin applied.

Maybe each model could have a createQuery() method which as a default implementation just returns a new Query().

This method could then be overridden by plugins or just by the model itself to augment the query object.  It could also be useful in the situation where you want to automatically add a condition to every query, e.g. to add a 'user.id' condition to every query.

Very happy to hear thoughts or suggestions on other/better ways of doing this.

Thanks

Aaron Heckmann

unread,
Oct 31, 2012, 1:54:38 PM10/31/12
to mongoo...@googlegroups.com
I have a project in the works for a cross-platform query builder which
supports this. I plan on introducing it into mongoose when its ready.
> --
> --
> http://mongoosejs.com - docs
> http://plugins.mongoosejs.com - plugins search
> http://github.com/learnboost/mongoose - source code
>
> You received this message because you are subscribed to the Google
> Groups "Mongoose Node.JS ORM" group.
> To post to this group, send email to mongoo...@googlegroups.com
> To unsubscribe from this group, send email to
> mongoose-orm...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/mongoose-orm?hl=en
>
>



--
Aaron
@aaronheckmann

Charles Davison

unread,
Oct 31, 2012, 1:57:20 PM10/31/12
to mongoo...@googlegroups.com
Sounds great, look forward to hearing more about it.

Cheers
Reply all
Reply to author
Forward
0 new messages