-
Notifications
You must be signed in to change notification settings - Fork 217
/
Copy pathmonitor.js
58 lines (46 loc) · 1.84 KB
/
monitor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* eslint-disable */
////////////////////////////////////////////////////
// This is a complete test application, which shows
// how to use the following options:
//
// a) override the default promise library;
// b) use pg-monitor to output all the query events;
// c) change the default theme for pg-monitor;
// d) add log handler to pg-monitor, to log events into a file or elsewhere.
//
// Packages used: pg-promise, pg-monitor, bluebird.
////////////////////////////////////////////////////
const promise = require('bluebird'); // or any other Promise/A+ compatible library;
const initOptions = {
promiseLib: promise // overriding the default (ES6 Promise);
};
const pgp = require('pg-promise')(initOptions);
// See all options: http://vitaly-t.github.io/pg-promise/module-pg-promise.html
const monitor = require('pg-monitor');
monitor.attach(initOptions); // attach to all query events;
// See API: https://github.com/vitaly-t/pg-monitor#attachoptions-events-override
monitor.setTheme('matrix'); // change the default theme;
// Other themes: https://github.com/vitaly-t/pg-monitor/wiki/Color-Themes
monitor.setLog((msg, info) => {
// save the screen messages into your own log file;
});
// See API: https://github.com/vitaly-t/pg-monitor#log
// Database connection details;
const cn = {
host: 'localhost', // 'localhost' is the default;
port: 5432, // 5432 is the default;
database: 'myDatabase',
user: 'myUser',
password: 'myPassword',
// to auto-exit on idle, without having to shut down the pool;
// see https://github.com/vitaly-t/pg-promise#library-de-initialization
allowExitOnIdle: true
};
const db = pgp(cn); // database instance;
db.any('select * from users where active = $1', [true])
.then(data => {
console.log('DATA:', data);
})
.catch(error => {
console.log('ERROR:', error);
});