File tree Expand file tree Collapse file tree 1 file changed +16
-15
lines changed Expand file tree Collapse file tree 1 file changed +16
-15
lines changed Original file line number Diff line number Diff line change 1
- const pg = require ( 'pg' )
1
+ const { Pool } = require ( 'pg' )
2
2
3
3
/**
4
4
* @class postgres
@@ -13,7 +13,7 @@ module.exports = class {
13
13
* @param {String } config.database The connection database
14
14
*/
15
15
constructor ( config ) {
16
- this . pg = new pg . Client ( config )
16
+ this . pg = new Pool ( config )
17
17
}
18
18
19
19
/**
@@ -22,18 +22,19 @@ module.exports = class {
22
22
* @returns {Object } promise
23
23
*/
24
24
query ( query ) {
25
- let res
26
- // TODO: overlapping connections
25
+ let client
27
26
return this . pg . connect ( )
28
- . then ( ( ) => this . pg . query ( query ) )
29
- . then ( data => {
30
- res = data
31
- return this . pg . end ( )
27
+ . then ( ( cli ) => {
28
+ client = cli
29
+ return client . query ( query )
30
+ } )
31
+ . then ( res => {
32
+ client . release ( )
33
+ return res
32
34
} )
33
- . then ( ( ) => res )
34
35
. catch ( ( err ) => {
35
- return this . pg . end ( )
36
- . then ( ( ) => { throw err } )
36
+ client . release ( )
37
+ throw err
37
38
} )
38
39
}
39
40
@@ -105,11 +106,11 @@ module.exports = class {
105
106
. then ( data => {
106
107
let i = 1
107
108
let changes = ''
108
- let len = Object . keys ( body ) . length
109
- for ( let prop in body ) {
110
- if ( { } . hasOwnProperty . call ( body , prop ) ) {
109
+ let len = Object . keys ( data ) . length
110
+ for ( let prop in data ) {
111
+ if ( { } . hasOwnProperty . call ( data , prop ) ) {
111
112
let comma = ( i !== len ) ? ', ' : ''
112
- changes += `${ prop } ='${ body [ prop ] } '${ comma } `
113
+ changes += `${ prop } ='${ data [ prop ] } '${ comma } `
113
114
i ++
114
115
}
115
116
}
You can’t perform that action at this time.
0 commit comments