Sunday, 14 May 2017

using postgresql with nodejs

I am trying to use a postgresql table to store a single json, retrieve it in node.js and then update it. My code seems to work, but I am asking if there is a better way to do this. Now I have this:

let JSONvariable;
//retrieve JSONvariable
client.query('SELECT * FROM tablename;').on('row', row => {
   if (row.thiskey === 'KEY') JSONvariable = row.jsonvariable;
});
//update JSONvariable
client.query(`UPDATE tablename SET jsonvariable = '${JSON.stringify(JSONvariable)}' WHERE thiskey = 'KEY';`);

My table was made:

CREATE TABLE tablename(
   thiskey varchar,
   jsonvariable json
);
INSERT INTO tablename VALUES ( 'KEY', '{}');

And is it possible to have the retrieval be synchronous?



via Orion

No comments:

Post a Comment