Skip to content

Instantly share code, notes, and snippets.

@tgriesser
Created June 24, 2014 23:57
Show Gist options
  • Save tgriesser/8b4fdd3af1419bce4d49 to your computer and use it in GitHub Desktop.
Save tgriesser/8b4fdd3af1419bce4d49 to your computer and use it in GitHub Desktop.
var Promise = require("bluebird");
var request = Promise.promisify(require('request'));
var knex = require('knex');
var pg = knex({
client: 'pg',
connection: connString
});
function getUrl(location) {
return 'https://maps.googleapis.com/maps/api/geocode/json?address=' + location.address + ', ' + location.city + ', ' + location.state + ' ' + location.zip_code;
}
pg.select('id', 'address', 'city', 'state', 'zip_code').from('companies').whereNull('pos').map(function(company) {
return request(getUrl(company)).then(function(body) {
body = JSON.parse(body)
if (body.status == 'OK') {
return pg('companies').update({
pos: pg.raw('point(?, ?)', [location.lng, location.lat])
}).where({id: company.id})
} else {
console.log('ERROR: AT COMPANY ID ' + company.id);
}
})
}).then(function(rows) {
process.exit()
}).catch(function(e) {
console.log(e.stack);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment