Skip to content

Instantly share code, notes, and snippets.

@pphetra
Created July 5, 2013 09:30
Show Gist options
  • Select an option

  • Save pphetra/5933287 to your computer and use it in GitHub Desktop.

Select an option

Save pphetra/5933287 to your computer and use it in GitHub Desktop.
var redis = require('redis');
var client = redis.createClient();
var pg = require('pg');
var async = require('async');
var _ = require('underscore');
var when = require('when');
connect("tcp://erp:erp@localhost/poc")
.then(getFeedFromRedis)
.then(getReport)
.then(getComment)
.then(function(result) {
console.log(result);
client.quit();
conn.end();
});
var conn;
function connect(uri) {
conn = new pg.Client(uri);
var deferred = when.defer();
conn.connect(function(err) {
deferred.resolve();
});
return deferred.promise;
}
function getFeedFromRedis() {
var deferred = when.defer();
client.zrange("c1", 0, -1, function(err, results) {
deferred.resolve(results);
});
return deferred.promise;
}
function getReport(ids) {
return when.map(ids, function(id) {
var deferred = when.defer();
conn.query('select title from report where id = $1', [id], function(err, result) {
deferred.resolve({
id: id,
title: result.rows[0].title
});
});
return deferred.promise;
});
}
function getComment(reports) {
return when.map(reports, function(report) {
var deferred = when.defer();
conn.query('select id, title from comment where report_id = $1', [report.id], function(err, result) {
report.comments = result.rows;
deferred.resolve(report);
})
return deferred.promise;
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment