Comparing version 0.1.0 to 0.1.1
@@ -1,1 +0,3 @@ | ||
module.exports = require('./lib/api.js'); | ||
var Jammin = module.exports = {} | ||
Jammin.API = require('./lib/api.js'); | ||
Jammin.Client = require('./lib/client.js'); |
@@ -0,1 +1,2 @@ | ||
var Path = require('path'); | ||
var Async = require('async'); | ||
@@ -27,2 +28,11 @@ var Mongoose = require('mongoose'); | ||
var setJamminFromParams = function(req, res, next) { | ||
for (key in req.params) { | ||
if (typeof req.jammin.query[key] === 'undefined') { | ||
req.jammin.query[key] = req.params[key]; | ||
} | ||
} | ||
next(); | ||
} | ||
var setJamminDefaults = function(req, res, next) { | ||
@@ -32,2 +42,3 @@ req.jammin = { | ||
document: JSON.parse(JSON.stringify(req.body)), | ||
arguments: JSON.parse(JSON.stringify(req.body)), | ||
method: req.method.toLowerCase(), | ||
@@ -41,2 +52,3 @@ } | ||
var API = function(options) { | ||
options = options || {}; | ||
if (typeof options === 'string') options = {databaseURL: options}; | ||
@@ -46,3 +58,5 @@ this.router = Express.Router(); | ||
this.router.use(setJamminDefaults); | ||
this.mongoose = Mongoose.createConnection(options.databaseURL); | ||
if (options.databaseURL) { | ||
this.mongoose = Mongoose.createConnection(options.databaseURL); | ||
} | ||
this.swaggerBuilder = new SwaggerBuilder(options.swagger); | ||
@@ -61,2 +75,33 @@ } | ||
var getModuleRoute = function(fn, options) { | ||
return function(req, res, next) { | ||
if (!options.async) { | ||
var result = fn.apply(fn, req.jammin.arguments); | ||
res.json(result); | ||
} else { | ||
req.jammin.arguments.push(function(err, result) { | ||
if (err) res.status(500).json({error: err}); | ||
else res.json(result || {success: true}); | ||
}); | ||
fn.apply(fn, req.jammin.arguments); | ||
} | ||
} | ||
} | ||
API.prototype.module = function() { | ||
var self = this; | ||
arguments = Array.prototype.slice.call(arguments); | ||
var basePath = arguments.shift(); | ||
var options = arguments.shift(); | ||
var module = options.module; | ||
for (var key in module) { | ||
var fn = module[key]; | ||
var fnName = key; | ||
if (typeof fn === 'function') { | ||
var fnPath = Path.join(basePath, key); | ||
self.router.post(fnPath, arguments, getModuleRoute(fn, options)); | ||
} | ||
} | ||
} | ||
API.prototype.swagger = function() { | ||
@@ -135,5 +180,2 @@ var self = this; | ||
var dbAction = function(req, res, next) { | ||
for (key in req.params) { | ||
req.jammin.query[key] = req.params[key]; | ||
} | ||
self.queryDB(req.jammin.method, many, req.jammin.query, req.jammin.document, function(err, thing) { | ||
@@ -177,7 +219,4 @@ if (err) res.status(500).json({error: err.toString()}); | ||
}, options.swagger); | ||
var firstAction = function(req, res, next) { | ||
next(); | ||
} | ||
middleware.push(dbAction); | ||
middleware.unshift(firstAction); | ||
middleware.unshift(setJamminFromParams); | ||
middleware.unshift(expressPath); | ||
@@ -184,0 +223,0 @@ this.api.router[method].apply(this.api.router, middleware); |
{ | ||
"name": "jammin", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "REST API Generator using Express and Mongoose", | ||
@@ -19,6 +19,7 @@ "main": "index.js", | ||
"async": "^0.9.0", | ||
"chai": "^2.2.0" | ||
"chai": "^2.2.0", | ||
"validator": "^3.39.0" | ||
}, | ||
"scripts": { | ||
"test": "mocha test/petstore.js" | ||
"test": "mocha test/petstore.js && mocha test/modules.js" | ||
}, | ||
@@ -25,0 +26,0 @@ "author": "Bobby Brennan <bobby@lucybot.com> (https://lucybot.com/)", |
@@ -7,3 +7,3 @@ ## Installation | ||
## About | ||
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. | ||
Jammin is the fastest way (that I know of) to build REST APIs in NodeJS. 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,8 +15,8 @@ ## Usage | ||
var Jammin = require('jammin'); | ||
var API = new Jammin('mongodb://<username>:<password>@<mongodb_host>'); | ||
var API = new Jammin.API('mongodb://<username>:<password>@<mongodb_host>'); | ||
var PetSchema = new Jammin.Schema({ | ||
var PetSchema = { | ||
name: String, | ||
age: Number | ||
}); | ||
}; | ||
@@ -80,5 +80,9 @@ API.define('Pet', PetSchema); | ||
You can use middleware to intercept database calls, alter the request, perform authentication, etc. | ||
Change ```req.jammin.query``` to alter how Jammin selects items from the database. | ||
Change ```req.jammin.document``` to alter the document Jammin will insert into the database. | ||
Change ```req.jammin.method``` to alter how Jammin interacts with the database. | ||
The example below alters ```req.query``` to construct a complex Mongo query from user inputs. | ||
@@ -100,3 +104,3 @@ ```js | ||
} else if (req.method === 'GET') { | ||
req.jammin.query = {deleted: {"$ne": true}} | ||
req.jammin.query.deleted = {"$ne": true}; | ||
} else if (req.method === 'POST' || req.method === 'PUT') { | ||
@@ -140,1 +144,4 @@ req.jammin.document.deleted = false; | ||
``` | ||
## Extended Usage | ||
See the example [Petstore Server](test/petstore-server.js) for other examples. |
@@ -23,3 +23,3 @@ var FS = require('fs'); | ||
var Jammin = require('../index.js') | ||
var API = new Jammin({ | ||
var API = new Jammin.API({ | ||
databaseURL: DatabaseURL, | ||
@@ -26,0 +26,0 @@ swagger: { |
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
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
48197
15
1460
143
3
5