Skip to content

Commit

Permalink
add isEmpty to update
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-german committed Sep 21, 2023
1 parent 9c4daa9 commit 84f50ab
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class Update extends Write {
async run(): Promise<UpdateCommandOutput> { return await this._client!.send(new UpdateCommand(this.build())) }
transactWriteItem(): TransactWriteItem { return { Update: this.build() as any as TransactUpdate } }

isEmpty(): boolean { return this.setBuilder.isEmpty() && this.addBuilder.isEmpty() && this.removeBuilder.isEmpty() && this.deleteBuilder.isEmpty() }
add(key: string, value: any): Update { this.addBuilder.pair(key, value, (n, v) => { return `${n} ${v}` }); return this; }
subtract(key: string, value: any): Update { this.setBuilder.pair(key, value, (n, v) => { return `${n} = ${n} - ${v}` }); return this; }

Expand Down
8 changes: 7 additions & 1 deletion test/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ describe('update', () => {
const update = new Update('table1850', { PK: 'big', SK: 'boi' })

it('build with just one set', () => {
expect(update.isEmpty()).toBe(true)
update.set('a', 1)
expect(update.isEmpty()).toBe(false)
const built = update.build()

expect(built).toEqual({
Expand Down Expand Up @@ -196,9 +198,13 @@ describe('update', () => {
UpdateExpression: 'SET #autonomy = :autonomy'
}

const update2 = new Update('90zcjx', { PK: 'catch', SK: 33 }).set('autonomy', false).condition('autonomy', '=', true)
const update2 = new Update('90zcjx', { PK: 'catch', SK: 33 })
update2.returnItemCollectionMetrics = 'NONE'

expect(update2.isEmpty()).toBe(true)
update2.set('autonomy', false).condition('autonomy', '=', true)
expect(update2.isEmpty()).toBe(false)

const expected2 = {
ConditionExpression: '#autonomy = :autonomyEqualsConditionValue',
ExpressionAttributeNames: { '#autonomy': 'autonomy' },
Expand Down

0 comments on commit 84f50ab

Please sign in to comment.