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

resors

v0.2.0

Published

Simply write resources (for express and mongoose)

Downloads

4

Readme

#Resors

Simply write resource.

Resors

Resors writes REST resources for your mongoose models. It can be a one-liner, but there are planty of options you can set, and methods you can override, so you get exactly the resources you need.

Quick Example

You would need an express server, and some mongoose models.

// express
var app = require('express')();

// mongoose
var model = require('mongoose').model('users', {
    name: String,
    email: { type: String, required: true }
});

// resors!
app.use('/api', require('resors').middleware());

// run run run
require('http').createServer(app).listen(80);

That's it! On http://localhost/api you'd find a list of resources, and in http://localhost/api/users your users resource.

Options

By default, Resors only allow GET http method, but enabling other methods is easy, by adding an options object to the model. If you don't want the model having a Resors, set model.resors = false.

models.resors = {
    allow: [ 'get', 'post', 'put', 'delete' ], // default: ['get']
    fields: ['name', 'email'],
    filtering: [ 'name', 'name.full' ],
    sorting: 'name',
    
    // run this before each request
    before: function(req, res, next) {
        var resors = req.resors;

        // authentication
        if (!req.user.admin)
            res.authenticated = false;

        // validation or sanitation (use mongoose if you can!)
        if (resors.method('put')) {
            if (!req.body.email)
                resors.errors.push(['email', 'Email is required.']);
        }

        next();
    },
    
    // Play with mongoose query on GET requests
    query: function(req, res, next) {
        var q = res.query;

        // authorization
        if (req.user && !req.user.admin) {
            q = q.where('name', req.user.name);
        }

        res.query = q;
        next();
    },
    
    // runs after every request
    after: function(req, res, next) {
        console.log('after', res.result);
        next();
    }
};

Override

Inernally, Resors creates a MongooseResors instance for each, well, mongoose resource. If you would like to override one of its methods, you can do something like this:

var r = model.resors = new MongooseResors(model, {
    // options, same as above
});

r.create = function(req, res, next) {
    req.body.cool_field = 'hi there';
    MongooseResors.fn.create(req, res, next);
};

How does it works?

Resors is built on top of express.js, using connect middleware mechanism to function. Each request falls through the following series of middlewares:

init          ...

before        can be set via options
              usage: authentication, validation
              vars: req.resors, req.authenticated

error check   ...

route         (index, show, create, update, delete)

query         can be set via options
              (runs only for `index` and `show`)
              usage: populate, authorization
              vars: res.query

exec          (executes res.query, doesn't runs for `create`)

after         can be set via options
              usage: post-production
              vars: res.err, res.result

finish        (send res.err or err.result)

Each middleware receives req, res, and next as params, and this is set to the Resors instance. Therefore, next() will move to the next middleware, this.finish(req, res) will jump to the end, and res.json(false) will, e.g., return a negative response.

Sponsors