-
Notifications
You must be signed in to change notification settings - Fork 1
/
postgisDao.js
62 lines (53 loc) · 1.44 KB
/
postgisDao.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
59
60
61
62
/**
* Access postgis db
**/
var config = require('config');
var pg = require('pg');
var utils = require('./utils')
var conString = config.get('gisDb.connectionString');
function getQuerySize(query) {
return explainQuery(query)
.then(function(queryMeta) {
return queryMeta.rows[0]["QUERY PLAN"][0].Plan["Plan Rows"]
}).catch(utils.passRejectedPromise)
}
function explainQuery(query) {
var expQuery = "EXPLAIN (FORMAT JSON) " + query;
return executeQuery(expQuery);
}
function schemaInfo(){
var query = "SELECT column_name, data_type, udt_name, table_name"
+ " FROM information_schema.columns"
+ " WHERE table_schema = 'public'";
return executeQuery(query);
}
function executeQuery(query) {
return new Promise(function(resolve, reject) {
pg.connect(conString, function(error, client, done) {
if (error) reject(error);
client.query(query, [],
function(error, result) {
done();
if (error) reject(error);
result = _colnamesIfNoRows(result);
resolve(result);
});
});
});
}
function _colnamesIfNoRows(result){
if(!result || result.rows.length > 0){
return result;
}
result = [result.fields.reduce(function(prev, curr){
prev[curr["name"]] = "";
return prev;
}, {})]
return result
}
module.exports = {
getQuerySize: getQuerySize,
explainQuery: explainQuery,
executeQuery: executeQuery,
schemaInfo: schemaInfo
}