https://github.com/luoyjx/koa-rest-mongoose
mongoose rest api generator for koa
https://github.com/luoyjx/koa-rest-mongoose
koa mongoose rest-api
Last synced: 27 days ago
JSON representation
mongoose rest api generator for koa
- Host: GitHub
- URL: https://github.com/luoyjx/koa-rest-mongoose
- Owner: luoyjx
- License: mit
- Created: 2016-08-29T14:16:06.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-05-25T02:28:09.000Z (almost 8 years ago)
- Last Synced: 2025-03-21T13:53:12.294Z (about 1 month ago)
- Topics: koa, mongoose, rest-api
- Language: JavaScript
- Homepage:
- Size: 23.4 KB
- Stars: 12
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# koa-rest-mongoose
[](https://www.npmjs.com/package/koa-rest-mongoose) [](https://travis-ci.org/luoyjx/koa-rest-mongoose)
mongoose rest generator for koa1
[](https://nodei.co/npm/koa-rest-mongoose/)
# Installation
```shell
npm install koa-rest-mongoose
```# Usage
```javascript
const koa = require('koa');
const mongoose = require('mongoose');
const KoaRestMongoose = require('koa-rest-mongoose');// 1 step, mongoose
const mongoUrl = '127.0.0.1:27017/koa_rest_mongoose';
const schema = new mongoose.Schema({
email: String,
name: String,
password: String,
address: String,
zipcode: Number,
lists: Array
});mongoose.connect(mongoUrl);
mongoose.model('user', schema);// 2 step, koa and router
const app = koa();
const rest = KoaRestMongoose({
prefix: '/api'
});
app.use(rest.routes());// 3 step, done
app.listen(process.env.PORT || 5000);
```
# TODO feature- [ ] middlewares support
# API
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. |