New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

koa-mongo-router

Package Overview
Dependencies
Maintainers
1
Versions
160
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-mongo-router

KOA REST API Router for MongoDB

  • 0.4.146
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

KOA REST API Router for MongoDB

A router that exposes a standard REST API for a MongoDB

Status: BETA

Build Status

  1. Usage
  2. Mongo Routes
  3. Query String

Usage

npm install koa-mongo-router
import { getDatabaseRouter } from 'koa-mongo-router'
import { IDatabaseRouterOptions } from 'koa-mongo-router/lib/database-router-options'

const databaseRouterOptions: IDatabaseRouterOptions = {
    permissionCheck: async (
        ctx: Koa.Context,
        next: () => Promise<any>,
        database: string,
        collection: string
    ) => {
        // Assumes you have middleware that already adds a user
        if (ctx.state.user == undefined) {
            ctx.status = 401
            return
        }

        // Example of validating if a user has read or write permissions
        switch (ctx.Method) {
            case "GET":
                if (!ctx.state.user.canRead(database, collection)) {
                    ctx.status = 403
                    return
                }
                break

            case "PUT":
            case "POST":
            case "PATCH":
            case "DELETE":
                if (!ctx.state.user.canWrite(database, collection)) {
                    ctx.status = 403
                    return
                }
                break
        }

        // If user haas permission for method, then continue on
        await next()
    }
};

const mongoRouter = getDatabaseRouter(databaseRouterOptions)

const app = new Koa()
    .use(mongoRouter.routes())
    .use(mongoRouter.allowedMethods())

Mongo Routes

MethodRouteDescriptionNotes
GET/Get databases
GET/:databaseGet database collections
DELETE/:databaseDelete database
GET/:database/:collectionGet collection itemsQuery String
POST/:database/:collectionCreate a collection item
PUT/:database/:collectionCreate or replace collection itemsQuery String Filtering
PATCH/:database/:collectionUpdate collection itemsQuery String Filtering
DELETE/:database/:collectionDelete collection itemsQuery String Filtering
GET/:database/:collection/:idGet a collection item
PUT/:database/:collection/:idCreate or replace a collection item
PATCH/:database/:collection/:idUpdate a collection item
DELETE/:database/:collection/:idDelete a collection item
GET/:database/:collection/schemaGet collection schema
PUT/:database/:collection/schemaPut collection schema
DELETE/:database/:collection/schemaDelete collection schema
GET/:database/:collection/indicesGet collection indices
POST/:database/:collection/indicesCreate collection index
DELETE/:database/:collection/indices/:nameDelete collection index

Get Items

Get items from a collection. Items can be filtered, paged, sorted, and counted using query string parameters.

RequestParametersNotes
MethodGET
Path/:database/:collection
ReturnsAn array of items
Codes200 Success
304 Not ModifiedConditional GET

Create An Item

Create a new item. This creates a new _id and assigns it to the item.

RequestParameters
MethodPOST
Path/:database/:collection
BodyThe item to create
ReturnsThe id of the created item
Status Codes201 Created

Create Or Replace Items

Create or replace items.

RequestParameters
MethodPUT
Path/:database/:collection
BodyAn array of items
Status Codes200 OK

Update Items

Update items.

RequestParameters
MethodUPDATE
Path/:database/:collection
BodyThe patch for the items
Status Codes200 OK

Delete Items

Delete items.

RequestParameters
MethodDELETE
Path/:database/:collection
Status Codes200 OK

Get An Item

Get an item.

RequestParameters
MethodGET
Path/:database/:collection/:id
Status Codes200 OK
404 Not Found

Get Or Replace An Item

Get or replace an item.

RequestParameters
MethodPUT
Path/:database/:collection/:id
BodyThe item
Status Codes200 OK
201 Created

Update An Item

Update an item.

RequestParameters
MethodPATCH
Path/:database/:collection/:id
BodyThe patch for the item
Status Codes200 OK
404 Not Found

Delete An Item

Delete an item.

RequestParameters
MethodDELETE
Path/:database/:collection/:id
Status Codes200 OK
404 Not Found

Query String

Query String Options

OptionDescriptionExample
$limitLimit the number of items?$limit=10
$skipSkip the given number of items?$skip=20
$fieldsReturn only specified fields?$fields=name,description
$sortSort on specified fields?$sort=name,-description
$countReturn the total count header?$count
$paginateReturn pagination header?$paginate

Query String Filtering

OperationQuery String
field exists?foo
field does not exist?!foo
field equals?foo=bar
field equals a string (don't cast)?foo:=bar
field does not equal?foo!=bar
field greater than?foo>10
field less than?foo<10
field greater than or equal to?foo>=10
field less than or equal to?foo<=10
field equals any of?foo=bar&foo=baz
field does not equal any of?foo!=bar&foo!=baz
field contains case-insensitive string?foo~=bar
field starts with case-insensitive string?foo^=bar
field ends with case-insensitive string?foo$=bar
record exists?!

Keywords

FAQs

Package last updated on 30 Jul 2020

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