0.4.146 • Published 5 years ago

koa-mongo-router v0.4.146

Weekly downloads
570
License
ISC
Repository
github
Last release
5 years ago

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?!
0.4.146

5 years ago

0.4.145

5 years ago

0.4.144

5 years ago

0.4.143

5 years ago

0.4.142

5 years ago

0.4.141

5 years ago

0.4.140

5 years ago

0.4.139

5 years ago

0.4.138

5 years ago

0.4.137

5 years ago

0.4.136

5 years ago

0.4.135

5 years ago

0.4.134

5 years ago

0.4.133

5 years ago

0.4.132

5 years ago

0.4.131

5 years ago

0.4.130

5 years ago

0.4.129

5 years ago

0.4.128

5 years ago

0.4.127

5 years ago

0.4.126

5 years ago

0.4.125

5 years ago

0.4.124

5 years ago

0.4.123

5 years ago

0.4.122

5 years ago

0.4.121

5 years ago

0.4.120

5 years ago

0.4.119

5 years ago

0.4.118

5 years ago

0.4.117

5 years ago

0.4.116

5 years ago

0.4.115

5 years ago

0.4.114

5 years ago

0.4.113

5 years ago

0.4.112

5 years ago

0.4.111

5 years ago

0.4.110

5 years ago

0.4.109

5 years ago

0.4.108

5 years ago

0.4.107

5 years ago

0.4.106

5 years ago

0.4.105

5 years ago

0.4.104

5 years ago

0.4.103

5 years ago

0.4.102

5 years ago

0.4.101

5 years ago

0.4.100

5 years ago

0.4.97

5 years ago

0.4.98

5 years ago

0.4.99

5 years ago

0.4.96

5 years ago

0.4.95

5 years ago

0.4.94

5 years ago

0.4.93

5 years ago

0.4.92

5 years ago

0.4.91

5 years ago

0.4.90

5 years ago

0.4.89

5 years ago

0.4.88

5 years ago

0.4.87

5 years ago

0.4.86

5 years ago

0.4.85

5 years ago

0.4.84

5 years ago

0.4.83

5 years ago

0.4.82

5 years ago

0.4.81

5 years ago

0.4.80

5 years ago

0.4.79

5 years ago

0.4.78

5 years ago

0.4.77

5 years ago

0.4.76

5 years ago

0.4.75

5 years ago

0.4.74

5 years ago

0.4.73

5 years ago

0.4.72

5 years ago

0.4.71

5 years ago

0.4.70

5 years ago

0.4.69

5 years ago

0.4.68

5 years ago

0.4.67

5 years ago

0.4.64

5 years ago

0.4.65

5 years ago

0.4.63

5 years ago

0.4.66

5 years ago

0.4.62

6 years ago

0.4.61

6 years ago

0.4.60

6 years ago

0.4.59

6 years ago

0.4.58

6 years ago

0.4.57

6 years ago

0.4.56

6 years ago

0.4.55

6 years ago

0.4.54

6 years ago

0.4.53

6 years ago

0.4.52

6 years ago

0.4.51

6 years ago

0.4.50

6 years ago

0.4.49

6 years ago

0.4.48

6 years ago

0.4.47

6 years ago

0.4.46

6 years ago

0.4.45

6 years ago

0.4.44

6 years ago

0.4.43

6 years ago

0.4.42

6 years ago

0.4.41

6 years ago

0.4.40

6 years ago

0.4.39

6 years ago

0.4.38

6 years ago

0.4.37

6 years ago

0.4.36

6 years ago

0.4.35

6 years ago

0.4.34

6 years ago

0.4.33

6 years ago

0.4.32

6 years ago

0.4.31

6 years ago

0.4.30

6 years ago

0.4.29

6 years ago

0.4.28

6 years ago

0.4.27

6 years ago

0.4.26

6 years ago

0.4.25

6 years ago

0.4.24

6 years ago

0.4.23

6 years ago

0.4.22

6 years ago

0.4.21

6 years ago

0.4.20

6 years ago

0.4.19

6 years ago

0.4.18

6 years ago

0.4.17

6 years ago

0.4.16

6 years ago

0.4.15

6 years ago

0.4.14

6 years ago

0.4.13

6 years ago

0.4.12

6 years ago

0.4.11

6 years ago

0.4.10

6 years ago

0.4.9

6 years ago

0.4.8

6 years ago

0.4.7

6 years ago

0.4.6

6 years ago

0.4.5

6 years ago

0.4.4

6 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago