Skip to content

Commit 18eb1c1

Browse files
committed
Add db factory
1 parent d5d3ec3 commit 18eb1c1

File tree

5 files changed

+27
-40
lines changed

5 files changed

+27
-40
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ npm start
5151
- ~~display map~~
5252
- ~~db connection~~
5353
- ~~web interface - Angular~~
54-
- db module
54+
- ~~db module~~
5555
- zoom, pan to detail
5656

Diff for: db-config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
var pgp = require('pg-promise')();
2+
var conString = "postgres://postgres@localhost:5432/pdt-gis";
3+
var db = pgp(conString);
4+
15
module.exports = {
2-
conString: "postgres://postgres@localhost:5432/pdt-gis"
6+
db: db
37
};

Diff for: package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "perfect-school",
3-
"version": "0.0.0",
3+
"version": "0.0.1",
44
"private": true,
55
"scripts": {
66
"pretest": "jshint routes",
@@ -13,6 +13,8 @@
1313
"express": "^4.13.3",
1414
"jade": "~1.11.0",
1515
"morgan": "~1.6.1",
16+
"pg": "^4.4.3",
17+
"pg-promise": "^2.3.1",
1618
"serve-favicon": "~2.3.0"
1719
},
1820
"jshintConfig": {

Diff for: routes/api.js

+10-22
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
1-
var pg = require('pg');
2-
var config = require('../db-config');
3-
1+
var dbFactory = require('../db-config');
42

53
exports.identify = function(req, res){
6-
console.log("Getting nearest school");
7-
pg.connect(config.conString, function(err, client, done) {
8-
9-
if (err) {
10-
return console.error('error fetching client from pool', err);
11-
}
12-
var query = "SELECT *, st_asgeojson(way) as geojson FROM schools s " +
13-
"WHERE st_dwithin(ST_SetSRID(ST_MakePoint("+req.query.lon+","+req.query.lat+"),4326)::geography,s.way::geography, 1000)";
14-
console.log("Query is:", query);
15-
client.query(query, function(err, result) {
16-
done();
17-
if (err) {
18-
return console.error('error running query', err);
19-
}
20-
console.log(result.rows);
21-
res.send(result.rows);
22-
});
23-
24-
});
4+
console.log("Getting nearest schools");
5+
var query = "SELECT *, st_asgeojson(way) as geojson FROM schools s WHERE st_dwithin(ST_SetSRID(ST_MakePoint($1, $2),4326)::geography,s.way::geography, 1000)";
6+
return dbFactory.db.query(query, [req.query.lon, req.query.lat ]).then(function(results){
7+
return res.send(results);
8+
}).catch(function (e) {
9+
return res.status(500, {
10+
error: e
11+
});
12+
});
2513
};

Diff for: routes/schools.js

+8-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
var express = require('express');
2-
var pg = require('pg');
32
var router = express.Router();
43

5-
var config = require('../db-config');
4+
var dbFactory = require('../db-config');
5+
66

77
/* GET schools listing. */
88
router.get('/', function(req, res, next) {
@@ -14,20 +14,13 @@ router.get('/:id', getSchool);
1414

1515
function getSchool(req, res, next){
1616
console.log("Getting school");
17-
pg.connect(config.conString, function(err, client, done) {
18-
19-
if (err) {
20-
return console.error('error fetching client from pool', err);
21-
}
22-
client.query("SELECT *, st_asgeojson(way) as geojson FROM schools WHERE urn='"+req.params.id+"'", function(err, result) {
23-
done();
24-
if (err) {
25-
return console.error('error running query', err);
26-
}
27-
console.log(result.rows);
28-
res.send(result.rows[0]);
17+
var query = "SELECT *, st_asgeojson(way) as geojson FROM schools WHERE urn=$1";
18+
return dbFactory.db.one(query, req.params.id).then(function(results){
19+
return res.send(results);
20+
}).catch(function (e) {
21+
return res.status(500, {
22+
error: e
2923
});
30-
3124
});
3225
}
3326

0 commit comments

Comments
 (0)