├── .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 [](http://badge.fury.io/js/koa-mongo-rest) [](https://gemnasium.com/t3chnoboy/koa-mongo-rest) [](https://travis-ci.org/t3chnoboy/koa-mongo-rest)
2 |
3 | Easy REST api for [koa](http://koajs.com) server
4 |
5 | [](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 |
Line | Hits | Source |
---|---|---|
1 | 1 | var createModel, generateActions, generateRoutes; |
2 | ||
3 | 1 | generateRoutes = require('./routes'); |
4 | ||
5 | 1 | generateActions = require('./actions'); |
6 | ||
7 | 1 | createModel = require('./model'); |
8 | ||
9 | 1 | module.exports = function(schema, mongoUrl) { |
10 | 1 | var actions, model; |
11 | 1 | model = createModel(schema, mongoUrl); |
12 | 1 | actions = generateActions(model); |
13 | 1 | model.generateApi = function(app) { |
14 | 1 | return generateRoutes(app, schema, actions); |
15 | }; | |
16 | 1 | return model; |
17 | }; |
Line | Hits | Source |
---|---|---|
1 | 1 | var createModel, mongoose; |
2 | ||
3 | 1 | mongoose = require('mongoose'); |
4 | ||
5 | 1 | module.exports = createModel = function(schema, mongoUrl) { |
6 | 1 | var DocumentSchema; |
7 | 1 | mongoose.connect(mongoUrl); |
8 | 1 | DocumentSchema = new mongoose.Schema(schema.schema, { |
9 | collection: schema.collectionName, | |
10 | versionKey: schema.versionKey | |
11 | }); | |
12 | 1 | return mongoose.model(schema.collectionName, DocumentSchema); |
13 | }; |
Line | Hits | Source |
---|---|---|
1 | 1 | var generateRoutes; |
2 | ||
3 | 1 | module.exports = generateRoutes = function(app, schema, actions) { |
4 | 1 | app.get("/" + schema.collectionName + "/find", actions.findAll); |
5 | 1 | app.get("/" + schema.collectionName + "/find/:id", actions.findById); |
6 | 1 | app.get("/" + schema.collectionName + "/create", actions.createWithQuery); |
7 | 1 | app.get("/" + schema.collectionName + "/destroy/:id", actions.deleteById); |
8 | 1 | app.get("/" + schema.collectionName + "/update/:id", actions.updateByIdWithQuery); |
9 | 1 | app.get("/" + schema.collectionName + "/replace/:id", actions.replaceByIdWithQuery); |
10 | 1 | app.get("/" + schema.collectionName, actions.findAll); |
11 | 1 | app.get("/" + schema.collectionName + "/:id", actions.findById); |
12 | 1 | app.post("/" + schema.collectionName, actions.create); |
13 | 1 | app.post("/" + schema.collectionName + "/:id", actions.updateById); |
14 | 1 | app["delete"]("/" + schema.collectionName + "/:id", actions.deleteById); |
15 | 1 | app.put("/" + schema.collectionName + "/", actions.create); |
16 | 1 | app.put("/" + schema.collectionName + "/:id", actions.replaceById); |
17 | 1 | app.patch("/" + schema.collectionName + "/:id", actions.updateById); |
18 | }; |
Rest api generation for koa server.
40 | -------------------------------------------------------------------------------- /docs/tail.html: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /docs/test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |Rest api generation for koa server.
40 |return request.get('/user').expect(200).expect('Content-Type', /json/).expect(users).end(done);
return request.get('/user/2').expect(200).expect('Content-Type', /json/).expect({
61 | name: 'Joff',
62 | age: 27,
63 | _id: 2
64 | }).end(done);
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);
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);
return request.del('/user/2').expect(200).expect({
110 | name: 'Joff',
111 | age: 27,
112 | _id: 2
113 | }).end(done);
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);
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);
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);