Guest User

Untitled

a guest
Feb 8th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const PgClient = require('pg').Client;
  4.  
  5. module.exports = function makeHelpers() {
  6. var hlp = { ctx: null };
  7.  
  8. hlp.connectToDatabase = async function (dbCfg){
  9. const dbClient = new PgClient({
  10. host : dbCfg.host,
  11. port : dbCfg.port,
  12. user: dbCfg.username,
  13. database: dbCfg.dbname,
  14. password: dbCfg.password
  15. });
  16. hlp.ctx.dbClient = dbClient;
  17.  
  18. try {
  19. await dbClient.connect();
  20. }
  21. catch (e) {
  22. console.error('Could not connect to the Postgres database.');
  23. console.error(e);
  24. process.exit(4);
  25. }
  26. };
  27.  
  28. hlp.helperFunction1 = async function (d){
  29. let res = hlp.ctx.dbClient.query('..some query..');
  30. // ...do more work
  31. return val;
  32. };
  33.  
  34. return hlp;
  35. };
Add Comment
Please, Sign In to add comment