Skip to content

Commit 334d204

Browse files
committed
feat(transact-update): add updateAttribute equivalent to UpdateRequest
1 parent b384b14 commit 334d204

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
import { UpdateModel } from '../../../test/models'
22
import { Metadata, metadataForClass } from '../../decorator/metadata'
33
import { createKeyAttributes } from '../../mapper'
4-
import { update } from '../expression/logical-operator/update.function'
4+
import { update2 } from '../expression/logical-operator/update.function'
55
import { getTableName } from '../get-table-name.function'
66
import { TransactUpdate } from './transact-update'
77

88
describe('TransactUpdate', () => {
99
let op: TransactUpdate<UpdateModel>
1010
let metadata: Metadata<UpdateModel>
11+
let now: Date
1112
beforeEach(() => {
1213
op = new TransactUpdate(UpdateModel, 'myId')
14+
now = new Date()
1315
metadata = metadataForClass(UpdateModel)
1416
})
1517

16-
it('correct transactItem', () => {
17-
const now = new Date()
18-
op.operations(update<UpdateModel>('lastUpdated').set(now))
19-
op.onlyIfAttribute('name').eq('Foo Bar')
20-
18+
afterEach(() => {
2119
expect(op.transactItem).toEqual({
2220
Update: {
2321
TableName: getTableName(UpdateModel),
2422
Key: createKeyAttributes(metadata, 'myId'),
2523

26-
UpdateExpression: 'SET #lastUpdated = :lastUpdated',
24+
UpdateExpression: 'SET #lastUpdated = if_not_exists(#lastUpdated, :lastUpdated)',
2725
ConditionExpression: '#name = :name',
2826

2927
ExpressionAttributeNames: {
@@ -37,4 +35,17 @@ describe('TransactUpdate', () => {
3735
},
3836
})
3937
})
38+
39+
it('correct transactItem [operations]', () => {
40+
op.operations(update2(UpdateModel, 'lastUpdated').set(now, true))
41+
.onlyIfAttribute('name')
42+
.eq('Foo Bar')
43+
})
44+
45+
it('correct transactItem [updateAttribute]', () => {
46+
op.updateAttribute('lastUpdated')
47+
.set(now, true)
48+
.onlyIfAttribute('name')
49+
.eq('Foo Bar')
50+
})
4051
})

src/dynamo/transactwrite/transact-update.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import * as DynamoDB from 'aws-sdk/clients/dynamodb'
22
import { createKeyAttributes } from '../../mapper'
33
import { ModelConstructor } from '../../model'
44
import { prepareAndAddUpdateExpressions } from '../expression/prepare-and-add-update-expressions.function'
5-
import { UpdateExpressionDefinitionFunction } from '../expression/type'
5+
import { addUpdate } from '../expression/request-expression-builder'
6+
import { RequestUpdateFunction, UpdateExpressionDefinitionFunction } from '../expression/type'
67
import { TransactBaseOperation } from './transact-base-operation'
78

89
export class TransactUpdate<T> extends TransactBaseOperation<T, DynamoDB.Update, TransactUpdate<T>> {
@@ -11,6 +12,10 @@ export class TransactUpdate<T> extends TransactBaseOperation<T, DynamoDB.Update,
1112
this.params.Key = createKeyAttributes(this.metadata, partitionKey, sortKey)
1213
}
1314

15+
updateAttribute<K extends keyof T>(attributePath: K): RequestUpdateFunction<TransactUpdate<T>, T, K> {
16+
return addUpdate(attributePath, this, this.metadata)
17+
}
18+
1419
operations(...updateDefFns: UpdateExpressionDefinitionFunction[]): TransactUpdate<T> {
1520
prepareAndAddUpdateExpressions(this.metadata, this.params, updateDefFns)
1621
return this

0 commit comments

Comments
 (0)