xml2js.parseString(removeBackslash(xmlString[1]), function (error, result) {
        if (error) {
          console.error(error);
          return;
        }

        if (!error && result) {
          pg.connect(config.connections.postgresql, function (error, client, pgDone) {
            if (error) {
              pgDone();
              console.error(error);
              return;
            }

            // Loop throught each dataSet (year).
            var thisYear = moment().year();
            var index = 0;
            when
              .map(result.chart.dataSet, function (data) {
                var currentYear = FIRST_YEAR + (index++);
                if (currentYear > thisYear) {
                  return when.resolve();
                }

                return when.map(data.set, function (week) {
                  return when.promise(function(resolve, reject) {
                    var doc = {
                      source: 'boe',
                      date: moment(currentYear.toString()).weeks(week.$.x).day('Sunday').toDate().clearTime(),
                      year: currentYear,
                      week: parseFloat(week.$.x),
                      value: parseFloat(week.$.y)
                    };
                    var query = {
                      source: doc.source,
                      year: doc.year,
                      week: doc.week
                    };

                    console.info('Saving year:', doc.year, 'week:', doc.week);

                    client.query(' \
                      SELECT id FROM ililog WHERE source = $1 AND year = $2 AND week = $3 \
                    ', [ doc.source, doc.year, doc.week ], function (error, result) {
                      if (error) {
                        return reject(error);
                      }

                      var updateQuery = "UPDATE ililog SET date = $1, value = $2, \"updatedAt\" = $3 WHERE source = 'boe' AND year = $4 AND week = $5";
                      var updateValue = [doc.date, doc.value, new Date(), doc.year, doc.week];

                      var insertQuery = "INSERT INTO ililog (source, date, year, week, value, \"createdAt\", \"updatedAt\") VALUES ($1, $2, $3, $4, $5, $6, $7)";
                      var insertValue = [doc.source, doc.date, doc.year, doc.week, doc.value, new Date(), new Date()];

                      var query, value;
                      if (result.rows.length > 0) {
                        query = updateQuery;
                        value = updateValue;
                      }
                      else {
                        query = insertQuery;
                        value = insertValue;
                      }

                      // Update
                      client.query(query, value, function (error, result) {
                        if (error) {
                          console.error(error);
                          return reject(error);
                        }

                        resolve();
                      });
                    });
                  });
                });
              })
              .then(function () {
                console.info('Import done.');
              })
              .catch(function (error) {
                console.error('Something error:', error);
              })
              .finally(function () {
                pgDone();
              });
          });
        }
      });
var pg = require('pg');

pg.defaults.ssl = true;
pg.connect('postgres://*****:*****@ec2-23-23-95-27.compute-1.amazonaws.com:5432/d4m7o7f3n8jirt', function(err, client) {
  if (err) throw err;
  console.log('Connected to postgres!');

  client
    .query('DELETE FROM PERSON;')

  client
    .query('DELETE FROM SIGNATURE;')

});
Beispiel #3
0
var config = require('./config.js');

// Postgres connection
var pg = require('pg'),
    conn = "pg://"+config.username+":"+config.password+"@db.schemaverse.com:5432/schemaverse";

var UserModel = require('./models/user');
var CommandProcessor = require('./command_processor');
       
pg.connect(conn, function(err, client) {
    if (!err){
        var userModel = new UserModel.constructor(client);
        CommandProcessor(userModel, client);
    } else {
        console.log(err);
    }
});
Beispiel #4
0
var split = function(region, callback) {
  console.log("[*] splitting region %d", region.id);

  return pg.connect(DATABASE_URL, function(err, client, done) {
    if (err) {
      return callback(err);
    }

    var query = client.query.bind(client),
        insert = [
          "INSERT INTO foursquare_regions (geom, width, last_fetched)",
          "  WITH region AS (",
          "    SELECT",
          "      geom",
          "    FROM foursquare_regions",
          "    WHERE id = $1",
          "  ),",
          "  horiz AS (",
          "    SELECT (ST_Dump(ST_Split(geom, ST_SetSRID(ST_MakeLine(ST_MakePoint(ST_XMin(geom), (ST_YMin(geom) + ST_YMax(geom)) / 2), ST_MakePoint(ST_XMax(geom), (ST_YMin(geom) + ST_YMax(geom)) / 2)), ST_SRID(geom))))).geom",
          "    FROM region",
          "  ),",
          "  vert AS (",
          "    SELECT (ST_Dump(ST_Split(geom, ST_SetSRID(ST_MakeLine(ST_MakePoint((ST_XMin(geom) + ST_XMax(geom)) / 2, ST_YMin(geom)), ST_MakePoint((ST_XMin(geom) + ST_XMax(geom)) / 2, ST_YMax(geom))), ST_SRID(geom))))).geom",
          "    FROM horiz",
          "  )",
          "  SELECT",
          "    DISTINCT vert.geom,",
          "    $2::double precision AS width,",
          "    $3::timestamp with time zone AS last_fetched",
          "  FROM vert",
          "  INNER JOIN superunits ON ST_Intersects(superunits.geom, ST_Transform(vert.geom, ST_SRID(superunits.geom)))",
          // prevent duplicate key errors
          "  WHERE (SELECT COUNT(id) FROM foursquare_regions r WHERE ST_Equals(r.geom, vert.geom)) = 0"
        ].join("\n"),
        insertParams = [region.id, region.width / 2, region.last_fetched],
        update = "UPDATE foursquare_regions SET split = true WHERE id = $1",
        updateParams = [region.id];

    return async.series([
      async.apply(query, "BEGIN"),
      async.apply(query, insert, insertParams),
      async.apply(query, update, updateParams),
      async.apply(query, "COMMIT")
    ], function(err) {
      if (err) {
        console.warn("split error on region %d:", region.id, err);
        return query("ROLLBACK", function(e) {
          done();

          if (e) {
            console.warn("Rollback failed:", e);
          }

          return callback(err);
        });
      }

      done();

      return callback.apply(null, arguments);
    });
  });
};
Beispiel #5
0
 function(err, clean){
     if (err) throw err;
     pg.connect(that.conString, this);
 },
Beispiel #6
0
exports.request = function(req, res) {

    // Create URL
	var url = "postgres://" + db_settings.user + ":" + db_settings.password + "@" + db_settings.host + ":" + db_settings.port + "/" + db_settings.database_name;

	// Connect to Database
	pg.connect(url, function(err, client, done) {
		if(err) {
			res.status(errors.database.error_1.code).send(errors.database.error_1);
			return console.error(errors.database.error_1.message, err);
		} else {

            // Database Query
			client.query("SELECT * FROM Sensors WHERE sensor_id=$1;", [
				req.params.sensor_id
			], function(err, result) {
				done();

				if(err) {
					res.status(errors.database.error_2.code).send(_.extend(errors.database.error_2, err));
					return console.error(errors.database.error_2.message, err);
				} else {

					// Check if Sensor exists
					if(result.rows.length === 0) {
						res.status(errors.query.error_2.code).send(errors.query.error_2);
						return console.error(errors.query.error_2.message);
					} else {

                        // Get Sensor from results
						var sensor = result.rows[0];

                        // Check if User was authenticated
                        if(!req.headers.authorization || req.headers.authorization === ""){
                            res.status(errors.authentication.error_3.code).send(errors.authentication.error_3);
                            return console.error(errors.authentication.error_3.message);
                        } else {

                            // Decode Token
                            jwt.verify(req.headers.authorization, secret.key, function(err, decoded) {
                                if (err) {
                                    res.status(errors.authentication.error_2.code).send(errors.authentication.error_2);
                                    return console.error(errors.authentication.error_2.message);
                                } else {

                                    // Get username from authenticated user
                                    var username = decoded.username;

                                    // Check if User was creator of private Sensor or is admin
                                    if(username === sensor.creator || username === db.admin){

                                        // Prepare Query
                                        var query = "DELETE FROM Timeseries WHERE sensor_id=$1;";

                                        // Database query
                                        client.query(query, [
                                            req.params.sensor_id
                                        ], function(err, result) {
                                            done();

                                            if (err) {
                                                res.status(errors.database.error_2.code).send(_.extend(errors.database.error_2, err));
                                                return console.error(errors.database.error_2.message, err);
                                            } else {

                                                // Send Result
                                                res.status(204).send();
                                            }
                                        });

                                    } else {
                                        res.status(errors.authentication.error_2.code).send(errors.authentication.error_2);
                                        return console.error(errors.authentication.error_2.message);
                                    }
                                }
                            });
                        }
                    }
                }
            });
        }
    });
};
exports.testTradeStream = function(test) {
    test.expect(15);
    pg.connect(dbUrl, function(err, client, done) {
        test.ifError(err);
        //console.log("Error object is as follows: %o", err);

        var testTable = "test_" + ("0000000000" + Math.floor(10000000000*Math.random())).slice(-10); 

        //console.log("Client object is as follows: %o", client);

        async.series([
            async.apply(client.query.bind(client), "CREATE TABLE " + testTable + " " +
                "(exchange VARCHAR(50), currency CHAR(3), " +
                "time TIMESTAMP WITH TIME ZONE, price DECIMAL, volume DECIMAL, cnt INTEGER, " +
                "PRIMARY KEY(exchange, currency, time, price, volume))"),
            async.apply(client.query.bind(client), "INSERT INTO " + testTable + " " +
                "VALUES ('localbtc', 'PLN', '2013-07-16T00:34:50+00' ,'349.000000000000', '0.110000000000', 5)")
        ], function(createErr, result) {
            test.ifError(createErr);

            var loadOptions = {
                dbUrl : dbUrl,
                symbol : 'localbtcPLN',
                tableName : testTable,
                fileFields : ['time', 'price', 'volume'],
                symbolFields : ['exchange', 'currency'],
                countField : ['cnt']
            };

            pgLoader.createTradeStream(loadOptions, function(err, pgcfStream) {
                    //console.log("Error object is as follows: %o", createErr);
                    test.ifError(err);
                    fs.createReadStream(__dirname + "/read-files/localbtcPLN.csv").pipe(pgcfStream);            
                }, function(err) {
                    test.ifError(err);

                    async.series([
                        async.apply(client.query.bind(client),"SELECT exchange, currency, time, price, volume, cnt " + 
                            "FROM " + testTable + " ORDER BY 3 ASC LIMIT 1"),
                        async.apply(client.query.bind(client),"SELECT exchange, currency, time, price, volume, cnt " + 
                            "FROM " + testTable + " ORDER BY 3 DESC LIMIT 1"),
                        async.apply(client.query.bind(client),"SELECT COUNT(*) as \"count\" FROM " + testTable),
                        async.apply(client.query.bind(client),"SELECT SUM(cnt) as \"count_lines\" FROM " + testTable),
                        async.apply(client.query.bind(client),"DROP TABLE " + testTable)
                    ], function(err, result) {
                        test.ifError(err);
                        done();
                        test.equal(result[0].rows[0].time.toString(), (new Date("2013-06-24T16:56:22+00:00")).toString(), "Date on first line doesn't match expectations");
                        test.equal(result[0].rows[0].price, "371.170000000000", "Price on first line doesn't match expectations");
                        test.equal(result[0].rows[0].volume, "0.269400000000", "Volume on first line doesn't match expectations");
                        test.equal(result[0].rows[0].cnt, "3", "Count on first line doesn't match expectations");
                        test.equal(result[1].rows[0].time.toString(), (new Date("2014-03-18T08:42:20+00:00")).toString(), "Date on last line doesn't match expectations");
                        test.equal(result[1].rows[0].price, "1828.360000000000", "Price on last line doesn't match expectations");
                        test.equal(result[1].rows[0].volume, "1.367300000000", "Volume on last line doesn't match expectations");
                        test.equal(result[1].rows[0].cnt, "1", "Count on last line doesn't match expectations");
                        test.equal(result[2].rows[0].count, 478, "Count of total rows in tables doesn't match expectations.");
                        test.equal(result[3].rows[0].count_lines, 494, "Count of total lines in file doesn't match expectations.");
                        test.done();
                        disconnect();
                    });
                }
            );
        });
    });
};
Beispiel #8
0
        
var conString = process.env.DATABASE_URL;


pg.connect(conString, function(err, client, done) {
    bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
        bcrypt.hash("erikle13101989", salt, function(err, hash) {
            client.query('SELECT * FROM Users WHERE phone = 725637243;',
                    function(err, result) {
                        done();
                        if (err)
                            return console.error(err);
                        else {
                            if (result.rows.length < 1) {
                                client.query("INSERT INTO Users (phone, name, surname, password, address, psc, role, city, state) \n\
                                     VALUES (725637243, 'Dung', 'Le Xuan','" + hash + "','Náchodská 640/101',40801,'admin','Praha 9',1);",
                                        function(err, resultinsert) {
                                            if (err)
                                                return console.error(err);
                                            else
                                                client.end();
                                        });
                            }
                        }
                    });
        });
    });
});


dbapi.createDB();
Beispiel #9
0
exports.request = function(req, res){

	// Create URL
	var url = "postgres://" + db_settings.user + ":" + db_settings.password + "@" + db_settings.host + ":" + db_settings.port + "/" + db_settings.database_name;

	// Connect to Database
	pg.connect(url, function(err, client, done) {
		if(err) {
			res.status(errors.database.error_1.code).send(errors.database.error_1);
			return console.error(errors.database.error_1.message, err);
		} else {

			// Check if sensor_id exists
			if(req.params.sensor_id !== undefined && req.params.sensor_id !== 'undefined') {

				// Check if Sensor exists
				var query = "SELECT * FROM Sensors WHERE sensor_id=$1;";

				// Database Query
				client.query(query, [
					req.params.sensor_id
				], function(err, result) {
					done();

					if(err) {
						res.status(errors.database.error_2.code).send(_.extend(errors.database.error_2, err));
						return console.error(errors.database.error_2.message, err);
					} else {

						// Check if Sensor exists
						if(result.rows.length === 0) {
							res.status(errors.query.error_2.code).send(errors.query.error_2);
							return console.error(errors.query.error_2.message);
						} else {
							var sensor = result.rows[0];

							// Check if Sensor is public
							if(!sensor.private) {

								var query = "SELECT " +
										"sensors.sensor_id, " +
										"sensors.device_id, " +
										"sensors.description ," +
										"sensors.private, " +
										"sensors.creator, " +
										"sensors.online_status, " +
										"sensors.water_body_id, " +
										"water_bodies.water_body_type, " +
										"water_bodies.name AS water_body_name, " +
										"sensors.sensor_height, " +
										"'CENTIMETER' AS sensor_height_unit, " +
										"sensors.crossing_height, " +
										"'CENTIMETER' AS crossing_height_unit, " +
										"sensors.default_frequency, " +
										"'MILLISECONDS' AS default_frequency_unit, " +
										"sensors.danger_frequency, " +
										"'MILLISECONDS' AS danger_frequency_unit, " +
										"sensors.increased_frequency, " +
										"sensors.triggered_threshold, " +
										"sensors.triggered_weather, " +
										"sensors.threshold_value, " +
										"'CENTIMETER' AS threshold_value_unit, " +
										"ST_X(sensors.coordinates::geometry) AS lng, " +
										"ST_Y(sensors.coordinates::geometry) AS lat, " +
										"sensors.crossing_type, " +
										"sensors.seasonal, " +
										"sensors.wet_season_begin, " +
										"sensors.wet_season_end, " +
										"sensors.dry_season_begin, " +
										"sensors.dry_season_end, " +
										"sensors.created, " +
										"sensors.updated " +
									"FROM Sensors sensors JOIN Water_Bodies water_bodies ON sensors.water_body_id=water_bodies.water_body_id " +
									"WHERE sensors.sensor_id=$1;";

								// Database Query
								client.query(query, [
									req.params.sensor_id
								], function(err, result) {
									done();

									if(err) {
										res.status(errors.database.error_2.code).send(_.extend(errors.database.error_2, err));
										return console.error(errors.database.error_2.message, err);
									} else {

										// Send Result
										res.status(200).send(result.rows[0]);
									}
								});

							} else {

								// Check if User was authenticated
								if(!req.headers.authorization || req.headers.authorization === ""){
									res.status(errors.authentication.error_3.code).send(errors.authentication.error_3);
									return console.error(errors.authentication.error_3.message);
								} else {

									// Decode Token
									jwt.verify(req.headers.authorization, secret.key, function(err, decoded) {
										if (err) {
											res.status(errors.authentication.error_2.code).send(errors.authentication.error_2);
											return console.error(errors.authentication.error_2.message);
								        }

										// Get username from authenticated user
										var username = decoded.username;

										// Check if authenticated user is the right user
										if(username === sensor.creator) {
											query = "SELECT " +
													"sensors.sensor_id, " +
													"sensors.device_id, " +
													"sensors.description ," +
													"sensors.private, " +
													"sensors.creator, " +
													"sensors.online_status, " +
													"sensors.water_body_id, " +
													"water_bodies.water_body_type, " +
													"water_bodies.name AS water_body_name, " +
													"sensors.sensor_height, " +
													"'CENTIMETER' AS sensor_height_unit, " +
													"sensors.crossing_height, " +
													"'CENTIMETER' AS crossing_height_unit, " +
													"sensors.default_frequency, " +
													"'MILLISECONDS' AS default_frequency_unit, " +
													"sensors.danger_frequency, " +
													"'MILLISECONDS' AS danger_frequency_unit, " +
													"sensors.increased_frequency, " +
													"sensors.triggered_threshold, " +
													"sensors.triggered_weather, " +
													"sensors.threshold_value, " +
													"'CENTIMETER' AS threshold_value_unit, " +
													"ST_X(sensors.coordinates::geometry) AS lng, " +
													"ST_Y(sensors.coordinates::geometry) AS lat, " +
													"sensors.created, " +
													"sensors.updated " +
												"FROM Sensors sensors JOIN Water_Bodies water_bodies ON sensors.water_body_id=water_bodies.water_body_id " +
												"WHERE sensors.sensor_id=$1;";

											// Database Query
											client.query(query, [
												req.params.sensor_id
											], function(err, result) {
												done();

												if(err) {
													res.status(errors.database.error_2.code).send(_.extend(errors.database.error_2, err));
													return console.error(errors.database.error_2.message, err);
												} else {

													// Send Result
													res.status(200).send(result.rows[0]);
												}
											});

										} else // Check if authenticated user is admin
											if(username === db.admin) {

												query = "SELECT " +
														"sensors.sensor_id, " +
														"sensors.creator, " +
														"sensors.device_id, " +
														"sensors.description ," +
														"sensors.private, " +
														"sensors.creator, " +
														"sensors.online_status, " +
														"sensors.water_body_id, " +
														"water_bodies.water_body_type, " +
														"water_bodies.name AS water_body_name, " +
														"sensors.sensor_height, " +
														"'CENTIMETER' AS sensor_height_unit, " +
														"sensors.crossing_height, " +
														"'CENTIMETER' AS crossing_height_unit, " +
														"sensors.default_frequency, " +
														"'MILLISECONDS' AS default_frequency_unit, " +
														"sensors.danger_frequency, " +
														"'MILLISECONDS' AS danger_frequency_unit, " +
														"sensors.increased_frequency, " +
														"sensors.triggered_threshold, " +
														"sensors.triggered_weather, " +
														"sensors.threshold_value, " +
														"'CENTIMETER' AS threshold_value_unit, " +
														"ST_X(sensors.coordinates::geometry) AS lng, " +
														"ST_Y(sensors.coordinates::geometry) AS lat, " +
														"sensors.created, " +
														"sensors.updated " +
													"FROM Sensors sensors JOIN Water_Bodies water_bodies ON sensors.water_body_id=water_bodies.water_body_id " +
													"WHERE sensors.sensor_id=$1;";

												// Database Query
												client.query(query, [
													req.params.sensor_id
												], function(err, result) {
													done();

													if(err) {
														res.status(errors.database.error_2.code).send(_.extend(errors.database.error_2, err));
														return console.error(errors.database.error_2.message, err);
													} else {

														// Send Result
														res.status(200).send(result.rows[0]);
													}
												});

										} else {
											res.status(errors.authentication.error_2.code).send(errors.authentication.error_2);
											return console.error(errors.authentication.error_2.message);
										}
									});
								}
							}
						}
					}
				});

			} else {
				res.status(errors.database.error_3.code).send(errors.database.error_3);
				return console.error(errors.database.error_3.message);
			}
		}
	});
};
Beispiel #10
0
function getEntities(req, res, next, type) {
    var meta = resources[type.toLowerCase()];
    var page = req.query.page === undefined ? 1 : new Number(req.query.page);
    if (isNaN(page) || page <= 0) {
        res.send(400, "Parameter 'page' expects a number greater than 0");
        return next();
    }
    var where = '';
    if (meta.search) {
        var isFirst = true;
        for (var elem in req.query) {
            if (meta.search[elem] !== undefined) {
                if (!isFirst)
                    where += ' and ';
                else
                    isFirst = false;
                if (typeof req.query[elem] === 'string' && req.query[elem].indexOf(',') >= 0) {
                    var parts = req.query[elem].split(',');
                    where += '(';
                    for (var i =0; i < parts.length; i += 1) {
                        if (i> 0) {
                            where += ' or ';
                        }
                        where += util.format(meta.search[elem], parts[i]);
                    }
                    where += ')';
                } else {
                where += util.format(meta.search[elem], req.query[elem]);
                }
            }
        }
    }
    if (where) where = 'WHERE ' + where;

    pg.connect(connString, function (err, client, done) {
        if (err) {
            console.error('error fetching client from pool', err);
            res.send(500);
            return next();
        }

        async.waterfall([
            //count
            function (cb) {
                var count_qry = meta.queries['count'];
                count_qry = util.format(count_qry, where ? where : '');
                client.query(count_qry, function (err, result) {
                    done();
                    if (err) {
                        cb(new Error('error running query - ' + err));
                    }
                    else
                        cb(null, new Number(result.rows[0].count));
                });
            },
            //get paged result
            function (totalResults, cb) {
                var get_qry = meta.queries['list'];
                var limit = 'ALL';
                var offset = 0;

                var size = (req.query._count !== undefined ? req.query._count : config.pageSize) || 0;

                if (size) {
                    limit = size;
                    if (page) {
                        offset = (page - 1) * size;
                    }
                }
                if (offset && offset >= totalResults) {
                    res.send(404);
                    cb(null);
                    return;
                }

                var entities = {
                    totalResults: totalResults,
                    link: [],
                    entry: []
                };

                if (totalResults == 0) {
                    delete entities.link;
                    res.send(200, entities);
                    cb(null);
                    return;
                }

                var entityToJson = meta.toJson;
                get_qry = util.format(get_qry, where ? where : '');
                client.query(get_qry, [limit, offset], function (err, result) {
                    done();
                    if (err) {
                        cb(err);
                        return;
                    }

                    var searchUrl = baseUrl + meta.urlSearchFragment;
                    var currentPage = page ? page : 1;
                    var nextPage = totalResults > offset + limit ? currentPage + 1 : undefined;
                    var prevPage = offset > 0 ? currentPage - 1 : undefined;

                    entities.link.push({ rel: "self", href: searchUrl + "?page=" + currentPage });

                    if (prevPage)
                        entities.link.push({ rel: "previous", href: searchUrl + "?page=" + prevPage });

                    if (nextPage)
                        entities.link.push({ rel: "next", href: searchUrl + "?page=" + nextPage });

                    for (var i = 0; i < result.rows.length; i++) {
                        var row = result.rows[i];
                        var entity = entityToJson(row);
                        entities.entry.push({
                            id: entity.id,
                            content: entity.content
                        });
                    }
                    res.send(200, entities);
                    cb(null);
                });
            }],
            //callback
            function (err) {
                if (err) {
                    console.error(err);
                    res.send(500);
                }
                return next();
            });
    });

}
Beispiel #11
0
exports.index = function(req, res){

  var post = req.body;

  var algorithm = 'aes256'; // or any other algorithm supported by OpenSSL
  var key = 'VSkgZmUaqxnMaSvjNExDZvKX';
  var text =  post.password; //'macc2';
  var cipher = crypto.createCipher(algorithm, key);  
  var encrypted = cipher.update(text, 'utf8', 'hex') + cipher.final('hex');  

  var consString = process.env.DATABASE_URL || "postgres://*****:*****@localhost/postgres";

  var errorMsg;

  if( post.user.toLowerCase() !== '' && post.password !== '')
  {
    pg.connect( consString , function(err, client) {
          if(err) {
                 console.error('could not connect to postgres', err);                 
                 return res.render('signup', { title: '<=Server Error Try Later=>' });                 
                }

          client.query('INSERT INTO app_users(username,password) values($1,$2) RETURNING id',
                        [post.user.toLowerCase(), encrypted],
                        function(err, result) {
                          var resMsg;
                          if (err) {
                              console.log(err);
                              errorMsg = err;
                          } else {
                              console.log('row inserted with id: ' + result.rows[0].id);
                          }
                          resMsg = (( errorMsg === undefined) ? 
                                        'Sign up success! proceed to log in' :
                                        'Duplicate user name found try different ID!');
   
                          res.render('signup', { title: resMsg });
                          }

          );

          var query = client.query("SELECT username,password FROM app_users");
         
          query.on('row', function(row) {
            console.log(row);
          });
          query.on('end', function() {
              client.end();
          });
          
        });
   
    
   
    
  } else {
    res.render('signup', { title: 'MACC sign up page ( failed attempt... retry!)' });
  }
  
  
};
Beispiel #12
0
// GZip serving middleware must be declared before static folder declaration.
app.get([ '*.js', '*.json' ], serveGzipMiddleware)

// Serve post images from S3 bucket.
app.get('/images/posts/:id/:file', (req, res) => {
	var { id, file } = req.params
	res.redirect(`http://${S3_BUCKET_NAME}/images/posts/${id}/${file}`)
})

app.use(express.static('public'))

pg.connect(DATABASE_URL, function(err, client, done) {

	if (err == null) {
		app.use(function(req, res, next) {
			req.dbClient = client
			next()
		});
	} else {
		console.log('Could not connect to the database.')
	}

	app.use(router)

	app.listen(PORT, function() {
		console.log(`Server listening on port ${PORT}`)
	})

})
Beispiel #13
0
app.get('/api/phenotypedata', function(req, res) {

    var observedRowData = [];
    var scoreRowData = [];
    var scoreObj = {};
    var rawTraitData = {};
    var traitStatistics = {};

    // Get a Postgres client from the connection pool
    pg.connect(conString, function(err, client, done) {
        // Handle connection errors
        if(err) {
          done();
          console.log(err);
          return res.status(500).json({ success: false, data: err});
        }

        //var scoreQuery = client.query("SELECT * FROM phenotypeb WHERE datacategory = 'calculated' AND huid IN ('hu011C57','hu016B28','hu0211D6','hu025CEA')");
        var scoreQuery = client.query({
          text: "SELECT * FROM phenotypeb WHERE datacategory = 'calculated' AND version = $1 AND huid IN (SELECT DISTINCT huid FROM phenotypeb WHERE datacategory = 'observed')",
          values: [req.query.calcVersion]
        });
        scoreQuery.on('row', function(row) {
              scoreRowData.push(row);
        });

        scoreQuery.on('end', function() {
          done();
          scoreObj = _.keyBy(scoreRowData,'huid');

          // SQL Query > Select Data
          //var observedQuery = client.query("SELECT * FROM phenotypeb WHERE datacategory = 'observed' AND huid IN ('hu011C57','hu016B28','hu0211D6','hu025CEA')");
          var observedQuery = client.query({
            text: "SELECT * FROM phenotypeb WHERE datacategory = 'observed' AND version = $1 AND huid IN (SELECT DISTINCT huid FROM phenotypeb WHERE datacategory = 'calculated')",
            values: [req.query.obsVersion]
          });

          // Stream results back one row at a time
          observedQuery.on('row', function(row) {
              observedRowData.push(row);
          });

          // After all data is returned, close connection and return results
          observedQuery.on('end', function() {
              done();

              var plotData = {};
              observedRowData.forEach(function(cv) {
                // Lookup the cooresponding rsid score for the individual (cv).
                _.forOwn(cv, function(value, key) {

                  // Ignore huid or datacategory properties
                  if (key === 'huid' || key === 'datacategory' || key === 'version') { return; }

                  // Get a referenence to the calculated score for the given huid and phenotypic trait (hey)
                  var currentScoreValue = scoreObj[cv.huid][key];

                  // If it's the first time we're seeing this trait, create a property for it, initially defined as an empty object.
                  if (typeof plotData[key] == 'undefined') {
                    plotData[key] = {};
                  }

                  // If it's the first time we're seeing a given 'calculated score' and 'observed value' combination, create an object for that combo.
                  if (typeof plotData[key][currentScoreValue + '_' + value]  == 'undefined') {
                    plotData[key][currentScoreValue + '_' + value] = {
                      'traitObserved': value,
                      'traitScore': currentScoreValue,
                      'count': 1,
                    };
                  }
                  // Assuming we've already created an object for a given score and observation value, increment the counter for that object.
                  else {
                    plotData[key][currentScoreValue + '_' + value].count++;
                  }

                  if (typeof rawTraitData[key] == 'undefined') {
                    rawTraitData[key] = {};
                  }

                  rawTraitData[key][cv.huid] = {
                    traitObserved: value,
                    traitScore: currentScoreValue,
                  };

                  // Initialize base properties of traitStatistics
                  if (typeof traitStatistics[key] == 'undefined') {
                    traitStatistics[key] = {};
                  }
              });
            });

            _(rawTraitData).forOwn(function(traitData, key){
              var allTraitScores = [];
              var allTraitObservations = [];
              var observedScores = [];
              var notObservedScores = [];

              _(traitData).forOwn(function(huid){
                if(huid.traitObserved === 1){
                  observedScores.push(huid.traitScore);
                } else {
                  notObservedScores.push(huid.traitScore);
                }

                // console.log(huid.traitScore);
                allTraitScores.push(huid.traitScore);
                allTraitObservations.push(huid.traitObserved);

              });

              var traitItem = traitStatistics[key];

              traitItem.totalObserved = observedScores.length;
              traitItem.totalNotObserved = notObservedScores.length;

              traitItem.observedStdDev = traitItem.totalObserved === 0 ? 'N/A' : math.std(observedScores).toFixed(3);
              traitItem.notObservedStdDev = traitItem.totalNotObserved === 0 ? 'N/A' : math.std(notObservedScores).toFixed(3);
              traitItem.observedMeanScore = traitItem.totalObserved === 0 ? 'N/A' : math.mean(observedScores).toFixed(3);
              traitItem.notObservedMeanScore = traitItem.totalNotObserved === 0 ? 'N/A' : math.mean(notObservedScores).toFixed(3);
              traitItem.observedMedianScore = traitItem.totalObserved === 0 ? 'N/A' : math.median(observedScores).toFixed(3);
              traitItem.notObservedMedianScore = traitItem.totalNotObserved === 0 ? 'N/A' : math.median(notObservedScores).toFixed(3);
            });

            return res.json({
              plotData: plotData,
              traitStatistics: traitStatistics,
            });
        });
    });
});
});
Beispiel #14
0
 create   : function(callback) {
        pg.connect(connectionString, function(err, client) { 
            callback(err, client);
        });
 
 },
	    requestify.get(userInfoUrl).then(function(response3) {

	      var uid = response3.getBody().response.user.user_name;
	      var id = response3.getBody().response.user.id;
	      var user_name = response3.getBody().response.user.user_name;
	      var first_name = response3.getBody().response.user.first_name;
	      var last_name = response3.getBody().response.user.last_name;
	      var user_avatar = response3.getBody().response.user.user_avatar;
	      var user_avatar_hd = response3.getBody().response.user.user_avatar_hd;
	      var user_cover_photo = response3.getBody().response.user.user_cover_photo;
	      var user_cover_photo_offset = response3.getBody().response.user.user_cover_photo_offset;
	      var location = response3.getBody().response.user.location;
	      var bio = response3.getBody().response.user.bio;
	      var url = response3.getBody().response.user.url;
	      var untappd_url = response3.getBody().response.user.untappd_url;
	      var twitter = response3.getBody().response.user.contact.twitter;
	      var foursquare = response3.getBody().response.user.contact.foursquare;
	      var facebook = response3.getBody().response.user.contact.facebook;
	      var email_address = response3.getBody().response.user.settings.email_address;

	      firstName = first_name;
	      lastName = last_name;
	      userName = user_name;

	      console.log(process.env.DATABASE_URL);

	      pg.defaults.ssl = true;
	      pg.connect(process.env.DATABASE_URL, function(err, client) {
	        if (err) throw err;

	        var query = "INSERT INTO salesforce.untappduser__c (" +
	            "uid__c," +
	            "user_name__c," +
	            "facebook__c," +
	            "bio__c," +
	            "location__c," +
	            "email_address__c," +
	            "user_avatar_hd__c," +
	            "user_avatar__c," +
	            "first_name__c," +
	            "foursquare__c," +
	            "untappd_url__c," +
	            "last_name__c," +
	            "twitter__c," +
	            "url__c," +
	            "user_cover_photo__c," +
	            "id__c," +
	            "name) " +
	          "SELECT '" +
	            uid + "','" +
	            user_name + "','" +
	            facebook + "','" +
	            bio + "','" +
	            location + "','" +
	            email_address + "','" +
	            user_avatar_hd + "','" +
	            user_avatar + "','" +
	            first_name + "','" +
	            foursquare + "','" +
	            untappd_url + "','" +
	            last_name + "','" +
	            twitter + "','" +
	            url + "','" +
	            user_cover_photo + "','" +
	            id + "','" +
	            id + "' WHERE NOT EXISTS ( SELECT id__c FROM salesforce.untappduser__c WHERE id__c = '" + id + "' )";

	        console.log(query);

	        client.query(query, function(err, result) {
	          if (err) throw err;

	          console.log(result);
	        });

	        var query = "INSERT INTO accesstokens (token, uid) SELECT '" + accessToken + "','" + uid + 
	          "' WHERE NOT EXISTS ( SELECT token FROM accesstokens WHERE uid = '" + uid + "')";

	        client.query(query, function(err, result) {
	          if (err) throw err;

	          console.log(result);
	        });

	        response.redirect('/profile');
	      });
	    });
Beispiel #16
0
router.post('/pmt_partner_sankey_activities', jsonParser, function (req, res) {
    try {
                
        // validate data_group_ids (optional paramater) data groups to restrict data to
        var data_group_ids = req.body.data_group_ids;
        if (typeof data_group_ids !== 'string' || typeof data_group_ids == 'undefined') {
            res.status(400).json({ errCode: 400, status: "ERROR", message: "You must specify data_group_ids as an string in the JSON of your HTTP POST." });
            return;
        }
              
        // check organization
        var organization = req.body.organization;
        if (typeof organization !== 'string') {
            res.status(400).json({ errCode: 400, status: "ERROR", message: "You must specify an organization as a string in the JSON of your HTTP POST." });
            return;
        }        
        
        // check partnerlink_level
        var partnerlink_level = req.body.partnerlink_level;
        if (typeof partnerlink_level !== 'number') {
            res.status(400).json({ errCode: 400, status: "ERROR", message: "You must specify a partnerlink_level as number in the JSON of your HTTP POST." });
            return;
        }
        
        // check pmtId
        var pmtId = req.body.pmtId;
        if (typeof pmtId !== 'number') {
            res.status(400).json({ errCode: 400, status: "ERROR", message: "You must specify a pmtId as an integer in the JSON of your HTTP POST." });
            return;
        }
        
        // validate pg object in the config by the pmtId
        if (typeof config.pg[pmtId] !== 'object') {
            res.status(400).json({ errCode: 400, status: "ERROR", message: "Your pmtId does not correspond to a valid database instance." });
            return;
        }
        
        // create connection to database
        var conString = "postgres://" + config.pg[pmtId].user + ":" +
            config.pg[pmtId].password + "@" +
            config.pg[pmtId].host + "/" + config.pg[pmtId].database;
        
        // call function
        pg.connect(conString, function (err, client, done) {
            if (err) {
                return console.error('error fetching client from pool', err);
            }
            client.query('SELECT * FROM pmt_partner_sankey_activities($1, $2, $3)',[data_group_ids, organization, partnerlink_level], function (err, result) {
                done();
                if (err) {
                    res.status(500).json({ errCode: 500, status: "ERROR", message: "There was an error in the execution of API request. Contact the administrator." });
                    return;
                }
                var json = JSON.stringify(result.rows);
                res.writeHead(200, { 'content-type': 'application/json', 'content-length': Buffer.byteLength(json) });
                res.end(json);
            });
        });
    }
    catch (ex) {
        console.log(ex);
        res.status(400).json({ errCode: 400, status: "ERROR", message: "/pmt_partner_sankey_activities failed", ex: ex });
    }
});
Beispiel #17
0
var pg = require('pg');
var conString = "postgres://localhost/twitterdb";

//this initializes a connection pool
//it will keep idle connections open for a (configurable) 30 seconds
//and set a limit of 10 (also configurable)
pg.connect(conString, function(err, client, done) {
  if(err) {
    return console.error('error fetching client from pool', err);
  }
  client.query('SELECT * FROM Tweets WHERE userId IN ($1, $2, $3)',[4,2,8], function(err, result) {
    //call `done()` to release the client back to the pool
    done();

    if(err) {
      return console.error('error running query', err);
    }
    console.log('hello');
    console.log(result.rows);
    //output: 1
  });
});
Beispiel #18
0
router.post('/pmt_partner_sankey', jsonParser, function (req, res) {
    try {

        // validate data_group_ids (optional paramater) data groups to restrict data to
        var data_group_ids = req.body.data_group_ids;
        if (typeof data_group_ids !== 'string' || typeof data_group_ids == 'undefined') {
            data_group_ids = null;
        }
        
        // validate classification_ids (optional paramater) classifications to restrict data to
        var classification_ids = req.body.classification_ids;
        if (typeof classification_ids !== 'string' || typeof classification_ids == 'undefined') {
            classification_ids = null;
        }
        
        // validate org_ids (optional paramater) organizations to restrict data to
        var org_ids = req.body.org_ids;
        if (typeof org_ids !== 'string' || typeof org_ids == 'undefined') {
            org_ids = null;
        }
      
        // validate start_date (optional paramater) to restrict data to
        var start_date = req.body.start_date;
        if (typeof start_date !== 'string' || typeof start_date == 'undefined') {
            start_date = null;
        }
        
        // validate end_date (optional paramater) to restrict data to
        var end_date = req.body.end_date;
        if (typeof end_date !== 'string' || typeof end_date == 'undefined') {
            end_date = null;
        }
        
        // validate unassigned_taxonomy_ids (optional paramater) to restrict data that is not assigned a taxonomy
        var unassigned_taxonomy_ids = req.body.unassigned_taxonomy_ids;
        if (typeof unassigned_taxonomy_ids !== 'string' || typeof unassigned_taxonomy_ids == 'undefined') {
            unassigned_taxonomy_ids = null;
        }
        
        // validate pmtId
        var pmtId = req.body.pmtId;
        if (typeof pmtId !== 'number') {
            res.status(400).json({ errCode: 400, status: "ERROR", message: "You must specify a pmtId as an integer in the JSON of your HTTP POST." });
            return;
        }
        
        // validate pg object in the config by the pmtId
        if (typeof config.pg[pmtId] !== 'object') {
            res.status(400).json({ errCode: 400, status: "ERROR", message: "Your pmtId does not correspond to a valid database instance." });
            return;
        }
        
        // create connection to database
        var conString = "postgres://" + config.pg[pmtId].user + ":" +
            config.pg[pmtId].password + "@" +
            config.pg[pmtId].host + "/" + config.pg[pmtId].database;

        // call function
        pg.connect(conString, function (err, client, done) {
            if (err) {
                res.status(500).json({errCode: 500, status: "ERROR", message: "Unable to connect to database.", error: err});
                done();
                return;
            }

            client.query("SELECT * FROM pmt_partner_sankey($1, $2, $3, $4, $5, $6)",
                    [data_group_ids, classification_ids, org_ids, start_date, end_date, unassigned_taxonomy_ids], function (err, result) {
                done();
                if (err) {
                    res.status(500).json({errCode: 500, status: "ERROR", message: "There was an error in the execution of API request. Contact the administrator.", error: err});
                    return;
                }

                var json = JSON.stringify(result.rows);
                res.writeHead(200, { 'content-type': 'application/json', 'content-length': Buffer.byteLength(json) });
                res.end(json);

            });
        });
    }
    catch (ex) {
        console.log(ex);
        res.status(400).json({errCode: 400, status: "ERROR", message: "/pmt_partner_sankey failed", ex: ex});
    }
});
exports.testStream = function(test) {
    test.expect(4);
    pg.connect(dbUrl, function(err, client, done) {
        test.ifError(err);

        // Create reference tables
        var refTable = "test_" + ("0000000000" + Math.floor(10000000000*Math.random())).slice(-10),
            testTable = "test_" + ("0000000000" + Math.floor(10000000000*Math.random())).slice(-10); 

        // Query function that binds "this" to the current client
        var _query = function(sql, callback) {
            // console.log("Executing query: %s", sql); // Debugging
            client.query.call(client, sql, callback);
        };


        // Now line up the sequence of queries to establish the reference table, and the test table
        var tasks = {
            "create_ref" : async.apply(_query, generateCreateSql(refTable)),
            "insert_ref" : async.apply(_query, generateInsertSql(refTable, [[1,2],[2,4],[3,1]])),
            "create_test" : async.apply(_query, generateCreateSql(testTable)),            
        };

        async.series(tasks, function(err, result) { // Now stream the required data to the test table
            test.ifError(err);

            // Define the read stream
            var rs = new stream.Readable({ objectMode : true }),
                streamData = [
                    [{'b':3,'a':2},{'b':1,'a':1}],
                    [{'b':4,'a':2},{'b':1,'a':3}],
                    [{'b':2,'a':1}]
                ];
            rs._read = function() {
                if( streamData.length > 0 ) {
                    this.push(streamData.shift());
                } else {
                    this.push(null);
                }
            };

            // Setup piping
            var sm = rs.pipe(pgLoader.createStream(generatePgLoaderOptions(testTable, pgLoader.SQL_REPLACE)));
            sm.on("write", function() {

            });
            sm.on("finish", function() {
                var endTasks = {
                    "compare" : async.apply(_query, generateCompareSql(refTable, testTable)),
                    "drop_ref" : async.apply(_query, generateDropSql(refTable)),
                    "drop_test" : async.apply(_query, generateDropSql(testTable))
                };

                async.series(endTasks, function(err, result) {
                    test.ifError(err);
                    var compareResult = result.compare.rows;
                    test.equal(compareResult.length, 0, "Received non-empty difference from reference, with " + compareResult.length + " rows.");
                    done();
                    test.done();
                    disconnect();
                });
            });
        });
    });
};
Beispiel #20
0
exports.request = function(req, res){

	// Create URL
	var url = "postgres://" + db_settings.user + ":" + db_settings.password + "@" + db_settings.host + ":" + db_settings.port + "/" + db_settings.database_name;

	// Connect to Database
	pg.connect(url, function(err, client, done) {
		if(err) {
			res.status(errors.database.error_1.code).send(errors.database.error_1);
			return console.error(errors.database.error_1.message, err);
		} else {

			// Check if User was authenticated
			if(!req.headers.authorization || req.headers.authorization === ""){
				res.status(errors.authentication.error_3.code).send(errors.authentication.error_3);
				return console.error(errors.authentication.error_3.message);
			} else {

				// Decode Token
				jwt.verify(req.headers.authorization, secret.key, function(err, decoded) {
					if (err) {
						res.status(errors.authentication.error_2.code).send(errors.authentication.error_2);
						return console.error(errors.authentication.error_2.message);
			        } else {

						// Get username from authenticated user
						var username = decoded.username;

						// Check if authenticated user is the right user or admin
						if(username === req.params.username || username === db.admin) {

							// Database Query
			                client.query("SELECT * FROM Users WHERE username=$1;", [
			                    req.params.username
			                ], function(err, result) {
			                    done();

			                    if(err) {
			                        res.status(errors.database.error_2.code).send(_.extend(errors.database.error_2, err));
			                        return console.error(errors.database.error_2.message, err);
			                    } else {

			                        // Check if User exists
			                        if(result.rows.length === 0) {
										res.status(errors.query.error_1.code).send(errors.query.error_1);
										return console.error(errors.query.error_1.message);
			                        } else {

										var query = "SELECT " +
												"subscriptions.subscription_id, " +
												"subscriptions.created, " +
												"subscriptions.updated, " +
												"subscriptions.creator, " +
												"subscriptions.sensor_id, " +
												"sensors.description AS sensor_description, " +
												"subscriptions.threshold_id, " +
												"thresholds.description AS threshold_description, " +
												"thresholds.category AS threshold_category " +
											"FROM Subscriptions subscriptions JOIN Sensors sensors ON subscriptions.sensor_id=sensors.sensor_id " +
											"JOIN Thresholds thresholds ON subscriptions.threshold_id=thresholds.threshold_id "+
											"WHERE subscriptions.creator=$1 AND subscription_id=$2;";

										// Database Query
										client.query(query, [
											req.params.username,
											req.params.subscription_id
										], function(err, result) {
											done();

											if(err) {
												res.status(errors.database.error_2.code).send(_.extend(errors.database.error_2, err));
												return console.error(errors.database.error_2.message, err);
											} else {

												// Check if Subscription exists
						                        if(result.rows.length === 0) {
													res.status(errors.query.error_5.code).send(errors.query.error_5);
													return console.error(errors.query.error_5.message);
						                        } else {

													// Send Result
													res.status(200).send(result.rows[0]);
												}
											}
										});
									}
								}
							});
						} else {
							res.status(errors.authentication.error_2.code).send(errors.authentication.error_2);
							return console.error(errors.authentication.error_2.message);
						}
					}
				});
			}
		}
	});
};
Beispiel #21
0
  return _(function(push, next) {
    return pg.connect(DATABASE_URL, function(err, client, done) {
      if (err) {
        done();

        push(err);
        // treat this as a fatal error
        return push(null, _.nil);
      }

      var query = client.query.bind(client),
          select = [
              "SELECT id, width, ST_XMin(geom) AS lngmin, ST_YMin(geom) AS latmin, ST_XMax(geom) AS lngmax, ST_YMax(geom) AS latmax, last_fetched",
              "FROM foursquare_regions",
              "WHERE locked_at IS NULL",
              "  AND split = false",
              "  AND (last_fetched IS NULL OR last_fetched <= NOW() - interval '1 day')",
              "ORDER BY COALESCE(last_fetched, '2014-10-04'::timestamp with time zone) ASC, width DESC",
              "LIMIT $1"
            ].join("\n"),
          update = "UPDATE foursquare_regions SET locked_at = NOW() WHERE id IN";

      // perform queries in a transaction to prevent visibility leakage
      return async.series([
        async.apply(query, "BEGIN"),
        // lock rows so multiple sources won't try to lock the same regions
        async.apply(query, "LOCK TABLE foursquare_regions IN ROW EXCLUSIVE MODE"),
        async.apply(async.waterfall, [
          async.apply(query, select, [batchSize]),
          function(result, done) {
            if (result.rows.length === 0) {
              return done(null, result.rows);
            }

            var placeholders = [];

            // extract the region ids and create placeholders for them
            var regionIds = result.rows.map(function(row, i) {
              placeholders.push("$" + (i + 1));

              return row.id;
            });

            result.rows.forEach(function(row) {
              row.bbox = [
                row.lngmin,
                row.latmin,
                row.lngmax,
                row.latmax
              ];
            });

            // tweak the UPDATE string to include placeholders
            update += util.format(" (%s)", placeholders.join(", "));

            return query(update, regionIds, function(err) {
              // use a custom callback to propagate rows forward
              return done(err, result.rows);
            });
          }
        ]),
        async.apply(query, "COMMIT")
      ], function(err, result) {
        if (err) {
          // rollback and report the original error
          return query("ROLLBACK", function(e) {
            if (e) {
              console.warn(e.stack);
            }

            // release the client back into the pool
            done();

            // propagate the error
            push(err);

            // treat this as a fatal error and end the stream
            return push(null, _.nil);
          });
        }

        // release the client back into the pool
        done();

        var rows = result[2];

        if (rows.length === 0) {
          // all caught up / everything is locked
          console.log("No more regions to fetch.");
          push(null, _.nil);
        }

        // yield each row to downstream consumer(s)
        rows.forEach(_.curry(push, null));

        return next();
      });
    });
  });
var getClient = function(callback) {
  pg.connect(connectionString, callback)
}
Beispiel #23
0
var http = require('http');
var pg = require('pg');
var conString = "postgres://*****:*****@localhost:5432/flashy";
var client;
var done;

// get a pg client from the connection pool
pg.connect(conString, function(err, pgclient, pgdone) {
	client = pgclient;
	done = pgdone;
});

var handleError = function(err, res) {
  // no error occurred, continue with the request
  if(!err) return false;
  // An error occurred, remove the client from the connection pool.
  // A truthy value passed to done will remove the connection from the pool
  // instead of simply returning it to be reused.
  // In this case, if we have successfully received a client (truthy)
  // then it will be removed from the pool.
  done(client);
  res.writeHead(500, {'content-type': 'text/plain'});
  res.end('An error occurred');
  return true;
};

function index(request, response){
  client.query('SELECT content FROM cards', function(err, result) {
  	response.setHeader('Content-Type', 'application/json');
  	response.write(JSON.stringify(result["rows"]));
var pg = require('pg');
var util = require('util');

/**
 * /Applications/Postgres.app/Contents/MacOS/bin/psql events
 */

pg.connect('postgres://localhost:5432/events', function(err, cl, done){

    console.log('connected: %s', err);
    cl.query("SELECT * FROM pg_catalog.pg_tables WHERE  schemaname NOT IN  ('pg_catalog', 'information_schema');"
        , function(err, result){
        console.log('tables: %s, %s', err, util.inspect(result));
        done();
    })
})
Beispiel #25
0
var globalClient;
var baseArticleTextUrl = 'http://www.britishnewspaperarchive.co.uk/tags/itemocr/BL/';
var conString = "postgres://*****:*****@localhost:5432/bbcpapers";
var addQuery = 'insert into articles (id, url, pub_date, newspaper, page_number, no_words, article_text, type, search_term) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)';

var allUrls = new Array(germanPages+1)
    .join().split(',')
    .map(function(item, index){ return germanUrl + index++;});

var testUrl = naziUrl+'0';

pg.connect(conString, function(err, client, done) {
	if(err) throw err;
	globalClient = client;

 	async.eachLimit(allUrls, 5, extractStoryUrls, function(err){
		done();
		client.end();
	});
});


/*
* @async PassThrough
*/
function extractStoryUrls(searchPageUrl, asyncCallback) {
	request(searchPageUrl, function (error, response, body) {
		if (error) throw error;

		if (!error && response.statusCode == 200) {
			var $ = cheerio.load(body);
var app = express();
var bodyparser = require('body-parser');
var port = process.env.PORT || 3000;
var pg = require('pg');
var connectionString;

app.use(bodyparser.json());
app.use(bodyparser.urlencoded({extended:true}));

if (process.env.DATABASE_URL){
    pg.defaults.ssl=true;
    connectionString = process.env.DATABASE_URL;
} else {
    connectionString = 'postgress://localhost:5432/db_tasks';
}

console.log("connection string =", connectionString);

pg.connect(connectionString, function(err, client, done){
    if(err){
        console.log("error connecting to database", err);
    }
});

app.use('/',index);

var server= app.listen(port,function(){
    var port = server.address().port;
    console.log("Something is alive on port:", port);
})
Beispiel #27
0
app.post('/campaignmemberdetails', cors, function (request, response) {
  response.header("Access-Control-Allow-Origin", "*");
  response.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  //next();
	//response.header("Access-Control-Allow-Origin", "*")
	//response.json(posts);
	var bContinueProcessing = true;
	//console.log("REQUEST     : "+request);
	//console.log("REQUEST BODY: "+request.body);
    //console.log(JSON.stringify(request.body));
	/*for (var prop in request.body) {
	    if (request.body.hasOwnProperty(prop)) {
	        console.log("1--"+prop +"-->"+request.body[prop]);
			console.log("object: "+JSON.stringify(request.body[prop]));
	    }
	}    
	console.log("REQUEST BODY: "+JSON.stringify(request.body));*/
	var newCampaignDetail = request.body;
	newCampaignDetail.activity_date__c = dateFormat(new Date(), "yyyy-mm-dd");
	newCampaignDetail.activity_date_and_time__c = dateFormat(new Date(), "yyyy-mm-dd h:MM:ss");
	console.log("newCampaignDetail: "+JSON.stringify(newCampaignDetail));
	var validationErrors  = "";
	
	validationErrors=performValidations(newCampaignDetail);
	//console.log("1-Validation errors: "+validationErrors+" len: "+validationErrors.length);
	if  (validationErrors.length>0) {
		bContinueProcessing = false;
		console.log("2-Validation errors: "+validationErrors+" len: "+validationErrors.length);
		response.send(validationErrors);
	}
	
	/*bCampaignExists = campaignExists (newCampaignDetail);
	console.log("returning results of campaignExists: " + bCampaignExists);
	if (bCampaignExists=="true") {
		console.log("Campaign exists!: "+bCampaignExists);
	} else {
		console.log("xNon-existent campaign ID: "+bCampaignExists);
		response.send("yNon-existent campaign ID");
	}*/
	
	// No errors from the validations, so we can continue
	if ( bContinueProcessing ) {
	
		//The logic below first checks to see if the campaign exists
		var sSQL = buildQuery(2)+newCampaignDetail.Campaign__c+"'";
		console.log("##### 100 ### bCampaignChecked: "+bCampaignChecked+" ### bCampaignExists: "+bCampaignExists+
					" ### bSubscriberKeyChecked: "+bSubscriberKeyChecked+" ### bSubscriberKeyFound: "+bSubscriberKeyFound+
					" ### bEmailAddressChecked: "+bEmailAddressChecked+" ### bEmailAddressFound: "+bEmailAddressFound);
		console.log("campaignExists: executing query: "+sSQL);
		pg.connect(process.env.DATABASE_URL, function(err, client, done) {
			client.query(sSQL, function(err, result) {
				done();
				if (err) { 
					console.error(err); 
					console.log("Non-existent campaign ID: "+newCampaignDetail.Campaign__c);
					response.send("Non-existent campaign ID: "+newCampaignDetail.Campaign__c+"\n"+err);
				} else { 
					if ( result.rows.length > 0 ) {
						bCampaignExists = true;
						console.log("Campaign exists!: "+bCampaignExists);
						console.log ("1-rows: "+JSON.stringify(result.rows)+
										" rows: "+result.rows.length+
										" sfid: "+result.rows[0].sfid);
						checkForContactBySubscriberKey(newCampaignDetail, request, response, next);
					} else {
						console.log("Non-existent campaign ID: "+newCampaignDetail.Campaign__c);
						response.send("Error:\tNon-existent campaign ID "+newCampaignDetail.Campaign__c+"\n");
					}
				}
			});
			
		});
	}
})
Beispiel #28
0
var pg = require('pg');
var sys = require('sys');
var connectionString = "postgres://*****:*****@localhost/planner";
fs = require('fs');
var client;
var after = function(callback) {
    return function(err, queryResult) {
      if(err) {
        console.log("Error! " + sys.inspect(err));
      }
      callback(queryResult)
    }
  }
pg.connect(connectionString, after(function(cli) {
    client = cli;
    console.log("connected");
    slurp(client);
  }));


function slurp(client) {
  client.query( 'select * from users ', after(function(results) {
      db.users = {};
      db.groups = {};
      db.plan = {};
      db.usernames = {};  // hashed on lastname+firstname -- used to check for double regs
      // not all doubles are invalid - some are two users with same name
      // these need to be inspected by teach/admin
      for (var ii in results.rows) {
        var u = results.rows[ii];
        db.users[u.username] = u;
Beispiel #29
0
  function cmd_change_avatar (args, done) {
    var hostname = args.zenHostname;
    var file = args.file;

    if (!_.contains(args.fileType, 'image')) return done(null, {ok: false, why: 'Avatar upload: file must be an image.'});
    if (file.length > 5242880) return done(null, {ok: false, why: 'Avatar upload: max file size of 5MB exceeded.'});

    // pg conf properties
    options.postgresql.database = options.postgresql.name;
    options.postgresql.user = options.postgresql.username;

    pg.connect(options.postgresql, function (err, client) {
      if (err) { return seneca.log.error('Could not connect to postgres', err); }

      var man = new LargeObjectManager(client);

      client.query('BEGIN', function (err) {
        if (err) {
          seneca.log.error('Unable to create transaction');
          done(err);
          return;
        }

        var bufferSize = 16384;
        man.createAndWritableStream(bufferSize, function (err, oid, stream) {
          var noop = function () {};
          var avatarInfo = {
            oid: oid.toString(),
            sizeBytes: 0,
            name: args.fileName,
            type: args.fileType
          };

          if (err) {
            seneca.log.error('Unable to create a new large object');
            client.end();
            done(err);
            done = noop;
            return;
          }

          var buf = new Buffer(file, 'base64');

          stream.write(buf, 'base64', function () {
            stream.end();
          });

          stream.on('data', function (chunk) {
            seneca.log.info('got ' + chunk.length + ' bytes of data');
            avatarInfo.sizeBytes += chunk.length;
          });

          stream.on('finish', function () {
            seneca.log.info('Uploaded largeObject. committing...', oid);
            client.query('COMMIT', function () {
              client.end();
              seneca.log.info('Saved LargeObject', oid);

              // update profile record with avatarInfo
              var profile = {
                id: args.profileId,
                avatar: avatarInfo
              };

              seneca.act({role: plugin, cmd: 'save'}, {profile: profile}, function (err, profile) {
                if (err) {
                  return done(err);
                }

                seneca.make$(ENTITY_NS).load$(profile.id, function (err, profile) {
                  if (err) seneca.log.error(err);

                  var protocol = process.env.PROTOCOL || 'http';

                  var forumProfile = _.clone(profile);
                  forumProfile.username = forumProfile.name;

                  forumProfile.uploadedpicture = protocol + '://' + hostname + '/api/1.0/profiles/' + profile.id + '/avatar_img';
                  forumProfile.picture = protocol + '://' + hostname + '/api/1.0/profiles/' + profile.id + '/avatar_img';

                  seneca.act({role: 'cd-nodebb-api', cmd: 'update', user: forumProfile, id: forumProfile.userId}, function (err, res) {
                    if (err) seneca.log.error(err);
                    if (res.error) seneca.log.error('NodeBB Profile Sync Error: ' + res.error);

                    done(undefined, profile);
                    done = noop;
                  });
                });
              });
            });
          });

          stream.on('error', function (err) {
            seneca.log.error('postgresql filestore error', err);
            done(err);
            done = noop;
          });
        });
      });
    });
  }
Beispiel #30
0
var create = fs.readFileSync('./db/create.sql').toString();

var populate = fs.readFileSync('./db/populate.sql').toString();

// SELECT skill.name FROM skill, position, position_skill WHERE position.name LIKE 'catcher' AND position.id = position_skill.position AND skill.id = position_skill.skill;

console.log('db reset initialized');
pg.connect(conn, function onConnect (err, client, done) {

    if (err) { console.log(err); }

    client.query(destroy);
    console.log('db destroyed');

    client.query(create, function (err, result) {
        if (err) { console.log(err); }
        console.log('db tables created');


        client.query(populate, function (err, result) {
            if (err) { console.log(err); }
            console.log('db tables populated');

            console.log('db reset complete');
            done();
            process.exit();
        });
    });
});