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

restal

v0.1.1

Published

A Express restful api generator with Mongoose

Downloads

12

Readme

restal

This is a simple restful api generator with express & mongoose.

Install

$ npm i restal -S

Usage

// connect to mongodb and create a mongoose model
const mongoose = require('mongoose')
mongoose.Promise = Promise
mongoose.connect('mongodb://localhost/test', { useMongoClient: true })
const Post = new mongoose.Schema({
    title: String,
    content: { type: String, required: true },
    classification: String
})
const post = mongoose.model('post', Post)

// create a express app
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
app.use(bodyParser.json())

// import restal
const Restal = require('restal')
// create a restal instance with a model, and mount it on a uri
const postApis = new Restal(post, '/post')
// inject the apis into the express app
postApis.inject(app)

// listening
app.listen(8000)

Public Fields & Methods

| constructor | remark | | ------------------ | ---------------------------- | | Restal(model, uri) | create a restal api instance |

| field | remark | | ----- | ----------------------------------------- | | uri | the uri that restal instance mounted | | model | the model that restal instance associated |

| method | remark | | --------------------------- | ------------------------------------ | | inject(app) | inject the apis into the express app | | preHandle(method, handler) | unshift pre-hanlde middleware(s) | | postHandle(method, handler) | push post-hanlde middleware(s) |

APIs

APIs are generated by restal when the restal instance created. And the APIs support five HTTP methods listed below.

| method | query params | json body | remark | | ------ | ------------------- | ------------------- | -------------------------- | | GET | id, cond, skip, num | (none) | get document(s) | | POST | (none) | document | create document | | PUT | id | document | update the whole document | | PATCH | id | some document props | update some document props | | DELETE | id, cond | (none) | delete document(s) |

After injection, APIs are routed by the express app and become active when the app is listening. The request body type of POST, PUT and PATCH is json and all responses are in json. So, the express app need to use body-parser before being injected.

GET

| query params | type | requirement | remark | | ------------ | ------------- | ----------- | ------------------------------------------ | | id | ObjectID(_id) | optional | highest priority, ignore other params | | cond | json | optional | query object for mongodb, see mongodb manual | | skip | number | optional | number of documents skipped, default 0 | | num | number | optional | number of documents requested, default all |

No request body is needed. If all query params are missing, all documents of the collection will be responsed.

POST

No query params needed but request body (the document object to create) is required in json.

PUT

| query params | type | requirement | remark | | ------------ | ------------- | ----------- | ----------------------------------------- | | id | ObjectID(_id) | required | locate the document needed to be updated |

The request body (the document object to update) is also required in json. The offered document will overwrite the whole original document in mongodb.

PATCH

| query params | type | requirement | remark | | ------------ | ------------- | ----------- | ----------------------------------------- | | id | ObjectID(_id) | required | locate the document needed to be updated |

The request body (some document props to update) is also required in json. The offered document props will overwrite those of original document in mongodb.

DELETE

| query params | type | requirement | remark | | ------------ | ------------- | ----------- | ------------------------------------- | | id | ObjectID(_id) | required | highest priority, ignore "cond" param | | cond | json | optional | query object for mongodb, see mongodb manual |

No request body is needed. If all query params are missing, all documents of the collection will be deleted.

Extention

Features of express middleware are fully maintained in restal, and it's convenient to extend the restal instance handlers.

// import restal
const Restal = require('restal')
// create a restal instance with a model, and mount it on a uri
const postApis = new Restal(post, '/post')

//              ----+----+----+---------+----+----+----
// preHandle -> ... | f2 | f1 | handler | f1 | f2 | ... <- postHandle
//              ----+----+----+---------+----+----+----

// add middleware(s) to pre-hanlde or intercept the request
postApis.preHandle('get', (req, res, next) => {
    const postModel = postApis.model
    const postUri = postApis.uri
    // do something here like authorization check
    
    // if next misses, original handler will be blocked
    // and you need to reponse on your own
    next()
})
// add middleware to post-handle the result
postApis.postHandle('get', (result, req, res, next) => {
    const postModel = postApis.model
    const postUri = postApis.uri
    // do somethind here like data processing

    // if you push post handler, the orignal handler won't
    // reponse the request and pass the orignal reponse to
    // post handler with param "result"

    // you need to reponse on your own
    res.status(200).send(result)
})

// inject the apis into the express app
postApis.inject(app)