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 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongo-rest

v3.0.7

Published

A simple to use mongoose REST API.

Downloads

7

Readme

MongoREST Version 1.0.1

This is an express node module to provide basic REST support to access mongodb documents via mongoose.

I use semantic versioning and my tag script to tag this module.

Usage

The usage of this module is quite straight forward:

  1. Include and instantiate mongo-rest.
  2. Provide mongo-rest with the mongoose models you want to support.
  3. Create a view file for each resource you want rendered as HTML.
  4. Optionally you can also define interceptors in case you want some resources to be handled exceptionally.

That's it, you're done.

1. Including and instantiating mongo-rest

MongoREST exposes a class you instatiate with your options. The long version looks like this:

var MongoRest = require('mongo-rest')
  , mongoRest = new MongoRest(app, { viewPath: 'admin/resources/' });

The options for MongoRest are:

  • urlPath: The path were the REST interface is accessible. Defaults to /.
  • viewPath: The path were the views to render resources are located.
  • viewPrefix: The prefix of the resource views. Defaults to 'resource_'. So for example a list of users will use the view resource_users

As a one liner it looks like this:

var mongoRest = new (require('mongo-rest'))({ viewPath: 'admin/resources/' });

When instantiated, MongoREST registers the routes with the app so that all REST routes become accessible. If you provided '/resources/' as urlPath then following urls will become alive for the users resource:

GET: /resources/users (Renders a list of all users)
POST: /resources/users (Creates a new user)

GET: /resources/users/id/12345 (Renders the user with ID 12345)
PUT: /resources/users/id/12345 (Updates the user with ID 12345)
DELETE: /resources/users/id/12345 (Deletes the user with ID 12345)

2. Adding a mongoose model as resource

To tell mongo-rest which resources it should support you simple add each mongoose model. Normally you do this in the same place you define your routes. The code is quite straight forward:

mongoRest.addResource('users', require('../models/user'));

That's it. Now MongoREST nows that it has to use this model whenever the resource users is accessed.

3. Create your views

When you access /resources/users for example, MongoREST will try to render this list. To do this it will need a template files.

Two template files are needed for each resource to...

  1. ...render a list of the resource
  2. ...render a single resource

To define where the views are located you pass the viewPath option. If you pass resources_views/ as viewPath and resource_ as viewPrefix then MongoREST will use resources_views/resource_users as view for a list of users and resources_views/resource_users_show as view for a single user.

4. Create interceptors (Optional)

Sometimes some actions need to be taken before or after inserting, updating or deleting records.

You register an interceptor like this:

var eventName = 'post.success'
  , handler = function(info, req, res, next) { };

mongoRest.addInterceptor('users', eventName, handler);
// You can also provide the same handler for multiple event names:
mongoRest.addInterceptor('users', [ 'post', 'put' ], function() { });

The available event names are:

  • post, post.success, post.error Called when a new resource is posted.
  • put, put.success, put.error Called when a resource is updated.
  • delete, delete.success, delete.error Called when a resource is deleted.

If you simply use the event name without .success or .error it will be called before the event will be carried out.

The parameters provided to the handler are:

  • info An object containing the doc and or the values that will be used to update the record
  • req
  • res

An example of an interceptor could look like this:

/**
 * Intercepts posts and puts for guestbook-messages. It compiles the provided textSource with jade, and stores
 * the old textSource in a textVersions array to provide a history.
 */
mongoRest.addInterceptor('guestbook-messages', [ 'post', 'put' ], function(info) {
  // Compile the new textSource value with jade, and put the compiled code in textHtml
  info.values.textHtml = (jade.compile(info.values.textSource))({});
  // Since there is no existing doc when posting a new resource, we test if it exists...
  if (info.doc) {
    // ...and if it does we add the old textSource to the textVersions array to have a history.
    info.doc.textVersions.push(info.doc.textSource);
  }
});

Tagging

I use