0.4.146 • Published 4 years ago

koa-mongo-router v0.4.146

Weekly downloads
570
License
ISC
Repository
github
Last release
4 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

4 years ago

0.4.145

4 years ago

0.4.144

4 years ago

0.4.143

4 years ago

0.4.142

4 years ago

0.4.141

4 years ago

0.4.140

4 years ago

0.4.139

4 years ago

0.4.138

4 years ago

0.4.137

4 years ago

0.4.136

4 years ago

0.4.135

4 years ago

0.4.134

4 years ago

0.4.133

4 years ago

0.4.132

4 years ago

0.4.131

4 years ago

0.4.130

4 years ago

0.4.129

4 years ago

0.4.128

4 years ago

0.4.127

4 years ago

0.4.126

4 years ago

0.4.125

4 years ago

0.4.124

4 years ago

0.4.123

4 years ago

0.4.122

4 years ago

0.4.121

4 years ago

0.4.120

4 years ago

0.4.119

4 years ago

0.4.118

4 years ago

0.4.117

4 years ago

0.4.116

4 years ago

0.4.115

4 years ago

0.4.114

4 years ago

0.4.113

4 years ago

0.4.112

4 years ago

0.4.111

4 years ago

0.4.110

4 years ago

0.4.109

4 years ago

0.4.108

4 years ago

0.4.107

4 years ago

0.4.106

4 years ago

0.4.105

4 years ago

0.4.104

4 years ago

0.4.103

4 years ago

0.4.102

4 years ago

0.4.101

4 years ago

0.4.100

4 years ago

0.4.97

4 years ago

0.4.98

4 years ago

0.4.99

4 years ago

0.4.96

4 years ago

0.4.95

4 years ago

0.4.94

4 years ago

0.4.93

4 years ago

0.4.92

4 years ago

0.4.91

4 years ago

0.4.90

4 years ago

0.4.89

4 years ago

0.4.88

4 years ago

0.4.87

4 years ago

0.4.86

4 years ago

0.4.85

4 years ago

0.4.84

4 years ago

0.4.83

4 years ago

0.4.82

4 years ago

0.4.81

4 years ago

0.4.80

4 years ago

0.4.79

4 years ago

0.4.78

4 years ago

0.4.77

4 years ago

0.4.76

4 years ago

0.4.75

4 years ago

0.4.74

4 years ago

0.4.73

4 years ago

0.4.72

4 years ago

0.4.71

4 years ago

0.4.70

4 years ago

0.4.69

4 years ago

0.4.68

4 years ago

0.4.67

4 years ago

0.4.64

4 years ago

0.4.65

4 years ago

0.4.63

4 years ago

0.4.66

4 years ago

0.4.62

4 years ago

0.4.61

4 years ago

0.4.60

4 years ago

0.4.59

5 years ago

0.4.58

5 years ago

0.4.57

5 years ago

0.4.56

5 years ago

0.4.55

5 years ago

0.4.54

5 years ago

0.4.53

5 years ago

0.4.52

5 years ago

0.4.51

5 years ago

0.4.50

5 years ago

0.4.49

5 years ago

0.4.48

5 years ago

0.4.47

5 years ago

0.4.46

5 years ago

0.4.45

5 years ago

0.4.44

5 years ago

0.4.43

5 years ago

0.4.42

5 years ago

0.4.41

5 years ago

0.4.40

5 years ago

0.4.39

5 years ago

0.4.38

5 years ago

0.4.37

5 years ago

0.4.36

5 years ago

0.4.35

5 years ago

0.4.34

5 years ago

0.4.33

5 years ago

0.4.32

5 years ago

0.4.31

5 years ago

0.4.30

5 years ago

0.4.29

5 years ago

0.4.28

5 years ago

0.4.27

5 years ago

0.4.26

5 years ago

0.4.25

5 years ago

0.4.24

5 years ago

0.4.23

5 years ago

0.4.22

5 years ago

0.4.21

5 years ago

0.4.20

5 years ago

0.4.19

5 years ago

0.4.18

5 years ago

0.4.17

5 years ago

0.4.16

5 years ago

0.4.15

5 years ago

0.4.14

5 years ago

0.4.13

5 years ago

0.4.12

5 years ago

0.4.11

5 years ago

0.4.10

5 years ago

0.4.9

5 years ago

0.4.8

5 years ago

0.4.7

5 years ago

0.4.6

5 years ago

0.4.5

5 years ago

0.4.4

5 years ago

0.4.3

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago