├── .gitignore ├── .travis.yml ├── Makefile ├── README.md ├── docs ├── coverage.html ├── docs.md ├── head.html ├── tail.html └── test.html ├── example ├── app.js └── coffee │ └── app.coffee ├── gulpfile.coffee ├── gulpfile.js ├── lib ├── actions.js ├── index.js ├── model.js └── routes.js ├── package.json └── test ├── index.coffee ├── mocha.opts └── server.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .DS_Store 3 | *.swp 4 | lib-cov/ 5 | *.log -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.11" 4 | services: 5 | - mongodb 6 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | TESTS = test/**/*.coffee 2 | REPORTER = dot 3 | 4 | test: 5 | @NODE_ENV=test NODE_TLS_REJECT_UNAUTHORIZED=0 ./node_modules/.bin/mocha \ 6 | --harmony \ 7 | --require should \ 8 | --require co-mocha \ 9 | --compilers coffee:coffee-script/register \ 10 | --reporter $(REPORTER) \ 11 | --timeout 5000 \ 12 | --growl \ 13 | $(TESTS) 14 | 15 | test-cov: lib-cov 16 | SUPERAGENT_COV=1 $(MAKE) test REPORTER=html-cov > docs/coverage.html 17 | 18 | lib-cov: 19 | jscoverage lib lib-cov 20 | 21 | test-server: 22 | @node --harmony example/app 23 | 24 | docs: test-docs 25 | 26 | md: test-md 27 | 28 | test-docs: 29 | make test REPORTER=doc \ 30 | | cat docs/head.html - docs/tail.html \ 31 | > docs/test.html 32 | 33 | test-md: 34 | make test REPORTER=markdown > docs/docs.md 35 | .PHONY: test-cov test docs test-docs 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Koa mongo REST [![NPM version](https://badge.fury.io/js/koa-mongo-rest.svg)](http://badge.fury.io/js/koa-mongo-rest) [![Dependency Status](https://gemnasium.com/t3chnoboy/koa-mongo-rest.svg)](https://gemnasium.com/t3chnoboy/koa-mongo-rest) [![Build Status](https://travis-ci.org/t3chnoboy/koa-mongo-rest.svg?branch=master)](https://travis-ci.org/t3chnoboy/koa-mongo-rest) 2 | 3 | Easy REST api for [koa](http://koajs.com) server 4 | 5 | [![NPM](https://nodei.co/npm/koa-mongo-rest.png?downloads=true)](https://nodei.co/npm/koa-mongo-rest/) 6 | 7 | 8 | 9 | ## Installation 10 | Install using npm: 11 | ```sh 12 | npm install koa-mongo-rest 13 | ``` 14 | 15 | ## Usage 16 | 17 | Require library 18 | ```javascript 19 | generateApi = require('koa-mongo-rest'); 20 | ``` 21 | 22 | Create mongoose model 23 | ```javascript 24 | mongoUrl = '127.0.0.1:27017'; 25 | mongoose = require('mongoose'); 26 | mongoose.connect(mongoUrl); 27 | 28 | schema = new mongoose.Schema({ 29 | email: String, 30 | name: String, 31 | password: String, 32 | address: String, 33 | zipcode: Number, 34 | lists: Array 35 | }); 36 | 37 | model = mongoose.model('users', schema); 38 | ``` 39 | 40 | Create server 41 | ```javascript 42 | var koa = require('koa'); 43 | var router = require('koa-router'); 44 | 45 | var app = koa(); 46 | 47 | //router is required 48 | app.use(router(app)); 49 | 50 | 51 | //add REST routes to your app. Prefix is optional 52 | generateApi(app, model, '/api'); 53 | 54 | app.listen(process.env.PORT || 5000); 55 | ``` 56 | 57 | Following REST API is now created for you: 58 | 59 | | HTTP Verb | /users | /users/:id | 60 | | ------------- | ------------- | --------------- | 61 | | 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. | 62 | | POST | Create a new document and send it back. | Update the addressed document with specified attributes. | 63 | | PUT | Create a new document and send it back. | Replace the addressed document. | 64 | | DELETE | n/a | Delete the addressed document. | 65 | | PATCH | n/a | Update the addressed document with specified attributes. | 66 | -------------------------------------------------------------------------------- /docs/coverage.html: -------------------------------------------------------------------------------- 1 | Coverage 35 |

Coverage

100%
34
34
0

index.js

100%
11
11
0
LineHitsSource
11var createModel, generateActions, generateRoutes;
2
31generateRoutes = require('./routes');
4
51generateActions = require('./actions');
6
71createModel = require('./model');
8
91module.exports = function(schema, mongoUrl) {
101 var actions, model;
111 model = createModel(schema, mongoUrl);
121 actions = generateActions(model);
131 model.generateApi = function(app) {
141 return generateRoutes(app, schema, actions);
15 };
161 return model;
17};

model.js

100%
7
7
0
LineHitsSource
11var createModel, mongoose;
2
31mongoose = require('mongoose');
4
51module.exports = createModel = function(schema, mongoUrl) {
61 var DocumentSchema;
71 mongoose.connect(mongoUrl);
81 DocumentSchema = new mongoose.Schema(schema.schema, {
9 collection: schema.collectionName,
10 versionKey: schema.versionKey
11 });
121 return mongoose.model(schema.collectionName, DocumentSchema);
13};

routes.js

100%
16
16
0
LineHitsSource
11var generateRoutes;
2
31module.exports = generateRoutes = function(app, schema, actions) {
41 app.get("/" + schema.collectionName + "/find", actions.findAll);
51 app.get("/" + schema.collectionName + "/find/:id", actions.findById);
61 app.get("/" + schema.collectionName + "/create", actions.createWithQuery);
71 app.get("/" + schema.collectionName + "/destroy/:id", actions.deleteById);
81 app.get("/" + schema.collectionName + "/update/:id", actions.updateByIdWithQuery);
91 app.get("/" + schema.collectionName + "/replace/:id", actions.replaceByIdWithQuery);
101 app.get("/" + schema.collectionName, actions.findAll);
111 app.get("/" + schema.collectionName + "/:id", actions.findById);
121 app.post("/" + schema.collectionName, actions.create);
131 app.post("/" + schema.collectionName + "/:id", actions.updateById);
141 app["delete"]("/" + schema.collectionName + "/:id", actions.deleteById);
151 app.put("/" + schema.collectionName + "/", actions.create);
161 app.put("/" + schema.collectionName + "/:id", actions.replaceById);
171 app.patch("/" + schema.collectionName + "/:id", actions.updateById);
18};
-------------------------------------------------------------------------------- /docs/docs.md: -------------------------------------------------------------------------------- 1 | # TOC 2 | - [REST API](#rest-api) 3 | - [Routes](#rest-api-routes) 4 | - [GET](#rest-api-routes-get) 5 | - [GET /:model](#rest-api-routes-get-get-model) 6 | - [GET /:model/:id](#rest-api-routes-get-get-modelid) 7 | - [POST](#rest-api-routes-post) 8 | - [POST /:model](#rest-api-routes-post-post-model) 9 | - [POST /:model/:id](#rest-api-routes-post-post-modelid) 10 | - [DELETE](#rest-api-routes-delete) 11 | - [DELETE /:model/:id](#rest-api-routes-delete-delete-modelid) 12 | - [PUT](#rest-api-routes-put) 13 | - [PUT /:model](#rest-api-routes-put-put-model) 14 | - [PUT /:model/:id](#rest-api-routes-put-put-modelid) 15 | - [PATCH](#rest-api-routes-patch) 16 | - [PATCH /:model/:id](#rest-api-routes-patch-patch-modelid) 17 | 18 | 19 | 20 | # REST API 21 | 22 | ## Routes 23 | 24 | ### GET 25 | 26 | #### GET /:model 27 | should respond with JSON for all records. 28 | 29 | ```js 30 | return request.get('/user').expect(200).expect('Content-Type', /json/).expect(users).end(done); 31 | ``` 32 | 33 | 34 | #### GET /:model/:id 35 | should respond with JSON for the record with the specified id. 36 | 37 | ```js 38 | return request.get('/user/2').expect(200).expect('Content-Type', /json/).expect({ 39 | name: 'Joff', 40 | age: 27, 41 | _id: 2 42 | }).end(done); 43 | ``` 44 | 45 | 46 | ### POST 47 | 48 | #### POST /:model 49 | should respond with JSON for the created record. 50 | 51 | ```js 52 | return request.post('/user').send({ 53 | name: 'James', 54 | age: 40, 55 | _id: 4 56 | }).expect(201).expect({ 57 | name: 'James', 58 | age: 40, 59 | _id: 4 60 | }).end(done); 61 | ``` 62 | 63 | 64 | #### POST /:model/:id 65 | should respond with JSON for the updated record. 66 | 67 | ```js 68 | return request.post('/user/2').send({ 69 | age: 28 70 | }).expect(200).expect({ 71 | name: 'Joff', 72 | age: 28, 73 | _id: 2 74 | }).end(done); 75 | ``` 76 | 77 | 78 | ### DELETE 79 | 80 | #### DELETE /:model/:id 81 | should respond with JSON for the destroyed record. 82 | 83 | ```js 84 | return request.del('/user/2').expect(200).expect({ 85 | name: 'Joff', 86 | age: 27, 87 | _id: 2 88 | }).end(done); 89 | ``` 90 | 91 | 92 | ### PUT 93 | 94 | #### PUT /:model 95 | should respond with JSON for the created record. 96 | 97 | ```js 98 | return request.put('/user').send({ 99 | name: 'John', 100 | age: 26, 101 | _id: 5 102 | }).expect(201).expect({ 103 | name: 'John', 104 | age: 26, 105 | _id: 5 106 | }).end(done); 107 | ``` 108 | 109 | 110 | #### PUT /:model/:id 111 | should return JSON for the replaced record. 112 | 113 | ```js 114 | return request.put('/user/2').send({ 115 | name: 'Joseph', 116 | age: 37 117 | }).expect(200).expect({ 118 | name: 'Joseph', 119 | age: 37, 120 | _id: 2 121 | }).end(done); 122 | ``` 123 | 124 | 125 | ### PATCH 126 | 127 | #### PATCH /:model/:id 128 | should respond with JSON for the updated record. 129 | 130 | ```js 131 | return request.patch('/user/2').send({ 132 | age: 28 133 | }).expect(200).expect({ 134 | name: 'Joff', 135 | age: 28, 136 | _id: 2 137 | }).end(done); 138 | ``` 139 | 140 | -------------------------------------------------------------------------------- /docs/head.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Koa mongo REST 5 | 6 | 17 | 36 | 37 | 38 |

Koa mongo REST

39 |

Rest api generation for koa server.

40 | -------------------------------------------------------------------------------- /docs/tail.html: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /docs/test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Koa mongo REST 5 | 6 | 17 | 36 | 37 | 38 |

Koa mongo REST

39 |

Rest api generation for koa server.

40 |
41 |

REST API

42 |
43 |
44 |

Routes

45 |
46 |
47 |

GET

48 |
49 |
50 |

GET /:model

51 |
52 |
should respond with JSON for all records
53 |
return request.get('/user').expect(200).expect('Content-Type', /json/).expect(users).end(done);
54 |
55 |
56 |
57 |

GET /:model/:id

58 |
59 |
should respond with JSON for the record with the specified id
60 |
return request.get('/user/2').expect(200).expect('Content-Type', /json/).expect({
 61 |   name: 'Joff',
 62 |   age: 27,
 63 |   _id: 2
 64 | }).end(done);
65 |
66 |
67 |
68 |
69 |
70 |

POST

71 |
72 |
73 |

POST /:model

74 |
75 |
should respond with JSON for the created record
76 |
return request.post('/user').send({
 77 |   name: 'James',
 78 |   age: 40,
 79 |   _id: 4
 80 | }).expect(201).expect({
 81 |   name: 'James',
 82 |   age: 40,
 83 |   _id: 4
 84 | }).end(done);
85 |
86 |
87 |
88 |

POST /:model/:id

89 |
90 |
should respond with JSON for the updated record
91 |
return request.post('/user/2').send({
 92 |   age: 28
 93 | }).expect(200).expect({
 94 |   name: 'Joff',
 95 |   age: 28,
 96 |   _id: 2
 97 | }).end(done);
98 |
99 |
100 |
101 |
102 |
103 |

DELETE

104 |
105 |
106 |

DELETE /:model/:id

107 |
108 |
should respond with JSON for the destroyed record
109 |
return request.del('/user/2').expect(200).expect({
110 |   name: 'Joff',
111 |   age: 27,
112 |   _id: 2
113 | }).end(done);
114 |
115 |
116 |
117 |
118 |
119 |

PUT

120 |
121 |
122 |

PUT /:model

123 |
124 |
should respond with JSON for the created record
125 |
return request.put('/user').send({
126 |   name: 'John',
127 |   age: 26,
128 |   _id: 5
129 | }).expect(201).expect({
130 |   name: 'John',
131 |   age: 26,
132 |   _id: 5
133 | }).end(done);
134 |
135 |
136 |
137 |

PUT /:model/:id

138 |
139 |
should return JSON for the replaced record
140 |
return request.put('/user/2').send({
141 |   name: 'Joseph',
142 |   age: 37
143 | }).expect(200).expect({
144 |   name: 'Joseph',
145 |   age: 37,
146 |   _id: 2
147 | }).end(done);
148 |
149 |
150 |
151 |
152 |
153 |

PATCH

154 |
155 |
156 |

PATCH /:model/:id

157 |
158 |
should respond with JSON for the updated record
159 |
return request.patch('/user/2').send({
160 |   age: 28
161 | }).expect(200).expect({
162 |   name: 'Joff',
163 |   age: 28,
164 |   _id: 2
165 | }).end(done);
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 | 175 | -------------------------------------------------------------------------------- /example/app.js: -------------------------------------------------------------------------------- 1 | koa = require('koa'); 2 | router = require('koa-router'); 3 | generateApi = require('../lib/index'); 4 | 5 | mongoUrl = '127.0.0.1:27017'; 6 | mongoose = require('mongoose'); 7 | mongoose.connect(mongoUrl); 8 | 9 | schema = new mongoose.Schema({ 10 | email: String, 11 | name: String, 12 | password: String, 13 | address: String, 14 | zipcode: Number, 15 | lists: Array 16 | }); 17 | 18 | app = koa(); 19 | app.use(router(app)); 20 | 21 | model = mongoose.model('user', schema); 22 | generateApi(app, model, '/api'); 23 | 24 | app.listen(process.env.PORT || 5000); 25 | -------------------------------------------------------------------------------- /example/coffee/app.coffee: -------------------------------------------------------------------------------- 1 | koa = require 'koa' 2 | router = require 'koa-router' 3 | generateApi = require '../lib/index' 4 | 5 | mongoUrl = '127.0.0.1:27017' 6 | mongoose = require 'mongoose' 7 | mongoose.connect mongoUrl 8 | 9 | schema = new mongoose.Schema 10 | email: String 11 | name: String 12 | password: String 13 | address: String 14 | zipcode: Number 15 | lists: Array 16 | 17 | app = koa() 18 | app.use router(app) 19 | 20 | model = mongoose.model 'user', schema 21 | generateApi app, model, '/api' 22 | 23 | app.listen(process.env.PORT || 5000) 24 | -------------------------------------------------------------------------------- /gulpfile.coffee: -------------------------------------------------------------------------------- 1 | gulp = require 'gulp' 2 | nodemon = require 'gulp-nodemon' 3 | coffeeES6 = require 'gulp-coffee-es6' 4 | 5 | paths = 6 | src : 'src/**/*.coffee' 7 | dest : 'lib' 8 | 9 | gulp.task 'compile', -> 10 | gulp.src paths.src 11 | .pipe coffeeES6 bare: yes 12 | .pipe gulp.dest paths.dest 13 | 14 | gulp.src 'example/coffee/**/*.coffee' 15 | .pipe coffeeES6 bare: yes 16 | .pipe gulp.dest 'example' 17 | 18 | gulp.task 'example-server', -> 19 | nodemon 20 | script: 'example/app.js' 21 | nodeArgs: ['--harmony'] 22 | ignore: [ 23 | './src/**' 24 | './test/**' 25 | './node_modules/**' 26 | ] 27 | 28 | gulp.task 'watch', -> 29 | gulp.watch paths.src, ['compile'] 30 | 31 | gulp.task 'default', ['compile', 'watch'] 32 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | require('coffee-script/register'); 2 | require('./gulpfile.coffee'); 3 | -------------------------------------------------------------------------------- /lib/actions.js: -------------------------------------------------------------------------------- 1 | module.exports = function(model) { 2 | return { 3 | findAll: function*(next) { 4 | yield next; 5 | var error, result; 6 | try { 7 | var conditions = {}; 8 | var query = this.request.query; 9 | if (query.conditions) { 10 | conditions = JSON.parse(query.conditions); 11 | } 12 | var builder = model.find(conditions); 13 | ['limit', 'skip', 'sort'].forEach(function(key){ 14 | if (query[key]) { 15 | builder[key](query[key]); 16 | } 17 | }) 18 | result = yield builder.exec(); 19 | return this.body = result; 20 | } catch (_error) { 21 | error = _error; 22 | return this.body = error; 23 | } 24 | }, 25 | findById: function*(next) { 26 | yield next; 27 | var error, result; 28 | try { 29 | result = yield model.findById(this.params.id).exec(); 30 | return this.body = result; 31 | } catch (_error) { 32 | error = _error; 33 | return this.body = error; 34 | } 35 | }, 36 | deleteById: function*(next) { 37 | yield next; 38 | var error, result; 39 | try { 40 | result = yield model.findByIdAndRemove(this.params.id).exec(); 41 | return this.body = result; 42 | } catch (_error) { 43 | error = _error; 44 | return this.body = error; 45 | } 46 | }, 47 | replaceById: function*(next) { 48 | yield next; 49 | var error, newDocument, result; 50 | try { 51 | yield model.findByIdAndRemove(this.params.id).exec(); 52 | newDocument = this.request.body; 53 | newDocument._id = this.params.id; 54 | result = yield model.create(newDocument); 55 | return this.body = result; 56 | } catch (_error) { 57 | error = _error; 58 | return this.body = error; 59 | } 60 | }, 61 | updateById: function*(next) { 62 | yield next; 63 | var error, result; 64 | try { 65 | result = yield model.findByIdAndUpdate(this.params.id, this.request.body, {new: true}).exec(); 66 | return this.body = result; 67 | } catch (_error) { 68 | error = _error; 69 | return this.body = error; 70 | } 71 | }, 72 | create: function*(next) { 73 | yield next; 74 | var error, result; 75 | try { 76 | result = yield model.create(this.request.body); 77 | this.status = 201; 78 | return this.body = result; 79 | } catch (_error) { 80 | error = _error; 81 | return this.body = error; 82 | } 83 | } 84 | }; 85 | }; 86 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | var generateActions, generateApi, generateRoutes; 2 | 3 | generateRoutes = require('./routes'); 4 | 5 | generateActions = require('./actions'); 6 | 7 | module.exports = generateApi = function(app, model, prefix) { 8 | var actions; 9 | if (prefix == null) { 10 | prefix = ''; 11 | } 12 | actions = generateActions(model); 13 | generateRoutes(app, model.modelName, actions, prefix); 14 | }; 15 | -------------------------------------------------------------------------------- /lib/model.js: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /lib/routes.js: -------------------------------------------------------------------------------- 1 | var generateRoutes, 2 | pluralize = require('pluralize'), 3 | bodyParser = require('koa-body-parser'); 4 | 5 | module.exports = generateRoutes = function(app, modelName, actions, prefix) { 6 | if (prefix == null) { 7 | prefix = ''; 8 | } 9 | modelName = pluralize(modelName); 10 | 11 | app.use(bodyParser()); 12 | 13 | app.get(prefix + ("/" + modelName), actions.findAll); 14 | app.get(prefix + ("/" + modelName + "/:id"), actions.findById); 15 | app.post(prefix + ("/" + modelName), actions.create); 16 | app.post(prefix + ("/" + modelName + "/:id"), actions.updateById); 17 | app.del(prefix + ("/" + modelName + "/:id"), actions.deleteById); 18 | app.put(prefix + ("/" + modelName), actions.create); 19 | app.put(prefix + ("/" + modelName + "/:id"), actions.replaceById); 20 | app.patch(prefix + ("/" + modelName + "/:id"), actions.updateById); 21 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "koa-mongo-rest", 3 | "version": "0.3.4", 4 | "description": "generate REST API with koa and mongo", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "test": "mocha --harmony" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git@github.com:t3chnoboy/koa-mongo-rest.git" 12 | }, 13 | "keywords": [ 14 | "koa", 15 | "REST", 16 | "API", 17 | "mongo", 18 | "mongodb", 19 | "mongoose", 20 | "es6", 21 | "generators", 22 | "database", 23 | "db" 24 | ], 25 | "author": "Dmitry Mazuro ", 26 | "license": "BSD-2-Clause", 27 | "bugs": { 28 | "url": "https://github.com/t3chnoboy/koa-mongo-rest/issues" 29 | }, 30 | "homepage": "https://github.com/t3chnoboy/koa-mongo-rest", 31 | "dependencies": { 32 | "koa-body-parser": "git://github.com/tomaash/koa-body-parser.git", 33 | "mongoose": "^4.0.2", 34 | "pluralize": "1.1.2" 35 | }, 36 | "devDependencies": { 37 | "co-mocha": "0.0.2", 38 | "coffee-script": "git://github.com/xixixao/coffee-script", 39 | "gulp": "^3.6.0", 40 | "gulp-coffee-es6": "git://github.com/t3chnoboy/gulp-coffee-es6.git", 41 | "gulp-nodemon": "^1.0.2", 42 | "koa": "^0.5.2", 43 | "koa-logger": "^1.2.0", 44 | "koa-router": "^3.1.2", 45 | "should": "^3.2.0-beta1", 46 | "supertest": "^0.10.0" 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /test/index.coffee: -------------------------------------------------------------------------------- 1 | server = require './server' 2 | model = server.model 3 | request = require('supertest').agent server.listen() 4 | 5 | users = [ 6 | name: 'Fronk' 7 | age : 28 8 | _id : 1 9 | , 10 | name: 'Joff' 11 | age : 27 12 | _id : 2 13 | , 14 | name: 'Scoobert' 15 | age : 54 16 | _id : 3 17 | ] 18 | 19 | describe 'REST API', -> 20 | 21 | describe 'Routes', -> 22 | 23 | beforeEach --> 24 | for user in users 25 | yield model.create user 26 | 27 | afterEach (done) -> 28 | model.remove {}, -> done() 29 | 30 | describe 'GET', -> 31 | 32 | describe 'GET /:model', -> 33 | it 'should respond with JSON for all records', (done) -> 34 | request 35 | .get '/users' 36 | .expect 200 37 | .expect 'Content-Type', /json/ 38 | .expect users 39 | .end done 40 | 41 | describe 'GET /:model/:id', -> 42 | it 'should respond with JSON for the record with the specified id', (done) -> 43 | request 44 | .get '/users/2' 45 | .expect 200 46 | .expect 'Content-Type', /json/ 47 | .expect 48 | name : 'Joff' 49 | age : 27 50 | _id : 2 51 | .end done 52 | 53 | describe 'POST', -> 54 | 55 | describe 'POST /:model', -> 56 | it 'should respond with JSON for the created record', (done) -> 57 | request 58 | .post '/users' 59 | .send 60 | name : 'James' 61 | age : 40 62 | _id : 4 63 | .expect 201 64 | .expect 65 | name : 'James' 66 | age : 40 67 | _id : 4 68 | .end done 69 | 70 | describe 'POST /:model/:id', -> 71 | it 'should respond with JSON for the updated record', (done) -> 72 | request 73 | .post '/users/2' 74 | .send 75 | age : 28 76 | .expect 200 77 | .expect 78 | name : 'Joff' 79 | age : 28 80 | _id : 2 81 | .end done 82 | 83 | 84 | describe 'DELETE', -> 85 | 86 | describe 'DELETE /:model/:id', -> 87 | it 'should respond with JSON for the destroyed record', (done) -> 88 | request 89 | .del '/users/2' 90 | .expect 200 91 | .expect 92 | name : 'Joff' 93 | age : 27 94 | _id : 2 95 | .end done 96 | 97 | describe 'PUT', -> 98 | 99 | describe 'PUT /:model', -> 100 | it 'should respond with JSON for the created record', (done) -> 101 | request 102 | .put '/users' 103 | .send 104 | name : 'John' 105 | age : 26 106 | _id : 5 107 | .expect 201 108 | .expect 109 | name : 'John' 110 | age : 26 111 | _id : 5 112 | .end done 113 | 114 | 115 | describe 'PUT /:model/:id', -> 116 | it 'should return JSON for the replaced record', (done) -> 117 | request 118 | .put '/users/2' 119 | .send 120 | name : 'Joseph' 121 | age : 37 122 | .expect 200 123 | .expect 124 | name : 'Joseph' 125 | age : 37 126 | _id : 2 127 | .end done 128 | 129 | describe 'PATCH', -> 130 | 131 | describe 'PATCH /:model/:id', -> 132 | it 'should respond with JSON for the updated record', (done) -> 133 | request 134 | .patch '/users/2' 135 | .send 136 | age : 28 137 | .expect 200 138 | .expect 139 | name : 'Joff' 140 | age : 28 141 | _id : 2 142 | .end done 143 | 144 | after (done) -> 145 | model.db.close(done) 146 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --require should 2 | --reporter spec 3 | --compilers coffee:coffee-script/register 4 | --require co-mocha 5 | -------------------------------------------------------------------------------- /test/server.js: -------------------------------------------------------------------------------- 1 | var koa = require('koa'); 2 | var router = require('koa-router'); 3 | var mongoose = require('mongoose'); 4 | var generateApi = require('../lib/index'); 5 | 6 | var app = koa(); 7 | app.use(router(app)); 8 | 9 | var mongoUrl = '127.0.0.1:27017'; 10 | mongoose.connect(mongoUrl); 11 | 12 | var schema = new mongoose.Schema({ 13 | name: String, 14 | age : Number, 15 | _id : Number 16 | }, {versionKey: false}); 17 | 18 | model = app.model = mongoose.model('user', schema); 19 | generateApi(app, model); 20 | 21 | module.exports = app; 22 | --------------------------------------------------------------------------------