Skip to content

Commit

Permalink
fix(readme): Updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
James Talton authored and James Talton committed Feb 10, 2019
1 parent 2af4357 commit 0d779ce
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 34 deletions.
52 changes: 21 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
# KOA REST API Router for MongoDB

A router that exposes a standard REST API for a MongoDB.

## Status: BETA

[![Build Status](https://travis-ci.com/jamestalton/koa-mongo-router.svg?branch=master)](https://travis-ci.com/jamestalton/koa-mongo-router)

## Usage

```bash
npm install koa-mongo-router
```

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

## REST API

1. [REST Operations](#REST-Operations)
1. [Query String](#Query-String)
1. [Authentication]()

## REST Operations
### REST Operations

| Method | Route | Description |
| -----: | -------------------------- | -------------------------------------- |
Expand All @@ -24,7 +37,9 @@
| PATCH | /:database/:collection/:id | [Update an item](#Get-Item) |
| DELETE | /:database/:collection/:id | [Delete an item](#Get-Item) |

### Get Items
#### Get Items

Get items from a collection. Items can be filtered, paged, sorted, and counted using [query string](#Query-String) parameters.

| Request | Parameters | Notes |
| ------: | ---------------------- | --------------- |
Expand All @@ -34,13 +49,9 @@
| Codes | 200 Success |
| | 304 Not Modified | Conditional GET |

Get items from a collection. Items can be filtered, paged, sorted, and counted using [query string](#Query-String) parameters.

Example:

> GET /library/books?bookName=A Tale of Two Cities
#### Create An Item

### Create An Item
Create a new item. This creates a new \_id and assigns it to the item.

| Request | Parameters |
| -----------: | -------------------------- |
Expand All @@ -50,28 +61,7 @@ Example:
| Returns | The id of the created item |
| Status Codes | 201 Created |

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

## REST API Routes

| Method | Route | Body | Headers | Query | Description |
| -----: | ---------------- | ------ | ---------------- | ----- | ------------------------------------------ |
| GET | /:collection | | | | Get items |
| PUT | /:collection | Array | | | Replace collection |
| PUT | /:collection | Array | | ?! | Create or replace items |
| PUT | /:collection | Array | If-None-Match:\* | ?! | Create items that do not already exist. |
| PUT | /:collection | Array | If-Match:\* | ?! | Replace items only if item already exists |
| POST | /:collection | Object | | | Create item |
| PATCH | /:collection | Object | | | Update items |
| DELETE | /:collection | | | | Delete collection |
| GET | /:collection/:id | | | | Get Item |
| PUT | /:collection/:id | Object | | | Create or replace item |
| PUT | /:collection/:id | Object | If-None-Match:\* | | Create item if item does not already exist |
| PUT | /:collection/:id | Object | If-Match:\* | | Replace item if item already exists |
| PATCH | /:collection/:id | Object | | | Update item |
| DELETE | /:collection/:id | Object | | | Delete item |

## Query String Options
### Query String Options

| Option | Description | Example |
| ---------: | ------------------------------ | -------------------------- |
Expand All @@ -82,7 +72,7 @@ Create a new item. This creates a new id and assigns it to the item.
| \$count | Return the total count header | ?\$count |
| \$paginate | Return pagination header | ?\$paginate |

## Query String Filtering
### Query String Filtering

| Operation | Query String |
| ----------------------------------------: | ------------------ |
Expand Down
2 changes: 1 addition & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { createAppServer, shutdownAppServer } from './server'
let server: Server | Http2SecureServer

export async function startApp() {
const mongoRouter = await getMongoRouter()
const mongoRouter = getMongoRouter()

const app = new Koa()
.use(requestLogger)
Expand Down
2 changes: 1 addition & 1 deletion src/mongo-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export interface IMongoRouterOptions {
permissionCheck?: (database: string, collection: string) => Promise<boolean>
}

export async function getMongoRouter(options?: IMongoRouterOptions) {
export function getMongoRouter(options?: IMongoRouterOptions) {
const mongoRouter = new Router()

if (options != undefined && options.permissionCheck != undefined) {
Expand Down
2 changes: 1 addition & 1 deletion test/mongo-router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let request: axios.AxiosInstance
let server: Server | Http2SecureServer

beforeAll(async function() {
const mongoRouter = await getMongoRouter()
const mongoRouter = getMongoRouter()
const app = new Koa().use(mongoRouter.routes())
server = await createAppServer(app.callback())
const port = (server.address() as AddressInfo).port
Expand Down

0 comments on commit 0d779ce

Please sign in to comment.