Permalink
Browse files

jest api schema

  • Loading branch information...
ishai committed Apr 19, 2012
1 parent 3aa0994 commit 95d49b0fbff137f4fdab4c6352b9ecc7494b2178
Showing with 49 additions and 2 deletions.
  1. +33 −1 api.js
  2. +7 −0 mongoose_resource.js
  3. +1 −1 package.json
  4. +8 −0 resource.js
View
34 api.js
@@ -29,6 +29,7 @@ var Api = module.exports = Class.extend({
_.extend(this.settings, this.app.settings.api);
this.resources = [];
+ this.resources_schemas = [];
var self = this;
this.app.get('/' + this.path, function(req, res){
@@ -50,8 +51,38 @@ var Api = module.exports = Class.extend({
.join('')
.value();
- this.resources.push({name:name, url:resource.path});
+ resource.schema_path = _.chain([])
+ .push(this.path)
+ .push('schema/')
+ .push(name)
+ .join('')
+ .value();
+
+ this.resources.push({
+ name:name,
+ url:resource.path,
+ schema: resource.schema_path
+ });
+ this.resources_schemas.push({
+ name:name,
+ url:resource.path,
+ allowed_methods:resource.allowed_methods,
+ fields:resource.show_fields(),
+ update_fields:resource.show_update_fields(),
+ filtering : _.map(resource.filtering || {},function(value,key)
+ {
+ return { field : key, usage1: resource.path + '?' + key + '=<value>', usage2: resource.path + '?' + key + '__in=<value1>,<value2>'};
+ }),
+ sorting : resource.path + "?order_by=<field1>,<field2>"
+ });
+
+ var resource_index = this.resources.length -1;
+
+ var self = this;
+ this.app.get('/' + resource.schema_path,function(req,res){
+ res.json(self.resources_schemas[resource_index]);
+ });
this.app.resource(resource.path, (function(methods){
_.each(['show', 'index', 'create', 'update', 'destroy', 'load'], function(name) {
@@ -61,6 +92,7 @@ var Api = module.exports = Class.extend({
});
return methods;
})({}));
+
},
//Alias for register -Backword Compability
register_resource:function () {
View
@@ -14,6 +14,13 @@ var MongooseResource = module.exports = Resource.extend({
this.validation = new Validation(model);
},
+ show_fields : function(){
+ return this.fields || _.map(this.model.schema.tree,function(value,key)
+ {
+ return key;
+ });
+ },
+
get_object:function (req, id, callback) {
var query = this.model.findById(id);
this.authorization.limit_object(req, query, function (err, query) {
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.78",
+ "version":"0.0.79",
"author":"Ishai Jaffe <ishai@empeeric.com>",
"contributors": [
{ "name": "Alon Valadji", "email": "alon@empeeric.com" }
View
@@ -638,6 +638,14 @@ var Resource = module.exports = Class.extend({
return new_object;
},
+ show_fields:function(){
+ return this.fields || [];
+ },
+
+ show_update_fields:function() {
+ return this.update_fields || this.show_fields();
+ },
+
// Methods to implemenet

0 comments on commit 95d49b0

Please sign in to comment.