You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varqueries=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);
Sub-entities
varqueries=require('model-queries');Post.use(queries);Post.collection('forUser','/user/:id/post/all');Post.forUser({id: 123}).run(callback);//=> GET /user/123/post/all
Sub-entities with query strings
Post.forUser({id: 123}).query({p: 2,n: 20}).run(callback);//=> GET /user/123/post/all?p=2&n=20
Custom response parsing
varqueries=require('model-queries');Post.use(queries);/* for example, to parse nested entities */Post.endpoint('withComments',':id',function(res,fn){varcomments=res.body.comments;deleteres.body.comments;varcol=newCollection;for(vari=0,len=comments.length;i<len;++i){col.push(newComment(comments[i]));}varpost=newPost(res.body);post.comments=col;fn(null,post);}varcallback=function(err,post){/* post has comments collection */};Post.withComments({id: 123}).query({comments: 1}).run(callback);//=> GET /post/123?comments=1
API
Model.collection( method, [path] )
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.
Model.endpoint( method, [path], [parse] )
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.