CNode

koa-rest-mongoose,koa 1.x

分享
Lluoyjx发布于10 年前最后回复10 年前2 回复6616 浏览0 收藏

why

期初我就是想找个能根据model生成api的库,找到了个叫 koa-mongo-rest 的,貌似很久没维护了,现在koa-router的版本低了,所以我照着他搞了一个。 如果你们需要的话。

koa-rest-mongoose

NPM version Dependency Status Build Status

mongoose rest generator for koa

NPM

安装

Install using npm: npm install koa-rest-mongoose

示例

var koa = require('koa');
var router = require('koa-router')();

var koaRestMongoose = require('koa-rest-mongoose');

var mongoUrl = '127.0.0.1:27017/koa_rest_mongoose';
var mongoose = require('mongoose');
mongoose.connect(mongoUrl);

var schema = new mongoose.Schema({
  email: String,
  name: String,
  password: String,
  address: String,
  zipcode: Number,
  lists: Array
});

var model = mongoose.model('user', schema);

var app = koa();

koaRestMongoose(app, router, model, '/api');

app.listen(process.env.PORT || 5000);

Following REST API is now created for you:

HTTP Verb/users/users/:id
GETGet 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=-zipcodeGet the addressed document.
POSTCreate a new document and send it back.Update the addressed document with specified attributes.
PUTCreate a new document and send it back.Replace the addressed document.
DELETEn/aDelete the addressed document.
PATCHn/aUpdate the addressed document with specified attributes.

测试

npm test

推荐

sequelize

最后

欢迎 issues或者pr :)

查看回复

回复 (2)

C
crzidea#1·10 年前

推荐里竟然链接的是我们的koa-restql,哈哈哈,赞一个! https://github.com/Meituan-Dianping/koa-restql

L
luoyjx#2·10 年前
引用 crzidea推荐里竟然链接的是我们的koa restql,哈哈哈,赞一个! https://github.com/Meituan Dianping/koa restql

@crzidea 因为sequelize的你们写了我就先不造轮子了,如果哪天需求不能满足的话,再动手

参与回复
登录后即可参与回复。登录