Comparing version 0.0.0 to 0.0.1
36
index.js
@@ -6,2 +6,10 @@ var Mongoose = require('mongoose'); | ||
var SELECT = '-_id -__v' | ||
var FIELDS = {}; | ||
SELECT.split(' ').forEach(function(field) { | ||
if (field.indexOf('-') === 0) { | ||
FIELDS[field.substring(1)] = false; | ||
} else { | ||
FIELDS[field] = true; | ||
} | ||
}) | ||
@@ -46,3 +54,4 @@ var API = function(options) { | ||
if (method === 'get') { | ||
self.db.findOne(query).select(SELECT).exec(function(err, thing) { | ||
var find = many ? self.db.find : self.db.findOne; | ||
find.apply(self.db, [query, SELECT]).exec(function(err, thing) { | ||
if (err) res.status(500).json({error: err.toString()}) | ||
@@ -53,14 +62,23 @@ else if (!thing) res.status(404).json({error: 'Not Found'}) | ||
} else if (method === 'post') { | ||
var newThing = new self.db(req.body); | ||
newThing.save(function(err) { | ||
var docs = many ? req.body : [req.body]; | ||
self.db.collection.insert(docs, function(err, docs) { | ||
if (err) res.status(500).json({error: err.toString()}) | ||
else res.json({success: true}); | ||
else res.json({success: true}) | ||
}) | ||
} else if (method === 'put') { | ||
self.db.findOneAndUpdate.select(SELECT).exec(query, req.body, function(err, thing) { | ||
if (err) res.status(500).json({error: err.toString()}); | ||
else res.json(thing); | ||
}) | ||
if (many) { | ||
self.db.update(query, req.body, {multi: true}).select(SELECT).exec(function(err) { | ||
if (err) res.status(500).json({error: err.toString()}); | ||
else res.json({success: true}); | ||
}) | ||
} else { | ||
self.db.findOneAndUpdate(query, req.body, {new: true}).select(SELECT).exec(function(err, thing) { | ||
if (err) res.status(500).json({error: err.toString()}); | ||
else if (!thing) res.status(404).json({error: 'Not Found'}); | ||
else res.json(thing); | ||
}); | ||
} | ||
} else if (method === 'delete') { | ||
self.db.findOneAndRemove(query, function(err, thing) { | ||
var remove = many ? self.db.remove : self.db.findOneAndRemove; | ||
remove.apply(self.db, [query]).exec(function(err, thing) { | ||
if (err) res.status(500).json({error: err.toString()}); | ||
@@ -67,0 +85,0 @@ else if (!thing) res.status(404).json({error: 'Not Found'}); |
{ | ||
"name": "jammin", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "REST API Generator using Express and Mongoose", | ||
@@ -11,5 +11,7 @@ "main": "index.js", | ||
}, | ||
"devDependencies": {}, | ||
"devDependencies": { | ||
"chai": "^2.2.0" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "mocha test/petstore.js" | ||
}, | ||
@@ -16,0 +18,0 @@ "author": "Bobby Brennan <bobby@lucybot.com> (https://lucybot.com/)", |
@@ -1,3 +0,1 @@ | ||
# We be Jammin' | ||
## Installation | ||
@@ -8,4 +6,6 @@ ```npm install jammin``` | ||
*Unimplemented features are tagged with ```TODO```* | ||
## About | ||
Jammin' is the fastest way to build a JSON REST API with Node, Express, and MongoDB. It consists of a light-weight wrapper around Mongoose and an Express router to expose HTTP methods. | ||
Jammin is the fastest way (that I know of) to build a JSON REST API with Node, Express, and MongoDB. It consists of a light-weight wrapper around [Mongoose](http://mongoosejs.com/) for database operations and an [Express](http://expressjs.com/) router to expose HTTP methods. It is fully extensible via middleware to support things like authentication, resource ownership, and complex queries. | ||
@@ -15,2 +15,3 @@ ## Usage | ||
```js | ||
var App = require('express')(); | ||
var Jammin = require('jammin'); | ||
@@ -20,37 +21,65 @@ var API = new Jammin('mongodb://<username>:<password>@<mongodb_host>'); | ||
var PetSchema = new Jammin.Schema({ | ||
name: String | ||
name: String, | ||
age: Number | ||
}); | ||
API.define('pet', PetSchema) | ||
API.define('pet', PetSchema); | ||
API.pet.get('/pets/{name}'); | ||
API.pet.post('/pets'); | ||
App.use('/api', API); | ||
App.listen(3000); | ||
``` | ||
```bash | ||
> curl -X POST 127.0.0.1:3000/pets -d '{"name": "Lucy", "age": 2}' | ||
{"success": true} | ||
> curl 127.0.0.1:3000/pets/Lucy | ||
{"name": "Lucy", "age": 2} | ||
``` | ||
### GET | ||
Jammin will use ```req.params``` and ```req.query``` to find an item the database. | ||
Jammin will use ```req.params``` and ```req.query``` to **find an item** the database. | ||
```js | ||
API.pet.get('/pet/{name}'); | ||
``` | ||
Use ```getMany``` to return an array of matching documents. | ||
```js | ||
API.pet.getMany('/pet') | ||
``` | ||
### POST | ||
Jammin will use ```req.body``` to create a new item in the database. | ||
Jammin will use ```req.body``` to **create a new item** in the database. | ||
```js | ||
API.pet.post('/pet'); | ||
API.pet.post('/pets'); | ||
``` | ||
Use ```postMany``` to accept an array of items to be created. | ||
```js | ||
API.pet.postMany('/pets'); | ||
``` | ||
### PUT | ||
Jammin will use ```req.params``` and ```req.query``` to find an item in the database, and use ```req.body``` to update that item. | ||
Jammin will use ```req.params``` and ```req.query``` to find an item in the database, and use ```req.body``` to **update that item**. | ||
```js | ||
API.pet.put('/pet/{name}'); | ||
API.pet.put('/pets/{name}'); | ||
``` | ||
Use ```putMany``` to update every matching item in the database. | ||
```js | ||
API.pet.putMany('/pets'); | ||
``` | ||
### DELETE | ||
Jammin will use ```req.params``` and ```req.query``` to remove an item from the database. | ||
Jammin will use ```req.params``` and ```req.query``` to **remove an item** from the database. | ||
```js | ||
API.pet.delete('/pet/{name}'); | ||
API.pet.delete('/pets/{name}'); | ||
``` | ||
Use deleteMany to delete every matching item in the database. | ||
```js | ||
API.pet.deleteMany('/pets'); | ||
``` | ||
### Middleware | ||
You can use middleware to intercept Jammin requests, alter the request, perform authentication, etc. | ||
You can use middleware to intercept database calls, alter the request, perform authentication, etc. | ||
The example below alters ```req.query``` to construct a complex Mongo query from user inputs. | ||
```js | ||
// Searches pets by name using parameter 'q', e.g. GET api.pets.com/search/pets?q=fido | ||
API.pet.getMany('/search/pets', function(req, res, next) { | ||
@@ -64,3 +93,3 @@ req.query = { | ||
### Swagger | ||
### Swagger ```TODO``` | ||
Serve a [Swagger specification](http://swagger.io) for your API at the specified path. You can use this to document your API via [Swagger UI](https://github.com/swagger-api/swagger-ui) or a [LucyBot portal](https://lucybot.com) | ||
@@ -70,2 +99,13 @@ ```js | ||
``` | ||
Jammin will fill out the technical details of your spec, but you can provide additional information: | ||
``` | ||
var API = new Jammin({ | ||
databaseURL: DatabaseURL, | ||
swagger: { | ||
info: {title: 'Pet Store'}, | ||
host: 'api.example.com', | ||
basePath: '/api' | ||
} | ||
}); | ||
``` | ||
@@ -118,3 +158,5 @@ ## Extended Usage | ||
API.pet.getMany('/search/pets', { | ||
parameters: [{name: 'q', in: 'query', type: 'string'}] | ||
swagger: { | ||
parameters: [{name: 'q', in: 'query', type: 'string'}] | ||
} | ||
}, function(req, res, next) { | ||
@@ -121,0 +163,0 @@ var userQuery = Util._extend({}, req.query); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
19369
7
411
2
202
1
1
1