Created
May 16, 2012 02:46
-
-
Save jsocol/2706896 to your computer and use it in GitHub Desktop.
Prongo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var mongodb = require('mongodb'); | |
| var express = require('express'); | |
| var app = express.createServer(); | |
| var db = new mongodb.Db('mysite', | |
| new mongodb.Server('localhost', 27027, {}), | |
| {native_parser: true}); | |
| app.get('/', function(req, res) { | |
| // Open the connection. | |
| db.open(function(err, db) { | |
| // Get one collection. | |
| db.collection('users', function(err, usersColl) { | |
| // Search the first collection. | |
| usersColl.find({}, {'limit': 3}, function(err, usersCursor) { | |
| // Convert the result into an array. | |
| usersCursor.toArray(function(err, users) { | |
| // Get the second collection. | |
| db.collection('articles', function(err, artColl) { | |
| // Search the second collection. | |
| artColl.find({}, {'limit': 3}, function(err, artCursor) { | |
| // Convert the result into an array. | |
| artCursor.toArray(function(err, articles) { | |
| // Now we have two arrays, users and articles, in scope. | |
| // Render the home page. | |
| res.render('home.ejs', {'users': users, 'articles': articles}); | |
| }); | |
| }); | |
| }); | |
| // OH GOD WHY | |
| }); | |
| }); | |
| }); | |
| // CLOSE EVERYTHING | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var mongodb = require('mongodb'); | |
| var express = require('express'); | |
| var prongo = require('./prongo.js'); | |
| var Deferred = require('Deferred'); | |
| var app = express.createServer(); | |
| var db = new mongodb.Db('mysite', | |
| new mongodb.Server('localhost', 27027, {}), | |
| {native_parser: true}); | |
| function home(req, res) { | |
| var uDef = prongo.getArray(db, 'users', {}, {'limit': 3}); | |
| var aDef = prongo.getArray(db, 'articles', {}, {'limit': 4}); | |
| var lookup = Deferred.when(uDef, aDef); | |
| lookup.done(function(users, articles)) { | |
| res.render('home.ejs', {'users': users, 'articles': articles}); | |
| }).fail(function(err) { | |
| res.render('error.ejs', {'error': err}) | |
| }); | |
| } | |
| app.get('/', home); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def home(request): | |
| foos = Foo.objects.all().orderby('-created')[0:5] | |
| bars = Bar.objects.all().orderby('-created')[0:5] | |
| return render(request, 'home.html', {'foos': foos, 'bars': bars}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <h1><?= mysql_query($my, "SELECT title FROM posts WHERE..."); ?></h1> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| }); | |
| }); | |
| }); | |
| // OH GOD WHY | |
| }); | |
| }); | |
| }); | |
| // CLOSE EVERYTHING | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var Deferred = require('Deferred'); | |
| exports.getArray = function getArray(db, coll, search, options) { | |
| var def = Deferred(); | |
| db.open(function(err, db) { | |
| if (err) def.reject(err); | |
| db.collection(coll, function(err, collection) { | |
| if (err) def.reject(err); | |
| collection.find(search, options, function(err, cursor) { | |
| if (err) def.reject(err); | |
| cursor.toArray(function(err, arr) { | |
| if (err) def.reject(err); | |
| def.resolve(arr); | |
| }); | |
| }); | |
| }); | |
| }); | |
| return def.promise(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| app.get('/', function(req, res) { | |
| // Open the connection. | |
| db.open(function(err, db) { | |
| // Get one collection. | |
| db.collection('users', function(err, usersColl) { | |
| // Search the first collection. | |
| usersColl.find({}, {'limit': 3}, function(err, usersCursor) { | |
| // Convert the result into an array. | |
| usersCursor.toArray(function(err, users) { | |
| // Get the second collection. | |
| db.collection('articles', function(err, artColl) { | |
| // Search the second collection. | |
| artColl.find({}, {'limit': 3}, function(err, artCursor) { | |
| // Convert the result into an array. | |
| artCursor.toArray(function(err, articles) { | |
| // Now we have two arrays, users and articles, in scope. | |
| // Render the home page. | |
| res.render('home.ejs', {'users': users, 'articles': articles}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment