mongoose-restapi-ui
TypeScript icon, indicating that this package has built-in type declarations

1.8.0 • Public • Published

Mongoose API Generator with UI embedded

npm version Build Status NPM Status codecov.io Donate

This package provides a Rest API for your mongoose models, with the following endpoints:

  • GET /model (with querystring for each path, with additional parameters: $any: any colum, $sortBy: sort by a column, $sort: asc or desc and $page: number page)
  • GET /model/:_id
  • GET /model/:name
  • POST /model
  • PUT /model/:_id
  • PUT /model/:name
  • DELETE /model/:_id
  • DELETE /model/:name

Usage

Use our router extended from express with our custom methods:

import { ApiRouter } from 'mongoose-restapi-ui'
const customer = model('Customer', new Schema({
    name: { type: String, required: true },
    comment: { type: String }
}))
router.setModel('/customer', customer)
app.use('/', router)

In order to use on a different path, mark it on our ApiRouter

...
router.setGlobalRoute('/api/config')
...
router.setModel(...
router.setModel(...
app.use('/api/config', router)

Publish UI:

app.get('/api/ui', router.publishUiTree())

(Note that publishUI method don't need the global path, can be published on other site and accepts an optional parameter express.Router that will be switched if there are provided. If not are provided there are all models and UI on the same router.)

(NEW) Use permissions and roles

The library needs to pass your mongoose connection, and a middleware in order to get the user. Here an example:

Typescript:

import * as express from 'express'
import { ApiRouter } from 'mongoose-restapi-ui'
import { model, Schema, connect } from 'mongoose'
 
connect('mongodb://localhost:27017/dummyDatabase')
const customer = model('Customer', new Schema({
    name: { type: String, required: true },
    comment: { type: String }
}))
const app = express()
const router = ApiRouter()
router.use((req, res, next)=>{
    req.user = // your mongoose user document....
    next()
})
router.setModel('/customer', customer)
router.setConnection(mongoose) // or object returned from mongoose.connect
app.use('/', router)
app.listen(3000)

Javascript:

const express = require('express')
const { model, Schema, connect } = require('mongoose')
const { ApiRouter } = require('mongoose-restapi-ui')
 
connect('mongodb://localhost:27017/dummyDatabase')
 
const customer = model('Customer', new Schema({
    name: { type: String, required: true },
    comment: { type: String }
}))
 
const app = express()
const router = ApiRouter()
router.use((req, res, next)=>{
    req.user = // your mongoose user document....
    next()
})
router.setModel('/customer', customer)
router.setConnection(mongoose) // or object returned from mongoose.connect
app.use('/', router)
app.listen(3000)

UI integration

Use react component mongoose-restapi-ui-component.

API

Default object is an extended express Router, please initialize as express Router. (Default mongodb api is v3.x)

import ApiRouter from 'mongoose-restapi-ui'
const router = ApiRouter()

If your database is MongoDB 4.X, this library can use his new API ($any can filter numbers, _id, dates... as a contains)

import ApiRouter from 'mongoose-restapi-ui'
const router = ApiRouter({isMongo4: true})

This object has the same properties as router, with other ones:

Name Type Description
setGlobalRoute(path) path: string witch for nexts models that their api starts in path path.
setModel(route, model [, options]) route: string
model: mongoose.Model
options: ServeOptions
returns EventEmitter
Set model model on path route from the router. Generates GET, POST, PUT, PATCH and DELETE methods. Returns an EventEmitter that emits the following events:
  • add: Event that emits the new doc added to database.
  • update: Event that emits the updated doc. Emits an object with keys: old: old document, new: document updated
  • delete: Event that emits the deleted doc.
ServeOptions {
name: string
getFilterByPermissions: FilterByPermissions
hasAddPermission: RequestPermission
hasEditPermission: RequestPermission
hasUpdatePermission: RequestPermission
hasDeletePermission: RequestPermission
}
Switch path name as the name label for UI purpose as complex objects.
FilterByPermissions(req, callback) req: express.Request
callback: FilterByPermissionsCallback
Function in order to get a pre-filter query (query), for a custom permissions setup.
FilterByPermissionsCallback(err, query) err: Error
query: Object
Callback called in order to get a pre-filter query (query), for a custom permissions setup.
IUsermust be extend: { roles: ObjectId } & mongoose.DocumentUser used in order to set permissions and roles.
RequestPermission(user, doc, callback) user: IUser
doc: mongoose.Document
callback: RequestPermissionCallback
called on interaction with an endpoint rest (post, put, patch or delete)
RequestPermissionCallback(error, hasPermission, reason?) error: Error
hasPermission: Boolean
reason?: string
Will be called in order to custom permissions. Will be called second callback parameter with true or false as result of permission check. If there are provided the third parameter of callback and false are provided as result, will be sended it as custom statusText with status 403.

Next features

  • API rest self documented
  • Permissions and roles inheritance

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.8.0
    2
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.8.0
    2
  • 1.7.1
    0
  • 1.7.0
    0

Package Sidebar

Install

npm i mongoose-restapi-ui

Weekly Downloads

2

Version

1.8.0

License

MIT

Unpacked Size

664 kB

Total Files

72

Last publish

Collaborators

  • hector7