|
| 1 | +var miniprofiler = require('../../miniprofiler.js'); |
| 2 | +var pg = require('pg'); |
| 3 | +var miniprofilerPG = function(pg) { |
| 4 | + return function (req, res, next) { |
| 5 | + if (req.miniprofiler.enabled) { |
| 6 | + var pgQuery = pg.Client.prototype.query |
| 7 | + pg.Client.prototype.query = function(config, values, callback) { |
| 8 | + req.miniprofiler.timeQuery('sql', 'SELECT $1::int AS number', pgQuery.bind(this), config, values, callback); |
| 9 | + pg.Client.prototype.query = pgQuery |
| 10 | + } |
| 11 | + } |
| 12 | + next() |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +var express = require('express'); |
| 17 | +var connString = "postgres://postgres:postgres@localhost/async_demo"; |
| 18 | + |
| 19 | +// Defaults to 'left'. Uncomment this to move to right. Also supports 'bottomLeft', 'bottomRight'. |
| 20 | +/* |
| 21 | +miniprofiler.configure({ |
| 22 | + popupRenderPosition: 'right' |
| 23 | +}); |
| 24 | +*/ |
| 25 | + |
| 26 | +var app = express(); |
| 27 | +app.use(miniprofiler.profile()); |
| 28 | +app.use(miniprofilerPG(pg)); |
| 29 | + |
| 30 | +app.set('view engine', 'pug'); |
| 31 | +app.set('views', './examples/views') |
| 32 | + |
| 33 | +app.get('/', function(req, res) { |
| 34 | + pg.connect(connString, function(err, client, done) { |
| 35 | + client.query('SELECT $1::int AS number', ['1'], function(err, result) { |
| 36 | + done(); |
| 37 | + res.render('home'); |
| 38 | + }); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +app.get('/multi-query', function(req, res) { |
| 43 | + req.miniprofiler.step('Step 1', function() { |
| 44 | + req.miniprofiler.step('Step 2', function() { |
| 45 | + req.miniprofiler.timeQuery('sql', 'SELECT * FROM TEST', function() { |
| 46 | + req.miniprofiler.timeQuery('sql', 'SELECT * FROM TEST', function() { |
| 47 | + res.render('multi-query'); |
| 48 | + }); |
| 49 | + }); |
| 50 | + }); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +app.listen(8080); |
0 commit comments