Permalink
Browse files

initial commit

  • Loading branch information...
aheckmann committed Apr 16, 2011
0 parents commit f3969b4d5d4b469081646a19b6fd71f848072a0d
Showing with 770 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +6 −0 .gitmodules
  3. +3 −0 .npmignore
  4. +5 −0 History.md
  5. +22 −0 LICENSE
  6. +10 −0 Makefile
  7. +95 −0 Readme.md
  8. +138 −0 index.js
  9. +11 −0 package.json
  10. +1 −0 support/expresso
  11. +1 −0 support/jade
  12. +7 −0 test/fixtures/both.jade
  13. +1 −0 test/fixtures/layout.jade
  14. +4 −0 test/fixtures/promise.jade
  15. +4 −0 test/fixtures/query.jade
  16. +459 −0 test/index.js
@@ -0,0 +1,3 @@
+*.swp
+*.swn
+*.swo
@@ -0,0 +1,6 @@
+[submodule "support/expresso"]
+ path = support/expresso
+ url = https://github.com/visionmedia/expresso.git
+[submodule "support/jade"]
+ path = support/jade
+ url = https://github.com/visionmedia/jade.git
@@ -0,0 +1,3 @@
+.git*
+support/
+test/
@@ -0,0 +1,5 @@
+
+0.0.1 / 2011-04-15
+==================
+
+ * Initial release
22 LICENSE
@@ -0,0 +1,22 @@
+(The MIT License)
+
+Copyright (c) 2011 LearnBoost <dev@learnboost.com>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,10 @@
+
+test:
+ @NODE_ENV=test ./support/expresso/bin/expresso \
+ -I lib \
+ -I support \
+ -I support/connect/lib \
+ -I support/jade/lib \
+ test/index.js
+
+.PHONY: test
@@ -0,0 +1,95 @@
+
+# Express-goose
+
+ Express-goose adds Mongoose Query and Promise support to Express render, partial, and send methods.
+
+## Installation
+
+ $ npm install express-goose
+
+### Example
+
+ In your models:
+
+ User.methods.getLikes = function (callback) {
+ // returns a Query
+ return this.model('Likes').find({ _user: this._id }, callback);
+ };
+
+ News.statics.getLatest = function (callback) {
+ var promise = new Promise;
+ if (callback) promise.addBack(callback);
+ this.find({ datePublished: { $gt: new Date(Date.now() - 60000*60) } }, promise.resolve.bind(promise));
+ return promise;
+ };
+
+ In your routes:
+
+ app.get('/dashboard', function (req, res) {
+ var News = db.model('News');
+
+ // render support
+ res.render('dashboard', {
+ likes: req.user.getLikes()
+ , latestNews: News.getLatest()
+ });
+ });
+
+ app.get('/partial', function (req, res, next) {
+ res.partial('likes', {
+ likes: req.user.getLikes()
+ });
+ });
+
+ app.get('/send', function (req, res) {
+ // just pass a Query or Promise in and the result will be rendered as json.
+ // if an error occurs, the error will be next()ed along as expected.
+ var News = db.model('News');
+ res.send(News.getLatest());
+ });
+
+ app.get('/promises', function (req, res) {
+ var promise = new Promise(somethingAsync);
+ res.send(promise);
+ });
+
+### Features
+
+ res.render support
+ res.partial support
+ res.send support
+
+### Error handling
+
+ If a Query or Promise resolves to an error it will be forwarded on with `next(err)` as expected.
+
+## Authors
+
+ - [Aaron Heckmann (aheckmann)](http://github.com/aheckmann)
+
+## More Information
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2011 LearnBoost &lt;dev@learnboost.com&gt;
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
138 index.js
@@ -0,0 +1,138 @@
+/*!
+ * Express-goose
+ * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
+ * MIT Licenced
+ */
+
+/**
+ * Version.
+ */
+
+exports.version = '0.0.1';
+
+/**
+ * Module dependencies.
+ */
+
+const res = require('http').ServerResponse.prototype
+ , Promise = require('mongoose').Promise
+ , Query = require('mongoose').Query;
+
+/**
+ * Wrap the original rendering methods with support
+ * for Queries and Promises.
+ */
+
+res.render = wrap(res.render);
+res.partial = wrap(res.partial);
+
+function wrap (method) {
+ return function expressgoose (view, options, callback, parent, sub) {
+
+ if (!options || 'function' == typeof options) {
+ return method.call(this, view, options, callback, parent, sub);
+ }
+
+ var self = this;
+ return resolve(options, function (err, result) {
+ if (err) {
+ return 'function' == typeof callback
+ ? callback(err)
+ : self.req.next(err);
+ }
+
+ // must return here so partials always work
+ return method.call(self, view, result, callback, parent, sub);
+ });
+ }
+}
+
+/**
+ * Resolves any Queries and Promises within the passed options.
+ */
+
+function resolve (options, callback, nested) {
+ var keys = Object.keys(options)
+ , i = keys.length
+ , remaining = []
+ , pending
+ , item
+ , key;
+
+ while (i--) {
+ key = keys[i];
+ item = options[key];
+ if (item instanceof Query || item instanceof Promise) {
+ item.key = key;
+ remaining.push(item);
+ }
+ }
+
+ if (!remaining.length) {
+ return callback(null, options);
+ }
+
+ pending = remaining.length;
+
+ function error (err) {
+ if (error.ran) return;
+ callback(error.ran = err);
+ }
+
+ remaining.forEach(function (item) {
+ function handleResult (err, result) {
+ if (err) return error(err);
+ options[item.key] = result;
+ --pending || callback(null, options);
+ }
+
+ if (item instanceof Query) {
+ item.run(handleResult);
+ } else {
+ item.addBack(handleResult);
+ }
+ });
+
+ if (nested) return;
+
+ // locals support
+ var locals = options.locals;
+ if (locals) {
+ ++pending;
+ resolve(locals, function (err, resolved) {
+ if (err) return error(err);
+ options.locals = resolved;
+ --pending || callback(null, options);
+ }, true);
+ }
+
+}
+
+/**
+ * Add Promise/Query support to res.send.
+ */
+
+var oldsend = res.send;
+
+res.send = function (body, headers, status) {
+ var self = this;
+
+ function handleResult (err, result) {
+ if (err) return self.req.next(err);
+ oldsend.call(self, result, headers, status);
+ }
+
+ if (body instanceof Promise) {
+ return body.addBack(handleResult);
+ }
+
+ if (body instanceof Query) {
+ return body.run(handleResult);
+ }
+
+ if ('Object' == body.constructor.name) {
+ return resolve(body, handleResult);
+ }
+
+ oldsend.call(this, body, headers, status);
+};
@@ -0,0 +1,11 @@
+{
+ "name": "express-goose"
+, "description": "Adds Mongoose Promise/Query support to Express rendering."
+, "version": "0.0.1"
+, "author": "Aaron Heckmann <aaron@learnboost.com>"
+, "keywords": ["express", "mongoose", "mongo", "render", "promise", "query"]
+, "homepage": "https://github.com/learnboost/express-goose"
+, "dependencies": {}
+, "main": "index"
+, "engines": { "node": ">= 0.2.0" }
+}
Submodule expresso added at e8f447
Submodule jade added at bdeb8b
@@ -0,0 +1,7 @@
+ul
+ - each drumset in query
+ - if ('electronic' == drumset.type)
+ li= drumset.brand
+ - each drumset in promise
+ - if ('black' == drumset.color)
+ li= drumset.brand
@@ -0,0 +1 @@
+!=body
@@ -0,0 +1,4 @@
+ul
+ - each drumset in promise
+ - if ('GMS' === drumset.brand)
+ li= drumset.color
@@ -0,0 +1,4 @@
+ul
+ - each drumset in query
+ - if ('electronic' == drumset.type)
+ li= drumset.brand
Oops, something went wrong.

0 comments on commit f3969b4

Please sign in to comment.