JavaScriptational State Transfer for node.js with easy generating resource from Mongoose ORM
This module provides Resource base class with:
- Authentication
- Authorization
- Pagination
- Cache
- Throttling
- Validation
- MongooseResource
- Resources listing
varexpress=require('express'),app=express(),Jest=require('jest'),mongoose=require('mongoose'),Schema=mongoose.Schema;mongoose.connect('mongodb://localhost/app');app.configure(function(){app.set('port',process.env.PORT||80);app.use(express.logger('dev'));app.use(express.bodyParser());app.use(express.methodOverride());app.use(app.router);});// create mongoose model
varUser=mongoose.model('user',newSchema({username: {type: String,required: true},email: String,password: {type: String,validate: [function(v){returntrue},'custom validate']},credits: {type: Number,min: 1,max: 230},role: {type: String,'default': 'user',enum: ['user','admin']},date: {type:Date,'default': Date.now},groups: [{name:String,permissions: [{name:String,expires:Date}]}]}));// create mongoose resource for User model
varUserResource=Jest.MongooseResource.extend({init: function(){// call Jest.Resource constructor// passing the Model User we createdthis._super(User);// use array to decide which fields will be visible by API// this.fields = ['username','credits'];// use tree object to decide recursively which fields to exposethis.fields={username: true,credits: true,groups: {name: true,permissions: {name: true}}};// use list orthis.update_fields=['email','password'];// specify base query for the modelthis.default_query=function(query){returnquery.where('credits').gte(10);};// specify which fields can be used to filterthis.filtering={credits: true};// which http methods are allowedthis.allowed_methods=['get','post','put'];}})varapi=newJest.Api('api',app);api.register('users',newUserResource());app.listen(app.get('port'),function(){console.log('express started on port %d',app.get('port'));})now go to http://localhost/api/ to see the api's and http://localhost/api/users to work with User model the Jest way.
$ npm install jest There is none. But there is an example, and a test.
And maybe one day will be...