Skip to content

Commit 2cdb7e4

Browse files
author
Guilherme Oenning
committed
better eslint rules and provider for pg
1 parent 3952acc commit 2cdb7e4

File tree

6 files changed

+56
-56
lines changed

6 files changed

+56
-56
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
underscore-min.js
2+
ui

.eslintrc.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "eslint:recommended",
3+
"env": {
4+
"node": true
5+
},
6+
"rules": {
7+
"semi": [2, "always"]
8+
}
9+
}

examples/express/server.js

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,30 @@
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-
}
1+
var miniprofiler = require('../../miniprofiler.js')
2+
var pg = require('pg')
153

16-
var express = require('express');
17-
var connString = "postgres://postgres:postgres@localhost/async_demo";
4+
var express = require('express')
5+
var connString = 'postgres://postgres:postgres@localhost/async_demo'
186

19-
// Defaults to 'left'. Uncomment this to move to right. Also supports 'bottomLeft', 'bottomRight'.
20-
/*
21-
miniprofiler.configure({
22-
popupRenderPosition: 'right'
23-
});
24-
*/
7+
var app = express()
8+
app.use(miniprofiler.profile())
9+
app.use(miniprofiler.for.pg(pg))
2510

26-
var app = express();
27-
app.use(miniprofiler.profile());
28-
app.use(miniprofilerPG(pg));
29-
30-
app.set('view engine', 'pug');
11+
app.set('view engine', 'pug')
3112
app.set('views', './examples/views')
3213

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-
});
14+
app.get('/', function (req, res) {
15+
res.render('home')
16+
})
4117

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-
});
18+
app.get('/multi-query', function (req, res) {
19+
pg.connect(connString, function (err, client, done) {
20+
client.query('SELECT pg_sleep(1)', [], function (err, result) {
21+
client.query('SELECT $1::int AS number', ['2'], function (err, result) {
22+
console.log(result)
23+
done()
24+
res.render('multi-query')
25+
})
26+
})
27+
})
28+
})
5329

54-
app.listen(8080);
30+
app.listen(8080)

miniprofiler.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ _.templateSettings = {
2222
exports.configure = configure;
2323
exports.instrument = addProfilingInstrumentation;
2424
exports.profile = middleware;
25+
exports.for = {
26+
pg: require('./providers/miniprofiler.pg.js')
27+
}
2528

2629
// GLOBALS
2730
var storage = function(id, json) {
@@ -76,10 +79,8 @@ function static(reqPath, res) {
7679
});
7780
}
7881

79-
var util = require('util');
80-
8182
function results(req, res) {
82-
proc = function(post) {
83+
var proc = function(post) {
8384
// todo: store client timings
8485
var id = post.id || url.parse(req.url, true).query.id;
8586
var s = storage(id);
@@ -212,10 +213,6 @@ function configure(options){
212213
configured = true;
213214
}
214215

215-
function getProfiling(id){
216-
return storage(id);
217-
}
218-
219216
/*
220217
* Modifies `toInstrument` such that each function has been instrumented for miniprofiler purposes.
221218
*

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "A simple but effective mini-profiler.",
55
"main": "miniprofiler.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"lint": "eslint ."
88
},
99
"repository": {
1010
"type": "git",
@@ -14,8 +14,11 @@
1414
"license": "Apache-2.0",
1515
"readmeFilename": "README.md",
1616
"devDependencies": {
17+
"eslint": "^2.9.0",
1718
"express": "^4.13.4",
19+
"mocha": "^2.4.5",
1820
"pg": "^4.5.5",
19-
"pug": "^2.0.0-alpha6"
21+
"pug": "^2.0.0-alpha6",
22+
"redis": "^2.6.0-2"
2023
}
2124
}

providers/miniprofiler.pg.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var pgQuery;
2+
3+
module.exports = function (pg) {
4+
pgQuery = pgQuery || pg.Client.prototype.query
5+
return function (req, res, next) {
6+
if (req.miniprofiler.enabled) {
7+
pg.Client.query = function (config, values, callback) {
8+
req.miniprofiler.timeQuery('sql', config.toString(), pgQuery.bind(this), config, values, callback)
9+
}
10+
}
11+
next()
12+
}
13+
}

0 commit comments

Comments
 (0)