{"id":19149,"date":"2015-05-20T14:28:11","date_gmt":"2015-05-20T08:58:11","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=19149"},"modified":"2016-08-29T10:12:00","modified_gmt":"2016-08-29T04:42:00","slug":"connect-to-postgresql-using-javascript","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/connect-to-postgresql-using-javascript\/","title":{"rendered":"Connect to PostgreSQL using JavaScript"},"content":{"rendered":"<p>One of the best practices of <a title=\"automated testing services\" href=\"http:\/\/www.tothenew.com\/testing\/automated-independent-manual-testing\">Automation testing<\/a> is to leave the system from the state where you started your test from. While doing functional testing for an <a title=\"Angular development\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">angularjs application<\/a> using <strong>Protractor<\/strong> and <strong>Cucumber<\/strong> (BDD framework), there was a need to clean up whatever data was being generated in the Data setup (generally the <b>Given<\/b>) of the test after the test finishes.<\/p>\n<p>Following is an example on how to connect to PostgreSQL using JavaScript. Here we try to delete all information from CustomerAddress table about a customer which was populated during the test.<\/p>\n<p>1. Make sure postgres dependency is installed. If not you can do this by using following command:<br \/>\n<code><br \/>\n<strong>npm install pg or npm install \u2013g pg<\/strong><br \/>\n<\/code><br \/>\n2. Include the dependency into your code by:<br \/>\n<code><br \/>\n<strong>var pg = require(\u2018pg\u2019);<\/strong><br \/>\n<\/code><br \/>\n3. Provide connection string for the postgreSQL client, port generally is default one i.e. 5432:<br \/>\n<code><br \/>\n<strong>var connectionString = \"postgres:\/\/userName:password@serverName\/ip:port\/nameOfDatabase\";<\/strong><br \/>\n<\/code><br \/>\n4. Instantiate the client for postgres database:<br \/>\n<code><br \/>\n<strong>var pgClient = new pg.Client(connectionString);<\/strong><br \/>\n<\/code><br \/>\n5. Connect to database by using following command:<br \/>\n<code><br \/>\n<strong>pgClient.connect();<\/strong><br \/>\n<\/code><br \/>\n6. Execute the query using following statement:<br \/>\n<code><br \/>\n<strong>var query = pgClient.query(\"SELECT id from Customer where name = 'customername'\");<\/strong><br \/>\n<\/code><br \/>\n7. Get the result set using:<br \/>\n<code><br \/>\n<strong>query.on(\"row\", function(row,result){<br \/>\nresult.addRow(row);<br \/>\n});<\/strong><br \/>\n<\/code><br \/>\n8. You can delete row(s) by using the following piece of code:<br \/>\n<code><br \/>\n<strong>query.on(\"end\", function(result){<br \/>\nif(result.rows[0] === undefined){<br \/>\nreturn;<br \/>\n}<br \/>\nelse{<br \/>\nvar id = result.rows[0].id;<br \/>\nvar query = \"delete from CustomerAddress where customer_id = \" + id ;<br \/>\npgClient.query(query);<br \/>\n}<br \/>\npgClient.end();<br \/>\n});<\/strong><br \/>\n<\/code><br \/>\nSteps 1, 2, 3, 4, 5 and 6 are pretty straight forward.<br \/>\nPoint 6 and 7 need a bit of explanation:<\/p>\n<p>The <strong>row<\/strong> event is dispatched whenever a row is received from db and we use it to append into an array of <strong>rows<\/strong> which is passed to a rows property in the <strong>end<\/strong> event\u2019s result object. If we do not call the <strong>result.addRow()<\/strong> method, the <strong>rows<\/strong> array would be empty in the <strong>end<\/strong> event.<\/p>\n<p>The <strong>end<\/strong> event is dispatched when all rows have been returned by the query or when an error is encountered. Here we are first checking if the result is undefined or not. If not, delete the row in the table with same id and then close the client\u2019s connection using the <strong>end()<\/strong> method on the pgClient object.<\/p>\n<p>You might need to open the port for outside connections for making calls to database through port <strong>5432<\/strong>(being default port for postgreSQL) if you are connecting to the database remotely.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the best practices of Automation testing is to leave the system from the state where you started your test from. While doing functional testing for an angularjs application using Protractor and Cucumber (BDD framework), there was a need to clean up whatever data was being generated in the Data setup (generally the Given) [&hellip;]<\/p>\n","protected":false},"author":186,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":349},"categories":[1818],"tags":[1561,4843,55,1177,942],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/19149"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/186"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=19149"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/19149\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=19149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=19149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=19149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}