Advertisement
Guest User

Untitled

a guest
Sep 29th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. // npm install pg
  2. // bootstrap : psql -h localhost test test
  3. // create table config(id serial, conf json);
  4. // insert into config (conf) values ('{"test": 42}'::json);
  5.  
  6. var pg = require('pg');
  7. var Pool = pg.Pool;
  8.  
  9. var pool = new Pool({
  10. user: 'test',
  11. host: 'localhost',
  12. database: 'test',
  13. password: 'test',
  14. port: 5432,
  15. })
  16.  
  17. pool.connect()
  18. .then(function(client) {
  19. client.query('LISTEN test_event')
  20.  
  21. client.on('notification', function(message) {
  22. console.log('Got event=', message)
  23. })
  24.  
  25. return client.query({text: 'select * from config'})
  26. .then(function(res) {
  27. console.log('Got res=', res.rows[0]);
  28. client.query("NOTIFY test_event, '" + JSON.stringify(res.rows[0].conf) + "'")
  29. })
  30. .catch(function(error) {
  31. console.log('Got error=', error);
  32. })
  33. })
  34. .catch(function(error) {
  35. console.log('Pool error=', error)
  36. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement