1
- import Promise from 'bluebird' ;
2
- import pg from 'pg' ;
1
+ const Promise = require ( 'bluebird' )
2
+ const pg = require ( 'pg' )
3
3
4
4
/**
5
5
* @class postgres
6
6
*/
7
- export default class {
7
+ module . exports = class {
8
8
/**
9
9
* Creates the connection string for the instance
10
10
* @param {Object } config
11
- * @property {String } config.host The host to connect to
12
- * @property {String } config.username The connection username
13
- * @property {String } config.password The connection password
14
- * @property {String } config.database The connection database
11
+ * @param {String } config.host The host to connect to
12
+ * @param {String } config.user The connection username
13
+ * @param {String } config.password The connection password
14
+ * @param {String } config.database The connection database
15
15
*/
16
16
constructor ( config ) {
17
- this . connStr = `postgres:// ${ config . username } : ${ config . password } @ ${ config . host } / ${ config . database } ` ;
17
+ this . pg = new pg . Client ( config )
18
18
}
19
19
20
20
/**
@@ -23,24 +23,18 @@ export default class {
23
23
* @returns {Object } promise
24
24
*/
25
25
query ( query ) {
26
- return new Promise ( ( resolve , reject ) => {
27
- pg . connect ( this . connStr , ( err , client , done ) => {
28
- if ( err ) {
29
- reject ( err ) ;
30
- } else {
31
- client . query ( query , ( e , result ) => {
32
- // Release client
33
- done ( ) ;
34
- // Process
35
- if ( e ) {
36
- reject ( e ) ;
37
- } else {
38
- resolve ( result ) ;
39
- }
40
- } ) ;
41
- }
42
- } ) ;
43
- } ) ;
26
+ let res
27
+ return this . pg . connect ( )
28
+ . then ( ( ) => this . pg . query ( query ) )
29
+ . then ( data => {
30
+ res = data
31
+ return this . pg . end ( )
32
+ } )
33
+ . then ( ( ) => res )
34
+ . catch ( ( err ) => {
35
+ return this . pg . end ( )
36
+ . then ( ( ) => { throw err } )
37
+ } )
44
38
}
45
39
46
40
/**
@@ -51,17 +45,17 @@ export default class {
51
45
*/
52
46
createTable ( props ) {
53
47
// Build query
54
- const len = Object . keys ( props ) . length ;
55
- let i = 1 ;
56
- let query = `CREATE TABLE IF NOT EXISTS ${ this . tableName } (` ;
48
+ const len = Object . keys ( props ) . length
49
+ let i = 1
50
+ let query = `CREATE TABLE IF NOT EXISTS ${ this . tableName } (`
57
51
for ( let prop in props ) {
58
- let comma = ( i !== len ) ? ', ' : '' ;
59
- query += `${ prop } ${ props [ prop ] . join ( ' ' ) } ${ comma } ` ;
60
- i ++ ;
52
+ let comma = ( i !== len ) ? ', ' : ''
53
+ query += `${ prop } ${ props [ prop ] . join ( ' ' ) } ${ comma } `
54
+ i ++
61
55
}
62
- query += ');' ;
56
+ query += ')'
63
57
// Run query
64
- return this . query ( query ) ;
58
+ return this . query ( query )
65
59
}
66
60
67
61
/**
@@ -71,24 +65,17 @@ export default class {
71
65
* @returns {Object } promise
72
66
*/
73
67
create ( body , version = false ) {
74
- return new Promise ( ( resolve , reject ) => {
75
- // Validate
76
- const validationErrors = this . validate ( body , version ) ;
77
- if ( validationErrors ) {
78
- reject ( validationErrors ) ;
79
- } else {
80
- // Build query
81
- let cols = [ ] ;
82
- let vals = [ ] ;
83
- for ( let prop in body ) {
84
- cols . push ( prop ) ;
85
- vals . push ( '\'' + body [ prop ] + '\'' ) ;
86
- }
87
- const query = `INSERT INTO ${ this . tableName } (${ cols . join ( ',' ) } ) VALUES (${ vals . join ( ',' ) } );` ;
88
- // Run query
89
- resolve ( this . query ( query ) ) ;
90
- }
91
- } ) ;
68
+ return this . validate ( body )
69
+ . then ( data => {
70
+ const vals = [ ]
71
+ const cols = Object . keys ( data ) . reduce ( ( acc , key ) => {
72
+ vals . push ( `'${ data [ key ] } '` )
73
+ acc . push ( key )
74
+ return acc
75
+ } , [ ] )
76
+ const query = `INSERT INTO ${ this . tableName } (${ cols . join ( ',' ) } ) VALUES (${ vals . join ( ',' ) } )`
77
+ return this . query ( query )
78
+ } )
92
79
}
93
80
94
81
/**
@@ -97,25 +84,13 @@ export default class {
97
84
* @returns {Object } promise
98
85
*/
99
86
read ( query , version = false ) {
100
- let where ;
101
- if ( query ) {
102
- where = ` WHERE ${ query } ` ;
103
- } else {
104
- where = '' ;
105
- }
106
- return new Promise ( ( resolve , reject ) => {
107
- return this . query ( `SELECT * FROM ${ this . tableName } ${ where } ` )
108
- . then ( ( results ) => {
109
- let tmp = [ ] ;
110
- results . rows . forEach ( ( r ) => {
111
- tmp . push ( this . sanitize ( r , version ) ) ;
112
- } ) ;
113
- resolve ( tmp ) ;
87
+ const where = query ? ` WHERE ${ query } ` : ''
88
+ return this . query ( `SELECT * FROM ${ this . tableName } ${ where } ` )
89
+ . then ( ( results ) => {
90
+ return results . rows . map ( ( r ) => {
91
+ return this . sanitize ? this . sanitize ( r , version ) : r
114
92
} )
115
- . catch ( ( err ) => {
116
- reject ( err ) ;
117
- } ) ;
118
- } ) ;
93
+ } )
119
94
}
120
95
121
96
/**
@@ -126,24 +101,20 @@ export default class {
126
101
* @returns {Object } promise
127
102
*/
128
103
update ( query , body , version = false ) {
129
- return new Promise ( ( resolve , reject ) => {
130
- const validationErrors = this . validate ( body , version ) ;
131
- if ( validationErrors ) {
132
- reject ( validationErrors ) ;
133
- } else {
134
- let i = 1 ;
135
- let changes = '' ;
136
- let len = Object . keys ( body ) . length ;
104
+ return this . validate ( body )
105
+ . then ( data => {
106
+ let i = 1
107
+ let changes = ''
108
+ let len = Object . keys ( body ) . length
137
109
for ( let prop in body ) {
138
110
if ( { } . hasOwnProperty . call ( body , prop ) ) {
139
- let comma = ( i !== len ) ? ', ' : '' ;
140
- changes += `${ prop } ='${ body [ prop ] } '${ comma } ` ;
141
- i ++ ;
111
+ let comma = ( i !== len ) ? ', ' : ''
112
+ changes += `${ prop } ='${ body [ prop ] } '${ comma } `
113
+ i ++
142
114
}
143
115
}
144
- resolve ( this . query ( `UPDATE ${ this . tableName } SET ${ changes } WHERE ${ query } ` ) ) ;
145
- }
146
- } ) ;
116
+ return this . query ( `UPDATE ${ this . tableName } SET ${ changes } WHERE ${ query } ` )
117
+ } )
147
118
}
148
119
149
120
/**
@@ -152,7 +123,7 @@ export default class {
152
123
* @returns {Object } promise
153
124
*/
154
125
delete ( query ) {
155
- return this . query ( `DELETE FROM ${ this . tableName } WHERE ${ query } ` ) ;
126
+ return this . query ( `DELETE FROM ${ this . tableName } WHERE ${ query } ` )
156
127
}
157
128
158
129
/**
@@ -161,7 +132,7 @@ export default class {
161
132
* @param {Function } fn The function to extend on the object
162
133
*/
163
134
extend ( name , fn ) {
164
- this [ name ] = fn . bind ( this ) ;
135
+ this [ name ] = fn . bind ( this )
165
136
}
166
137
167
138
}
0 commit comments