npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

express-mongoose-resource

v0.0.4

Published

Express resourceful routing for Mongoose models

Downloads

27

Readme

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       ->  destroy

where 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      ->  destroy

Content-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/index for the index action
  • forums/edit for the new and edit actions
  • forums/show for the show action

The context passed to the template contains the following keys:

  • model, the mongoose model
  • schema, the mongoose schema
  • modelName, the mongoose model name
  • resource_id, the action name
  • instance, the mongoose model instance (new, show and edit actions)
  • object, a plain JavaScript object corresponding to the mongoose model instance, as returned by mongoose toJSON() (new, show and edit actions)
  • instances, the mongoose result set (index action)
  • objects, an array of plain JavaScript objects corresponding to the mongoose result set (index action)
  • json, the JSON string representation of the model instance (new, show and edit actions) or of the result set (index action)

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.