diff --git a/src/update.ts b/src/update.ts index f339963..cd5d715 100644 --- a/src/update.ts +++ b/src/update.ts @@ -14,6 +14,7 @@ export class Update extends Write { async run(): Promise { 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; } diff --git a/test/update.test.ts b/test/update.test.ts index fe003e3..673e2cb 100644 --- a/test/update.test.ts +++ b/test/update.test.ts @@ -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({ @@ -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' },