@@ -16,6 +16,10 @@ module.exports = class {
16
16
this . pg = new Pool ( config )
17
17
}
18
18
19
+ getTable ( ) {
20
+ return this . schemaName ? `"${ this . schemaName } "."${ this . tableName } "` : this . tableName
21
+ }
22
+
19
23
/**
20
24
* Runs query against instance conn
21
25
* @param {String } query The query to execute
@@ -48,7 +52,7 @@ module.exports = class {
48
52
// Build query
49
53
const len = Object . keys ( props ) . length
50
54
let i = 1
51
- let query = `CREATE TABLE IF NOT EXISTS ${ this . tableName } (`
55
+ let query = `CREATE TABLE IF NOT EXISTS ${ this . getTable ( ) } (`
52
56
for ( let prop in props ) {
53
57
let comma = ( i !== len ) ? ', ' : ''
54
58
query += `${ prop } ${ props [ prop ] . join ( ' ' ) } ${ comma } `
@@ -78,7 +82,7 @@ module.exports = class {
78
82
acc . push ( key )
79
83
return acc
80
84
} , [ ] )
81
- const query = `INSERT INTO ${ this . tableName } (${ cols . join ( ',' ) } ) VALUES (${ vals . join ( ',' ) } )`
85
+ const query = `INSERT INTO ${ this . getTable ( ) } (${ cols . join ( ',' ) } ) VALUES (${ vals . join ( ',' ) } )`
82
86
return this . query ( query )
83
87
. then ( data => {
84
88
if ( data . rowCount ) return res
@@ -94,7 +98,7 @@ module.exports = class {
94
98
*/
95
99
read ( query , version = false ) {
96
100
const where = query ? ` WHERE ${ query } ` : ''
97
- return this . query ( `SELECT * FROM ${ this . tableName } ${ where } ` )
101
+ return this . query ( `SELECT * FROM ${ this . getTable ( ) } ${ where } ` )
98
102
. then ( ( results ) => {
99
103
return results . rows . map ( ( r ) => {
100
104
return this . sanitize ? this . sanitize ( r , version ) : r
@@ -128,7 +132,7 @@ module.exports = class {
128
132
i ++
129
133
}
130
134
}
131
- return this . query ( `UPDATE ${ this . tableName } SET ${ changes } WHERE ${ query } ` )
135
+ return this . query ( `UPDATE ${ this . getTable ( ) } SET ${ changes } WHERE ${ query } ` )
132
136
. then ( data => {
133
137
if ( data . rowCount ) return res
134
138
throw new Error ( 'Unable to update record(s)' )
@@ -142,7 +146,7 @@ module.exports = class {
142
146
* @returns {Object } promise
143
147
*/
144
148
delete ( query ) {
145
- return this . query ( `DELETE FROM ${ this . tableName } WHERE ${ query } ` )
149
+ return this . query ( `DELETE FROM ${ this . getTable ( ) } WHERE ${ query } ` )
146
150
}
147
151
148
152
/**
0 commit comments