express-mongoose-resource v0.0.4
express-mongoose-resource
express-mongoose-resource provides resourceful routing for mongoose models to expressjs.
The library uses and extends express-resource, remaining fully compatible with it.
Install
npm install express-mongoose-resource
Usage
As with express-resource, simply require('express-mongoose-resource'), and resourceful routing will be available through the app.resource() method.
In addition to the usual express-resource usage and semantics of app.resource(), it's now also possible to simply pass a mongoose model to app.resource(), and
a new Resource object will be returned for the given model.
For instance, if we have a mongoose Forum model, calling
app.resource({model: Forum});will estabilish the default express-resource mapping (apart for the new schema action):
GET /forums/schema -> schema
GET /forums -> index
GET /forums/new -> new
POST /forums -> create
GET /forums/:forum -> show
GET /forums/:forum/edit -> edit
PUT /forums/:forum -> update
DELETE /forums/:forum -> destroywhere the :forum parameter is the mongoose ObjectId for the model instance.
Note that when model is not specified, app.resource() falls back to the standard express-resource implementation.
The format is determined using express-resource content negotiation, and if not specified it's assumed to be json.
All actions are automatically available for the json format.
It's also possible to nest resources:
var ForumSchema = new mongoose.Schema({
...
});
var Forum = db.model("Forum", ForumSchema);
var ThreadSchema = new mongoose.Schema({
forum: { type: mongoose.ObjectId, ref: 'Forum' },
...
});
var Thread = db.model("Thread", ThreadSchema);
...
var r_forum = app.resource({model: Forum});
var r_thread = app.resource({model: Thread});
r_forum.add(r_thread, {pivotField: 'forum'});which will a Thread resource nested under Forum:
GET /forums/:forum/threads/schema -> schema
GET /forums/:forum/threads -> index
GET /forums/:forum/threads/new -> new
POST /forums/:forum/threads -> create
GET /forums/:forum/threads/:forum -> show
GET /forums/:forum/threads/:forum/edit -> edit
PUT /forums/:forum/threads/:forum -> update
DELETE /forums/:forum/threads/:forum -> destroyContent-Negotiation
The format is determined using express-resource content negotiation, and if not specified it's assumed to be json.
HTML resources
The index, new, show and edit actions also support the html format. In this case, an expressjs template is rendered.
For the Forum example above, the templates would be:
forums/indexfor theindexactionforums/editfor thenewandeditactionsforums/showfor theshowaction
The context passed to the template contains the following keys:
model, the mongoose modelschema, the mongoose schemamodelName, the mongoose model nameresource_id, the action nameinstance, the mongoose model instance (new,showandeditactions)object, a plain JavaScript object corresponding to the mongoose model instance, as returned by mongoosetoJSON()(new,showandeditactions)instances, the mongoose result set (indexaction)objects, an array of plain JavaScript objects corresponding to the mongoose result set (indexaction)json, the JSON string representation of the model instance (new,showandeditactions) or of the result set (indexaction)
Bugs and pull requests
Please use the github repository to notify bugs and make pull requests.
License
This software is © 2012 Marco Pantaleoni, released under the MIT licence. Use it, fork it.
See the LICENSE file for details.