Skip to content

Instantly share code, notes, and snippets.

@MrArnoldPalmer
Created July 1, 2015 01:21
Show Gist options
  • Save MrArnoldPalmer/b4518fa75cf2ec820670 to your computer and use it in GitHub Desktop.
Save MrArnoldPalmer/b4518fa75cf2ec820670 to your computer and use it in GitHub Desktop.
Adding to JSON in PostgreSQL
var playerSchema = {
"steam_id": null,
"steam_username": null,
"steam_avatar_url": null,
"total_items_deposited_value": null,
"total_num_items_deposited": null,
"items": [{
"appid": null,
"contextid": null,
"classid": null,
"instanceid": null,
"icon_url": null,
"name": null,
"market_hash_name": null,
"median_price": null
}]
};
function UpdateRoundPlayers() {
var players = [];
pg.connect(connectionString, function(err, client, done) {
var query = client.query("SELECT players FROM rounds ORDER BY game_id DESC LIMIT 1");
query.on('row', function(row) {
players.push(row.players);
});
query.on('end', function() {
var updated = [];
if(players[0] == null) {
updated = [playerSchema];
}
else {
updated = players.concat(playerSchema);
}
console.log(updated);
var x = [];
pg.connect(connectionString, function(err, client, done) {
client.query("UPDATE rounds SET players=($1) WHERE game_id=(SELECT MAX(game_id) FROM rounds)", [JSON.stringify(updated)]);
var query = client.query("SELECT players FROM rounds ORDER BY game_id DESC LIMIT 1");
query.on('row', function(row) {
x.push(row);
});
query.on('end', function() {
client.end();
console.log(x);
});
if (err) {
console.log(err);
}
});
});
if (err) {
console.log(err);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment