Permalink
Browse files

or filter

not authenticated message
  • Loading branch information...
1 parent f9a0bc7 commit a639164a5d953fd0ca5d9d66d363025d6512ae77 @ishai ishai committed May 1, 2012
Showing with 70 additions and 31 deletions.
  1. +56 −19 mongoose_resource.js
  2. +1 −1 package.json
  3. +13 −11 resource.js
View
@@ -44,30 +44,67 @@ var MongooseResource = module.exports = Resource.extend({
var count_query = this.default_query(this.model.count(this.default_filters));
for (var filter in filters) {
- var splt = filter.split('__');
- var query_op = null;
- var query_key = filter;
- var query_value = filters[filter];
- if (splt.length > 1) {
- query_key = splt[0];
- query_op = splt[1];
- }
- if(self.model.schema.paths[query_key].options.type == Boolean)
- query_value = query_value.toLowerCase().trim() == 'true';
- if(self.model.schema.paths[query_key].options.type == Number)
- query_value = Number(query_value.trim());
- if(query_op)
+ if(filter == 'or')
{
- query.where(query_key)[query_op](query_value);
- count_query.where(query_key)[query_op](query_value);
+ var filter_value = _.map(filters[filter],function(or_filters)
+ {
+ var or_filter_value = {};
+ for (var filter in or_filters) {
+ var splt = filter.split('__');
+ var query_op = null;
+ var query_key = filter;
+ var query_value = or_filters[filter];
+ if (splt.length > 1) {
+ query_key = splt[0];
+ query_op = splt[1];
+ }
+ if(self.model.schema.paths[query_key])
+ {
+ if(self.model.schema.paths[query_key].options.type == Boolean)
+ query_value = query_value.toLowerCase().trim() == 'true';
+ if(self.model.schema.paths[query_key].options.type == Number)
+ query_value = Number(query_value.trim());
+ }
+ var current_or_filter_value = or_filter_value[query_key] || {};
+ if(query_op)
+ current_or_filter_value['$' + query_op] = query_value;
+ else
+ current_or_filter_value = query_value;
+ or_filter_value[query_key] = current_or_filter_value;
+ }
+ return or_filter_value;
+ });
+ console.log(filter_value);
+ query.or(filter_value);
}
else
{
- query.where(query_key, query_value);
- count_query.where(query_key, query_value);
+ var splt = filter.split('__');
+ var query_op = null;
+ var query_key = filter;
+ var query_value = filters[filter];
+ if (splt.length > 1) {
+ query_key = splt[0];
+ query_op = splt[1];
+ }
+ if(self.model.schema.paths[query_key])
+ {
+ if(self.model.schema.paths[query_key].options.type == Boolean)
+ query_value = query_value.toLowerCase().trim() == 'true';
+ if(self.model.schema.paths[query_key].options.type == Number)
+ query_value = Number(query_value.trim());
+ }
+ if(query_op)
+ {
+ query.where(query_key)[query_op](query_value);
+ count_query.where(query_key)[query_op](query_value);
+ }
+ else
+ {
+ query.where(query_key, query_value);
+ count_query.where(query_key, query_value);
+ }
}
- console.log(typeof(query_value));
- console.log(query_value);
}
var default_sort = query.options.sort || [];
View
@@ -1,7 +1,7 @@
{
"name":"jest",
"description":"JavaScriptational State Transfer. JS restful API layer with Mongoose based resources. Inspired by python Tastypie",
- "version":"0.0.87",
+ "version":"0.0.88",
"author":"Ishai Jaffe <ishai@empeeric.com>",
"contributors": [
{ "name": "Alon Valadji", "email": "alon@empeeric.com" }
View
@@ -352,19 +352,19 @@ var Resource = module.exports = Class.extend({
* @param object
* @param tree
*/
- dehydrate:function (object, tree) {
+ dehydrate:function (object, tree,parent_object) {
if(!object)
return object;
// if an array -> dehydrate each object independently
if (Array.isArray(object)) {
var objects = [];
for (var i = 0; i < object.length; i++) {
- objects.push(this.dehydrate(object[i], tree));
+ objects.push(this.dehydrate(object[i], tree,parent_object));
}
return objects;
}
if(typeof(object) == 'function')
- return object();
+ return object.call(parent_object);
// if basic type return as is
if (typeof(object) != 'object')
return object;
@@ -384,9 +384,9 @@ var Resource = module.exports = Class.extend({
for (var field in tree) {
// recursively dehydrate children
if (typeof(object.get) == 'function')
- new_object[field] = this.dehydrate(object.get(field) || object[field], tree[field]);
+ new_object[field] = this.dehydrate(object.get(field) || object[field], tree[field],object);
else
- new_object[field] = this.dehydrate(object[field], tree[field]);
+ new_object[field] = this.dehydrate(object[field], tree[field],object);
}
return new_object;
},
@@ -444,7 +444,7 @@ var Resource = module.exports = Class.extend({
self.internal_error(err, req, res);
else {
if (!is_auth) {
- self.unauthorized(res);
+ self.unauthorized(res,'not authenticated');
return;
}
@@ -547,8 +547,6 @@ var Resource = module.exports = Class.extend({
else
filters[field] = query[field];
}
- else
- continue;
// support 'in' query
if (operand == 'in')
filters[field] = query[field].split(',');
@@ -561,7 +559,9 @@ var Resource = module.exports = Class.extend({
filters['or'] = [];
for (var i = 0; i < or_filter.length; i++) {
if (or_filter[i] in filters) {
- filters['or'].push(filters[or_filter[i]]);
+ var qry = {};
+ qry[or_filter[i]] = filters[or_filter[i]];
+ filters['or'].push(qry);
delete filters[or_filter[i]];
}
}
@@ -570,8 +570,10 @@ var Resource = module.exports = Class.extend({
filters['nor'] = [];
for (var i = 0; i < nor_filter.length; i++) {
if (nor_filter[i] in filters) {
- filters['or'].push(filters[nor_filter[i]]);
- delete filters[or_filter[i]];
+ var qry = {};
+ qry[nor_filter[i]] = filters[nor_filter[i]];
+ filters['nor'].push(qry);
+ delete filters[nor_filter[i]];
}
}
}

0 comments on commit a639164

Please sign in to comment.