https://github.com/duyet/koa-mongodb-rest
Rest API generation for Koa
https://github.com/duyet/koa-mongodb-rest
Last synced: 4 months ago
JSON representation
Rest API generation for Koa
- Host: GitHub
- URL: https://github.com/duyet/koa-mongodb-rest
- Owner: duyet
- Created: 2016-02-06T09:57:29.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T14:54:14.000Z (over 2 years ago)
- Last Synced: 2026-01-19T08:56:20.803Z (5 months ago)
- Language: JavaScript
- Homepage:
- Size: 31.3 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Koa mongo REST []
Easy REST api for [koa](http://koajs.com) server
[](https://nodei.co/npm/koa-mongodb-rest/)
## Installation
Install using npm:
```sh
npm install koa-mongodb-rest
```
## Usage
Require library
```javascript
generateApi = require('koa-mongodb-rest');
```
Create mongoose model
```javascript
mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1/db');
schema = new mongoose.Schema({
email: String,
name: String,
password: String,
address: String,
zipcode: Number,
lists: Array
});
model = mongoose.model('users', schema);
```
Create server
```javascript
var koa = require('koa');
var route = require('koa-route');
var app = koa();
//router is required
app.use(router(app));
//add REST routes to your app. Prefix is optional
generateApi(app, route, model, '/api');
app.listen(process.env.PORT || 5000);
```
Following REST API is now created for you:
| HTTP Verb | /users | /users/:id |
| ------------- | ------------- | --------------- |
| GET | Get all documents, or documents that match the query.
You can use [mongoose find conditions] (http://mongoosejs.com/docs/queries.html), limit, skip and sort.
For example:
**/api/users?conditions={"name":"john"}&limit=10&skip=1&sort=-zipcode** | Get the addressed document. |
| POST | Create a new document and send it back. | Update the addressed document with specified attributes. |
| PUT | Create a new document and send it back. | Replace the addressed document. |
| DELETE | n/a | Delete the addressed document. |
| PATCH | n/a | Update the addressed document with specified attributes. |