Skip to content

Commit da2b266

Browse files
authored
Merge pull request #9 from node-modli/feat/schema
feat(index): add optional schemaName support
2 parents c15a9f1 + 899df1f commit da2b266

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ module.exports = class {
1616
this.pg = new Pool(config)
1717
}
1818

19+
getTable () {
20+
return this.schemaName ? `"${this.schemaName}"."${this.tableName}"` : this.tableName
21+
}
22+
1923
/**
2024
* Runs query against instance conn
2125
* @param {String} query The query to execute
@@ -48,7 +52,7 @@ module.exports = class {
4852
// Build query
4953
const len = Object.keys(props).length
5054
let i = 1
51-
let query = `CREATE TABLE IF NOT EXISTS ${this.tableName} (`
55+
let query = `CREATE TABLE IF NOT EXISTS ${this.getTable()} (`
5256
for (let prop in props) {
5357
let comma = (i !== len) ? ', ' : ''
5458
query += `${prop} ${props[prop].join(' ')}${comma}`
@@ -78,7 +82,7 @@ module.exports = class {
7882
acc.push(key)
7983
return acc
8084
}, [])
81-
const query = `INSERT INTO ${this.tableName} (${cols.join(',')}) VALUES (${vals.join(',')})`
85+
const query = `INSERT INTO ${this.getTable()} (${cols.join(',')}) VALUES (${vals.join(',')})`
8286
return this.query(query)
8387
.then(data => {
8488
if (data.rowCount) return res
@@ -94,7 +98,7 @@ module.exports = class {
9498
*/
9599
read (query, version = false) {
96100
const where = query ? ` WHERE ${query}` : ''
97-
return this.query(`SELECT * FROM ${this.tableName}${where}`)
101+
return this.query(`SELECT * FROM ${this.getTable()}${where}`)
98102
.then((results) => {
99103
return results.rows.map((r) => {
100104
return this.sanitize ? this.sanitize(r, version) : r
@@ -128,7 +132,7 @@ module.exports = class {
128132
i++
129133
}
130134
}
131-
return this.query(`UPDATE ${this.tableName} SET ${changes} WHERE ${query}`)
135+
return this.query(`UPDATE ${this.getTable()} SET ${changes} WHERE ${query}`)
132136
.then(data => {
133137
if (data.rowCount) return res
134138
throw new Error('Unable to update record(s)')
@@ -142,7 +146,7 @@ module.exports = class {
142146
* @returns {Object} promise
143147
*/
144148
delete (query) {
145-
return this.query(`DELETE FROM ${this.tableName} WHERE ${query}`)
149+
return this.query(`DELETE FROM ${this.getTable()} WHERE ${query}`)
146150
}
147151

148152
/**

test/src/index.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ describe('postgres', () => {
2525
return expect(inst.query('`')).to.be.rejectedWith(Error)
2626
})
2727
})
28+
describe('getTable', () => {
29+
after(() => {
30+
inst.schemaName = null
31+
})
32+
it('concats tableName with schemaName if the latter exists', () => {
33+
inst.schemaName = 'testSchema'
34+
expect(inst.getTable()).to.equal('"testSchema"."foo"')
35+
})
36+
})
2837
describe('createTable', () => {
2938
it('creates a new table based on object passed (if not exists)', () => {
3039
return inst.createTable(fixd.postgres.createTable)

0 commit comments

Comments
 (0)