Permalink
Browse files

Replace _csview with standard '$b.' for all controller params; add se…

…arch
  • Loading branch information...
1 parent 5f46bb9 commit 026b69c2b2fc274b5769e1701e9b6c9ab21bf2fa @deitch committed Mar 18, 2014
Showing with 36 additions and 19 deletions.
  1. +9 −3 README.md
  2. +7 −5 lib/controller.js
  3. +1 −1 package.json
  4. +19 −10 test/booster-test.js
View
@@ -147,6 +147,12 @@ Many apps, rather than having the path `/comment/:comment` prefer the format `/c
booster supports the format extension out of the box. If you need access to the parameter in your controller, it is in `req.params.format`. Of course, it is optional!
+#### Search Params
+When you do a `GET` to a resource collection - e.g. `GET /comment` as opposed to `GET /comment/1` - you can pass search parameters to the query.
+
+**All** query parameters passed to the request will be passed to `model.find()`, and by extension `db.find()`, **except** for ones specific to the controller. Controller parameters always start with `$b.`, e.g. '$b.csview'.
+
+
#### Responses
Each type of http verb gives the appropriate response, with some options to change globally or per-request.
@@ -1143,14 +1149,14 @@ user.get("10",function(err,res){
});
````
-Second, the default controllers (**not** the models), filter it, unless you **explicitly** request it not to, using the query parameter `_csview`. Just set it to the value you want, e.g. `_csview=private` will show fields tagged `"private"` and those tagged `"public"` (or untagged, which is the same thing), but **not** those tagged `"secret"`. Not setting `_csview` (or setting it to `_csview=public` will show public fields only.
+Second, the default controllers (**not** the models), filter it, unless you **explicitly** request it not to, using the query parameter `$b.csview`. Just set it to the value you want, e.g. `$b.csview=private` will show fields tagged `"private"` and those tagged `"public"` (or untagged, which is the same thing), but **not** those tagged `"secret"`. Not setting `$b.csview` (or setting it to `$b.csview=public` will show public fields only.
-Sending `_csview=secret` will be ignored!
+Sending `$b.csview=secret` will be ignored!
Controllers:
````
GET /user/:user // defaults to getting just public or untagged fields
-GET /user/:user&_csview=private // returns private and public fields
+GET /user/:user&$b.csview=private // returns private and public fields
````
View
@@ -1,6 +1,6 @@
/*jslint node:true, nomen:true */
-var _ = require('lodash'), parseSearch, isUniqueIssue, isSendObject;
+var _ = require('lodash'), parseSearch, isUniqueIssue, isSendObject, CSVIEW = '$b.csview';
// regular expression
@@ -117,12 +117,14 @@ module.exports = {
next();
},
index: function(req,res,next) {
- var m = req.booster.models[name], filter = req.param("_csview") || "public";
+ var m = req.booster.models[name], filter = req.param(CSVIEW) || "public", search = _.omit(req.query,function (val,key) {
+ return(key.indexOf('$b.') === 0);
+ });
// we do NOT allow filter "secret"
if (filter === "secret") {
res.send(400);
} else {
- m.find({},req.booster.parent,function (err,data) {
+ m.find(search,req.booster.parent,function (err,data) {
if (err) {
res.send(400,err);
} else {
@@ -132,7 +134,7 @@ module.exports = {
}
},
show: function(req,res,next) {
- var m = req.booster.models[name], filter = req.param("_csview") || "public", keys = req.params[pathName].split(",");
+ var m = req.booster.models[name], filter = req.param(CSVIEW) || "public", keys = req.params[pathName].split(",");
// we do NOT allow filter "secret"
if (filter === "secret") {
res.send(400);
@@ -230,7 +232,7 @@ module.exports = {
}
},
getProperty: function (req,res,next) {
- var m = req.booster.models[name], filter = req.param("_csview") || "public", property = req.param("prop123var");
+ var m = req.booster.models[name], filter = req.param(CSVIEW) || "public", property = req.param("prop123var");
// we do NOT allow filter "secret"
if (filter === "secret") {
res.send(400);
View
@@ -1,7 +1,7 @@
{
"name": "booster",
"description": "Booster is the ***fastest*** way to get a full-fledged REST service up and running in [nodejs](http://nodejs.org)!",
- "version": "0.7.1",
+ "version": "1.0.0",
"url": "http://boosterjs.com",
"author": "Avi Deitcher <avi@deitcher.net>",
"engines": ["node >= 0.8"],
View
@@ -51,6 +51,15 @@ describe('booster',function () {
it('should map LIST',function (done) {
r.get('/post').expect(200,db.data("post")).end(done);
});
+ it('should map SEARCH for exact match',function (done) {
+ r.get('/post').query({title:"foobar"}).expect(200,db.data("post",{title:"foobar"})).end(done);
+ });
+ it('should map SEARCH for partial match',function (done) {
+ r.get('/post').query({title:"foo"}).expect(200,db.data("post",{title:"foo"})).end(done);
+ });
+ it('should map SEARCH for ignore-case match',function (done) {
+ r.get('/post').query({title:"FOObAR"}).expect(200,db.data("post",{title:"foobar"})).end(done);
+ });
it('should return 404 for unknown resource', function(done){
r.get('/poster').expect(404,done);
});
@@ -1471,25 +1480,25 @@ describe('booster',function () {
});
describe('via controllers', function(){
describe('for a LIST',function () {
- it('should get public data only without _csview', function(done){
+ it('should get public data only without csview', function(done){
r.get('/user').expect(200,publicdata).end(done);
});
- it('should get private data with _csview=private', function(done){
- r.get('/user').query({_csview:"private"}).expect(200,privatedata).end(done);
+ it('should get private data with csview=private', function(done){
+ r.get('/user').query({'$b.csview':"private"}).expect(200,privatedata).end(done);
});
- it('should reject request with _csview=secret', function(done){
- r.get('/user').query({_csview:"secret"}).expect(400).end(done);
+ it('should reject request with csview=secret', function(done){
+ r.get('/user').query({'$b.csview':"secret"}).expect(400).end(done);
});
});
describe('for a GET', function(){
- it('should get public data only without _csview', function(done){
+ it('should get public data only without csview', function(done){
r.get('/user/1').expect(200,publicdata[0]).end(done);
});
- it('should get private data with _csview=private', function(done){
- r.get('/user/1').query({_csview:"private"}).expect(200,privatedata[0]).end(done);
+ it('should get private data with csview=private', function(done){
+ r.get('/user/1').query({'$b.csview':"private"}).expect(200,privatedata[0]).end(done);
});
- it('should reject request with _csview=secret', function(done){
- r.get('/user/1').query({_csview:"secret"}).expect(400).end(done);
+ it('should reject request with csview=secret', function(done){
+ r.get('/user/1').query({'$b.csview':"secret"}).expect(400).end(done);
});
});

0 comments on commit 026b69c

Please sign in to comment.