-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·35 lines (28 loc) · 934 Bytes
/
index.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
'use strict';
let pgQuery;
module.exports = function(pg) {
pgQuery = pgQuery || pg.Client.prototype.query;
return {
name: 'pg',
handler: function(req, res, next) {
pg.Client.prototype.query = function(config, values, callback) {
if (!req.miniprofiler || !req.miniprofiler.enabled)
return pgQuery.apply(this, arguments);
if (callback) {
req.miniprofiler.timeQuery('sql', config.toString(), pgQuery.bind(this), config, values, callback);
return;
}
const timing = req.miniprofiler.startTimeQuery('sql', config.toString());
const query = pgQuery.call(this, config, values, callback);
return query.then(result => {
req.miniprofiler.stopTimeQuery(timing);
return result;
}).catch(err => {
req.miniprofiler.stopTimeQuery(timing);
throw err;
});
};
next();
}
};
};