Custom endpoints for component/model
$ component install ericgj/model-queries
var queries = require('model-queries');
Post.use(queries);
Post.collection('page', 'all');
Post.page().query({page: 2, limit: 20}).run( callback );
//=> GET /post/all?page=2&limit=20
/*
* Or specify parameters separately, objects or strings
* Interface is identical to superagent `request.query`
*
*/
Post.page().query({page: 2}).query('limit=20').run( callback );
var queries = require('model-queries');
Post.use(queries);
Post.collection('forUser', '/user/:id/post/all');
Post.forUser({id: 123}).run( callback );
//=> GET /user/123/post/all
Post.forUser({id: 123}).query({p: 2, n: 20}).run( callback );
//=> GET /user/123/post/all?p=2&n=20
var queries = require('model-queries');
Post.use(queries);
/* for example, to parse nested entities */
Post.endpoint('withComments', ':id', function(res,fn){
var comments = res.body.comments;
delete res.body.comments;
var col = new Collection;
for (var i = 0, len = comments.length; i < len; ++i) {
col.push(new Comment(comments[i]));
}
var post = new Post(res.body);
post.comments = col;
fn(null, post);
}
var callback = function(err,post){ /* post has comments collection */ };
Post.withComments({id: 123}).query({comments: 1}).run( callback );
//=> GET /post/123?comments=1
Define a collection query endpoint for the model called as Model.method
.
When these queries are run, the response is parsed as a Collection of model
instances: identically to how Model.all
parses responses.
Define a query endpoint for the model with the given parse function for the responses. If no parse function is given, the response will be passed to the callback directly from superagent.
If path is not given, Model.url(method)
is the default. Note that any
path that doesn't start with "/" will be treated as relative to the model,
while any path that does start with "/" will be treated as absolute.
MIT