Skip to content

Commit

Permalink
README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Alon committed Apr 5, 2012
1 parent 5a1f866 commit cd07869
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
89 changes: 89 additions & 0 deletions README.md
@@ -0,0 +1,89 @@
Jest
====

> JavaScriptational State Stasfer for node.js with easy generating resource from Mongoose ORM
#### #

introduction
------------
This module provides:
- Resource base class with:
- Authentication
- Authorization
- Pagination
- Cache
- Throttling
- Validation
- MongooseResource
- Resources listing

synopsis
--------

var express = require('express')
, app = express.createServer(),
, mongoose = require('mongoose')
, Jest = require('jest');

var Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/app');

// create mongoose model

var User = mongoose.model('user', new Schema({
username: {type: String, required: true},
email: String,
password: {type: String, validate: [function(v) { return true}, '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

var UserResource = Jest.MongooseResource.extend({
init: function(){
// call Jest.Resource constructor
// passing the Model User we created
this._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 expose
this.fields = {'username': true, 'credits': true, groups: {name: true, permissions: {name: true} }};

// use list or
this.update_fields = ['email', 'password'];

// specify base query for the model
this.default_query = function(query){
return query.where('credits').gte(10);
};

// specify which fields can be used to filter
this.filtering = {'credits': true};

// which http methods are allowed
this.allowed_methods = ['get', 'post', 'put'];
}
})

var api = new Jest.Api('api', app);

api.register('users', new UserResource());

installation
------------

$ npm install jest

documentation
-------------

There is none.
But there is an example, and a test.

And maybe one day will be...
2 changes: 1 addition & 1 deletion authentication.js
Expand Up @@ -5,7 +5,7 @@ var Authentication = module.exports = Class.extend({
},
// does the request is authenticated, callback false will return 401
is_authenticated:function (req, callback) {
callback(null,true);
callback(null, true);
},
// get a request identifier, uses for throttling (optional)
get_request_identifier:function (req) {
Expand Down
13 changes: 12 additions & 1 deletion mongoose_resource.js
Expand Up @@ -26,6 +26,7 @@ var MongooseResource = module.exports = Resource.extend({

get_objects:function (req, filters, sorts, limit, offset, callback) {
var self = this;

var query = this.default_query(this.model.find(this.default_filters));
var count_query = this.default_query(this.model.count(this.default_filters));

Expand Down Expand Up @@ -55,14 +56,19 @@ var MongooseResource = module.exports = Resource.extend({
console.log(typeof(query_value));
console.log(query_value);
}

var default_sort = query.options.sort || [];
query.options.sort = [];

for (var i = 0; i < sorts.length; i++)
query.sort(sorts[i].field, sorts[i].type);

for(var i=0; i<default_sort.length; i++)
query.options.sort.push(default_sort[i]);

query.limit(limit);
query.skip(offset);

var results = null, count = null;

function on_finish() {
Expand Down Expand Up @@ -90,7 +96,8 @@ var MongooseResource = module.exports = Resource.extend({
}
});
});
this.authorization.limit_object_list(req, count_query, function (err, count_query) {

self.authorization.limit_object_list(req, count_query, function (err, count_query) {
if (err) callback(err);
else
count_query.exec(function (err, counter) {
Expand All @@ -105,10 +112,13 @@ var MongooseResource = module.exports = Resource.extend({

create_obj:function (req, fields, callback) {
var self = this;

var object = new self.model();

for (var field in fields) {
object[field] = fields[field];
}

self.authorization.edit_object(req, object, function (err, object) {
if (err) callback(err);
else {
Expand All @@ -121,6 +131,7 @@ var MongooseResource = module.exports = Resource.extend({

update_obj:function (req, object, callback) {
var self = this;

self.authorization.edit_object(req, object, function (err, object) {
if (err) callback(err);
else {
Expand Down

0 comments on commit cd07869

Please sign in to comment.