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