-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres.js
91 lines (78 loc) · 2.51 KB
/
postgres.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Created by balazs on 2/28/2017.
*/
var pg = require('pg');
var env = require("./env").env;
// create a config to configure both pooling behavior
// and client options
// note: all config is optional and the environment variables
// will be read if the config is not present
var config = env.postgresSettings;
//this initializes a connection pool
//it will keep idle connections open for a 30 seconds
//and set a limit of maximum 10 idle clients
var pool = new pg.Pool(config);
var runq = function runq(querystring, params, cb) {
var res = null;
pool.connect(function (err, client, done) {
if (err) {
console.log(err);
}
//ide
client.query(querystring, params, function (err, result) {
if (err) {
console.log(err);
return (null);
}
res = result;
done();
cb(res);
});
});
}
// to run a query we can acquire a client from the pool,
// run a query on the client, and then return the client to the pool
/*
pool.connect(function (err, client, done) {
if (err) {
return console.error('error fetching client from pool', err);
}
client.query('SELECT $1::int AS number', ['1'], function (err, result) {
//call `done(err)` to release the client back to the pool (or destroy it if there is an error)
done(err);
if (err) {
return console.error('error running query', err);
}
console.log(result.rows[0].number);
//output: 1
});
});
pool.connect(function (err, client, done) { //2
if (err) {
return console.error('error fetching client from pool', err);
}
client.query('SELECT NAME FROM PUBLIC.testt',
function (err, result) {
//call `done(err)` to release the client back to the pool (or destroy it if there is an error)
done(err);
if (err) {
return console.error('error running query', err);
}
result.rows.forEach(function(entry) {
console.log(entry.name);
});
console.log(result.rows.length);
//output: 1
});
});
*/
pool.on('error', function (err, client) {
// if an error is encountered by a client while it sits idle in the pool
// the pool itself will emit an error event with both the error and
// the client which emitted the original error
// this is a rare occurrence but can happen if there is a network partition
// between your application and the database, the database restarts, etc.
// and so you might want to handle it and at least log it out
console.error('idle client error', err.message, err.stack)
})
module.exports.runq = runq;