Skip to content

Commit

Permalink
add count suffix to newList to update expression
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-german committed Oct 19, 2023
1 parent 09cb397 commit b1361d1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@parana-games/tinymo",
"version": "2.0.5",
"version": "2.0.6",
"description": "simple DynamoDB DocumentClient wrapper",
"types": "./dist-types/index.d.ts",
"main": "./dist/index.js",
Expand Down
4 changes: 2 additions & 2 deletions src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export class Update extends Write {
const flatArray = [value].flat()

if (createIfNotExists) {
this.setBuilder.addValue(`newList`, [])
this.setBuilder.pair(listName, flatArray, (n, v) => `${n} = list_append(if_not_exists(${n}, :newList), ${v})`)
const valueToken = this.setBuilder.addValue(`newList`, [])
this.setBuilder.pair(listName, flatArray, (n, v) => `${n} = list_append(if_not_exists(${n}, ${valueToken}), ${v})`)
} else {
this.setBuilder.pair(listName, flatArray, (n, v) => `${n} = list_append(${n}, ${v})`)
}
Expand Down
18 changes: 18 additions & 0 deletions test/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,22 @@ describe('update', () => {
ConditionExpression: "#a = :aEqualsConditionValue AND #a = :aEqualsConditionValue2 AND #a = :aEqualsConditionValue3"
})
})

it('update with 2 empty lists', () => {
const update = new Update('table1850', { PK: 'duplicates' })
update.addToList('a', [1, 2, 3])
update.addToList('b', [4, 5, 6])
expect(update.build()).toStrictEqual({
"TableName": "table1850",
"Key": { "PK": "duplicates" },
"UpdateExpression": "SET #a = list_append(if_not_exists(#a, :newList), :a), #b = list_append(if_not_exists(#b, :newList2), :b)",
"ExpressionAttributeNames": { "#a": "a", "#b": "b" },
"ExpressionAttributeValues": {
":newList": [],
":a": [ 1, 2, 3 ],
":newList2": [],
":b": [ 4, 5, 6 ]
}
})
})
})

0 comments on commit b1361d1

Please sign in to comment.