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

restaman

v0.3.15

Published

Simple tool for expose mongoose models via REST api

Downloads

4

Readme

Restaman

Simple tool for expose mongoose model via REST api

Dependency Status npm

Requirements

  • node 5+
  • mongoose 4+
  • express
  • body-parser

Basic usage example

Assumed you have mongoose User model registered.

const Restaman = require('restaman');
const restaman = new Restaman();

restaman.addModel('User');
app.use('/api', restaman.router());

Now express app will handle REST routes for User model on /api/users endpoint

Exposing methods

Restaman allows expose model static methods using exposeStatic method of ModelWrapper instance.

const Restaman = require('restaman');
const restaman = new Restaman();
const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
    name: String;
});

userSchema.statics.someMethod(function(param1, param2) {
    return {message: `${param1} ${param2}`};
});

mongoose.model('User', userSchema);
restaman.addModel('User').exposeStatic('someMethod');
app.use('/api', restaman.router());

It will a create routes for User model on /api/users endpoint including POST /api/users/someMethod route; If we send POST request with data {param2: 'oh!', param1: 'ah'} to /api/users/someMethod it will return {message: 'ah oh!'}. Note, params handled by it names so they order no matter.

Hooks

Restaman provide easy way to transform request and response by using hooks. It includes pre and post types for init, create, find, findOne, delete, update, count actions.

Simple example for filtering docs by user:
const Restaman = require('restaman');
const restaman = new Restaman();

const postSchema = new mongoose.Schema({
    title: String,
    user: {
        type: Number,
        ref: 'User'
    }
});

mongoose.model('Post', postSchema);

const filterOwner = function(req, res, query) => {
    query.filter.user = req.user.id;
}
restaman.addModel('Post').pre('find', filterOwner);
app.use('/api', restaman.router());

Now for GET /api/users request it will add user field to query filter: Model.find({user: 123})

Excluding some fields from result docs:

const hideEmail = function(req, res, docs) => {
    docs.forEach(doc => delete doc.email)
}
restaman.addModel('User').post('find', hideEmail);

Note that find and findOne is distinct actions and uses each own hooks.

Dynamic DB switching example using pre init hook
restmean.addModel('User').pre('init', (req, res, params) => params.db = req.params.db);
app.use('/api/:db', restmean.router({mergeParams: true}));

Now REST requests will instantiate model from specified in request database (using useDb method), eg GET /api/test-db/users

Middleware

const requireAdmin = function(req, res, next){
    req.user.roles.indexOf('admin') === -1 ? res.sendStatus(401).end() : next();
}
restaman.addModel('Post').middleware(['create', 'update', 'delete'], requireAdmin);

More descriptions coming soon...

Query

Affected actions: find, findOne, count. Angular $http example:

$http.get('/api/posts', {
    params: {filter: {_id: 1}, projection: 'title', populate: 'user', limit: 3, skip: 5, sort: 'title'}
});

More descriptions coming soon...

API docs

Coming soon...

TODO

  • Add more tests
  • Expose instance methods