{"id":15918865,"url":"https://github.com/jamestalton/koa-mongo-router","last_synced_at":"2025-03-24T08:34:05.972Z","repository":{"id":40827560,"uuid":"169766113","full_name":"jamestalton/koa-mongo-router","owner":"jamestalton","description":"KOA Router REST API for MongoDB","archived":false,"fork":false,"pushed_at":"2023-01-06T01:39:16.000Z","size":3076,"stargazers_count":2,"open_issues_count":19,"forks_count":4,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-19T03:13:35.149Z","etag":null,"topics":["koa","mongo","mongodb","rest"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jamestalton.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-08T16:40:08.000Z","updated_at":"2020-07-30T16:38:59.000Z","dependencies_parsed_at":"2023-02-05T01:46:17.328Z","dependency_job_id":null,"html_url":"https://github.com/jamestalton/koa-mongo-router","commit_stats":null,"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamestalton%2Fkoa-mongo-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamestalton%2Fkoa-mongo-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamestalton%2Fkoa-mongo-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamestalton%2Fkoa-mongo-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jamestalton","download_url":"https://codeload.github.com/jamestalton/koa-mongo-router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245236002,"owners_count":20582348,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["koa","mongo","mongodb","rest"],"created_at":"2024-10-06T19:00:59.968Z","updated_at":"2025-03-24T08:34:05.674Z","avatar_url":"https://github.com/jamestalton.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# KOA REST API Router for MongoDB\n\nA router that exposes a standard REST API for a MongoDB\n\n## Status: BETA\n\n[![Build Status](https://travis-ci.com/jamestalton/koa-mongo-router.svg?branch=master)](https://travis-ci.com/jamestalton/koa-mongo-router)\n\n1. [Usage](#Usage)\n1. [Mongo Routes](#Mongo-Routes)\n1. [Query String](#Query-String)\n\n## Usage\n\n```bash\nnpm install koa-mongo-router\n```\n\n```TypeScript\nimport { getDatabaseRouter } from 'koa-mongo-router'\nimport { IDatabaseRouterOptions } from 'koa-mongo-router/lib/database-router-options'\n\nconst databaseRouterOptions: IDatabaseRouterOptions = {\n    permissionCheck: async (\n        ctx: Koa.Context,\n        next: () =\u003e Promise\u003cany\u003e,\n        database: string,\n        collection: string\n    ) =\u003e {\n        // Assumes you have middleware that already adds a user\n        if (ctx.state.user == undefined) {\n            ctx.status = 401\n            return\n        }\n\n        // Example of validating if a user has read or write permissions\n        switch (ctx.Method) {\n            case \"GET\":\n                if (!ctx.state.user.canRead(database, collection)) {\n                    ctx.status = 403\n                    return\n                }\n                break\n\n            case \"PUT\":\n            case \"POST\":\n            case \"PATCH\":\n            case \"DELETE\":\n                if (!ctx.state.user.canWrite(database, collection)) {\n                    ctx.status = 403\n                    return\n                }\n                break\n        }\n\n        // If user haas permission for method, then continue on\n        await next()\n    }\n};\n\nconst mongoRouter = getDatabaseRouter(databaseRouterOptions)\n\nconst app = new Koa()\n    .use(mongoRouter.routes())\n    .use(mongoRouter.allowedMethods())\n```\n\n## Mongo Routes\n\n| Method | Route                                | Description                                                    | Notes                                             |\n| -----: | ------------------------------------ | -------------------------------------------------------------- | ------------------------------------------------- |\n|    GET | /                                    | [Get databases]()                                              |                                                   |\n|    GET | /:database                           | [Get database collections]()                                   |                                                   |\n| DELETE | /:database                           | [Delete database]()                                            |                                                   |\n|    GET | /:database/:collection               | [Get collection items](#Get-Items)                             | [Query String](#Query-String)                     |\n|   POST | /:database/:collection               | [Create a collection item](#Create-An-Item)                    |\n|    PUT | /:database/:collection               | [Create or replace collection items](#Create-Or-Replace-Items) | [Query String Filtering](#Query-String-Filtering) |\n|  PATCH | /:database/:collection               | [Update collection items](#Update-Items)                       | [Query String Filtering](#Query-String-Filtering) |\n| DELETE | /:database/:collection               | [Delete collection items](#Delete-Items)                       | [Query String Filtering](#Query-String-Filtering) |\n|    GET | /:database/:collection/:id           | [Get a collection item](#Get-An-Item)                          |\n|    PUT | /:database/:collection/:id           | [Create or replace a collection item](#Get-Or-Replace-An-Item) |\n|  PATCH | /:database/:collection/:id           | [Update a collection item](#Update-An-Item)                    |\n| DELETE | /:database/:collection/:id           | [Delete a collection item](#Delete-An-Item)                    |\n|    GET | /:database/:collection/schema        | [Get collection schema](#)                                     |\n|    PUT | /:database/:collection/schema        | [Put collection schema](#)                                     |\n| DELETE | /:database/:collection/schema        | [Delete collection schema](#)                                  |\n|    GET | /:database/:collection/indices       | Get collection indices                                         |\n|   POST | /:database/:collection/indices       | Create collection index                                        |\n| DELETE | /:database/:collection/indices/:name | Delete collection index                                        |\n\n## Get Items\n\nGet items from a collection. Items can be filtered, paged, sorted, and counted using [query string](#Query-String) parameters.\n\n| Request | Parameters             | Notes           |\n| ------: | ---------------------- | --------------- |\n|  Method | GET                    |\n|    Path | /:database/:collection |\n| Returns | An array of items      |\n|   Codes | 200 Success            |\n|         | 304 Not Modified       | Conditional GET |\n\n## Create An Item\n\nCreate a new item. This creates a new \\_id and assigns it to the item.\n\n|      Request | Parameters                 |\n| -----------: | -------------------------- |\n|       Method | POST                       |\n|         Path | /:database/:collection     |\n|         Body | The item to create         |\n|      Returns | The id of the created item |\n| Status Codes | 201 Created                |\n\n## Create Or Replace Items\n\nCreate or replace items.\n\n|      Request | Parameters             |\n| -----------: | ---------------------- |\n|       Method | PUT                    |\n|         Path | /:database/:collection |\n|         Body | An array of items      |\n| Status Codes | 200 OK                 |\n\n## Update Items\n\nUpdate items.\n\n|      Request | Parameters              |\n| -----------: | ----------------------- |\n|       Method | UPDATE                  |\n|         Path | /:database/:collection  |\n|         Body | The patch for the items |\n| Status Codes | 200 OK                  |\n\n## Delete Items\n\nDelete items.\n\n|      Request | Parameters             |\n| -----------: | ---------------------- |\n|       Method | DELETE                 |\n|         Path | /:database/:collection |\n| Status Codes | 200 OK                 |\n\n## Get An Item\n\nGet an item.\n\n|      Request | Parameters                 |\n| -----------: | -------------------------- |\n|       Method | GET                        |\n|         Path | /:database/:collection/:id |\n| Status Codes | 200 OK                     |\n|              | 404 Not Found              |\n\n## Get Or Replace An Item\n\nGet or replace an item.\n\n|      Request | Parameters                 |\n| -----------: | -------------------------- |\n|       Method | PUT                        |\n|         Path | /:database/:collection/:id |\n|         Body | The item                   |\n| Status Codes | 200 OK                     |\n|              | 201 Created                |\n\n## Update An Item\n\nUpdate an item.\n\n|      Request | Parameters                 |\n| -----------: | -------------------------- |\n|       Method | PATCH                      |\n|         Path | /:database/:collection/:id |\n|         Body | The patch for the item     |\n| Status Codes | 200 OK                     |\n|              | 404 Not Found              |\n\n## Delete An Item\n\nDelete an item.\n\n|      Request | Parameters                 |\n| -----------: | -------------------------- |\n|       Method | DELETE                     |\n|         Path | /:database/:collection/:id |\n| Status Codes | 200 OK                     |\n|              | 404 Not Found              |\n\n## Query String\n\n### Query String Options\n\n|     Option | Description                    | Example                    |\n| ---------: | ------------------------------ | -------------------------- |\n|    \\$limit | Limit the number of items      | ?\\$limit=10                |\n|     \\$skip | Skip the given number of items | ?\\$skip=20                 |\n|   \\$fields | Return only specified fields   | ?\\$fields=name,description |\n|     \\$sort | Sort on specified fields       | ?\\$sort=name,-description  |\n|    \\$count | Return the total count header  | ?\\$count                   |\n| \\$paginate | Return pagination header       | ?\\$paginate                |\n\n### Query String Filtering\n\n|                                 Operation | Query String       |\n| ----------------------------------------: | ------------------ |\n|                              field exists | ?foo               |\n|                      field does not exist | ?!foo              |\n|                              field equals | ?foo=bar           |\n|        field equals a string (don't cast) | ?foo:=bar          |\n|                      field does not equal | ?foo!=bar          |\n|                        field greater than | ?foo\u003e10            |\n|                           field less than | ?foo\u003c10            |\n|            field greater than or equal to | ?foo\u003e=10           |\n|               field less than or equal to | ?foo\u003c=10           |\n|                       field equals any of | ?foo=bar\u0026foo=baz   |\n|               field does not equal any of | ?foo!=bar\u0026foo!=baz |\n|    field contains case-insensitive string | ?foo~=bar          |\n| field starts with case-insensitive string | ?foo^=bar          |\n|   field ends with case-insensitive string | ?foo\\$=bar         |\n|                             record exists | ?!                 |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamestalton%2Fkoa-mongo-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjamestalton%2Fkoa-mongo-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamestalton%2Fkoa-mongo-router/lists"}