Skip to content

Instantly share code, notes, and snippets.

@zph
Forked from bkerley/import.js
Created June 16, 2014 18:40
Show Gist options
  • Save zph/df1dcff43e17a4600ebc to your computer and use it in GitHub Desktop.
Save zph/df1dcff43e17a4600ebc to your computer and use it in GitHub Desktop.
var fs = require('fs');
var pg = require('pg').native;
global.Grailbird = {};
global.Grailbird.data = {};
var tweetFiles = fs.readdirSync("./data/js/tweets");
for (var f in tweetFiles) {
eval('global.'+fs.readFileSync('./data/js/tweets/'+ tweetFiles[f]));
}
pg.connect('postgres://localhost/bonzoesc_tweets', function(err, client, done) {
var rollback = function(err, client, done) {
console.log("aw fuck");
console.log(err);
client.query('ROLLBACK', function(err) {
return done(err)
})
process.exit(-1)
};
client.query('BEGIN', function(err, result) {
if(err) return rollback(client);
var q = 'INSERT INTO tweets (id_str, text, body) VALUES ($1, $2, $3)';
for (var key in global.Grailbird.data) {
var month = global.Grailbird.data[key];
for (var idx in month) {
var body = month[idx]
client.query(q, [body.id_str, body.text, JSON.stringify(body)], function(err) {
if (err) return rollback(err, client, done);
})
}
}
console.log('booya');
client.query('COMMIT', done)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment