Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

restal

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

restal

A Express restful api generator with Mongoose

  • 0.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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

constructorremark
Restal(model, uri)create a restal api instance
fieldremark
urithe uri that restal instance mounted
modelthe model that restal instance associated
methodremark
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.

methodquery paramsjson bodyremark
GETid, cond, skip, num(none)get document(s)
POST(none)documentcreate document
PUTiddocumentupdate the whole document
PATCHidsome document propsupdate some document props
DELETEid, 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 paramstyperequirementremark
idObjectID(_id)optionalhighest priority, ignore other params
condjsonoptionalquery object for mongodb, see mongodb manual
skipnumberoptionalnumber of documents skipped, default 0
numnumberoptionalnumber 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 paramstyperequirementremark
idObjectID(_id)requiredlocate 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 paramstyperequirementremark
idObjectID(_id)requiredlocate 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 paramstyperequirementremark
idObjectID(_id)requiredhighest priority, ignore "cond" param
condjsonoptionalquery 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)

Keywords

FAQs

Package last updated on 27 Sep 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc