entropy is a simple RESTful server in front of a MongoDB instance using node.js, Express and Mongoose.
Clone the Github repository. Update the vendor libraries:
$> git clone git://github.com/pminnieur/entropy.js.git
$> sh update_vendors.sh
You can run an entropy instance just by starting it with the node
command:
node entropy.js
Copy the config.json.sample
file to config.json
and modify it to fit your
needs.
You can use entropy without any models, but I recommend to have a schema for
each of your models. You can put your Mongoose models in the /models
directory. Here's an example of an simple user model:
// models/user.js
var mongoose = require("mongoose").Mongoose;
mongoose.model("user", {
properties: ["username", "email"],
cast: {
username: String,
email: String
},
indexes: ["username", "email"]
});
That's it. You can now find, read, create, modify and remove user objects via REST, just by calling the appropriate URLs. The routing schema is very simple:
GET /:collection
find documents
GET /:collection/:id
read a document
POST /:collection?collection[field1]=value1&collection[field2]=value2...
create a document
POST /:collection/:id?collection[field1]=value1&collection[field2]=value2...
modify a document (collection[] parameter must match :collection)
DELETE /:collection/:id
remove a document
NOTE: the
collection[]
array must be named like the model you want to access, thus for creating or modifying a user you must use it like this:?user[username]=foo&user[email]=bar
You can specifiy what you want to find with the following parameters:
GET /:collection?query[field1]=value1&query[field2]=value2&...
GET /:collection?sort[field1]=asc|desc&sort[field2]=asc|desc&...
GET /:collection?limit=5&offset=5
If you want to overload the default behavior of the REST controllers, simply
put your own in the /controllers
directory. Here's an example for a customized
user controller which removes users' email address from the response:
// controllers/user.js
var app = modules.parent.exports.app,
db = modules.parent.exports.db;
app.get('/user', function(req, res) {
db.model('user').find().all(function(users) {
ret = [];
users.forEach(function(user) {
user = user.toObject();
delete user.email;
ret.push(user);
});
res.header('Content-Type', 'application/json');
res.send(ret, 200);
})
});
For the full copyright and license information, please view the LICENSE
file
that was distributed with this source code.