Skip to content

Commit

Permalink
fix bug in updates with extra fields
Browse files Browse the repository at this point in the history
  • Loading branch information
gr0uch committed Oct 13, 2018
1 parent e2c21f9 commit 6d74caa
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog


##### 1.6.8 (2018-10-13)
- Fix: do not allow extraneous fields in update to cause silent error.


##### 1.6.7 (2018-09-26)
- Polish: make authorization parameters in connection URL optional.

Expand Down
6 changes: 6 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ module.exports = Adapter => class PostgreSQLAdapter extends Adapter {
const client = this.client
const typeMap = this.options.typeMap
const primaryKey = this.keys.primary
const recordTypes = this.recordTypes

// This is a little bit wrong, it is only safe to update within a
// transaction. It's not possible to put it all in one update statement,
Expand All @@ -451,6 +452,7 @@ module.exports = Adapter => class PostgreSQLAdapter extends Adapter {
let set = []

for (const field in update.replace) {
if (!(field in recordTypes[type])) continue
const value = update.replace[field]
index++
if (Array.isArray(value)) parameters.push(value.map(inputValue))
Expand All @@ -459,6 +461,7 @@ module.exports = Adapter => class PostgreSQLAdapter extends Adapter {
}

for (const field in update.push) {
if (!(field in recordTypes[type])) continue
const value = update.push[field]
index++

Expand All @@ -473,6 +476,7 @@ module.exports = Adapter => class PostgreSQLAdapter extends Adapter {
}

for (const field in update.pull) {
if (!(field in recordTypes[type])) continue
const value = update.pull[field]

if (Array.isArray(value)) {
Expand All @@ -489,6 +493,8 @@ module.exports = Adapter => class PostgreSQLAdapter extends Adapter {
set.push(`"${field}" = array_remove("${field}", $${index})`)
}

// When no fields are to be updated, it's a no-op.
if (!set.length) return resolve(0)

set = `set ${set.join(', ')}`

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fortune-postgres",
"description": "Postgres adapter for Fortune.",
"version": "1.6.7",
"version": "1.6.8",
"license": "MIT",
"repository": {
"type": "git",
Expand Down

0 comments on commit 6d74caa

Please sign in to comment.