Skip to content

Commit 6d74caa

Browse files
committed
fix bug in updates with extra fields
1 parent e2c21f9 commit 6d74caa

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22

33

4+
##### 1.6.8 (2018-10-13)
5+
- Fix: do not allow extraneous fields in update to cause silent error.
6+
7+
48
##### 1.6.7 (2018-09-26)
59
- Polish: make authorization parameters in connection URL optional.
610

lib/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ module.exports = Adapter => class PostgreSQLAdapter extends Adapter {
441441
const client = this.client
442442
const typeMap = this.options.typeMap
443443
const primaryKey = this.keys.primary
444+
const recordTypes = this.recordTypes
444445

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

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

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

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

475478
for (const field in update.pull) {
479+
if (!(field in recordTypes[type])) continue
476480
const value = update.pull[field]
477481

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

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

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "fortune-postgres",
33
"description": "Postgres adapter for Fortune.",
4-
"version": "1.6.7",
4+
"version": "1.6.8",
55
"license": "MIT",
66
"repository": {
77
"type": "git",

0 commit comments

Comments
 (0)