Skip to content

Commit

Permalink
Merge pull request #201 from shiftcode/#200-array-position-in-attribu…
Browse files Browse the repository at this point in the history
…te-path

fix(update-expression-builder): index in attribute path
  • Loading branch information
michaelwittwer authored Mar 27, 2019
2 parents 2dd0428 + 7267f5d commit 9eb6569
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
20 changes: 20 additions & 0 deletions src/dynamo/expression/update-expression-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ describe('buildUpdateExpression', () => {
})
})

it('should build expression for number at document path ', () => {
const exp = buildUpdateExpression('numberValues[0]', op, [23], [], metaDataU)
expect(exp).toEqual({
attributeNames: { '#numberValues': 'numberValues' },
attributeValues: { ':numberValues_at_0': { N: '23' } },
statement: '#numberValues[0] = #numberValues[0] + :numberValues_at_0',
type: 'SET',
})
})

it('should throw when not number', () => {
expect(() => buildUpdateExpression('age', op, ['notANumber'], [], metaDataS)).toThrow()
})
Expand All @@ -43,6 +53,16 @@ describe('buildUpdateExpression', () => {
})
})

it('should build expression for number at document path ', () => {
const exp = buildUpdateExpression('numberValues[0]', op, [23], [], metaDataU)
expect(exp).toEqual({
attributeNames: { '#numberValues': 'numberValues' },
attributeValues: { ':numberValues_at_0': { N: '23' } },
statement: '#numberValues[0] = #numberValues[0] - :numberValues_at_0',
type: 'SET',
})
})

it('should throw when not number', () => {
expect(() => buildUpdateExpression('age', op, ['notANumber'], [], metaDataS)).toThrow()
})
Expand Down
6 changes: 4 additions & 2 deletions src/dynamo/expression/update-expression-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ function buildDefaultExpression(

// special case: [same as in buildDefaultConditionExpression]
// we have the metadata for an Array/Set of an Object,
// but only get a single item when using `attribute('myCollectionProp[0]').set({})`
if (operator.action === 'set' && /\[\d+\]$/.test(attributePath)) {
// but only get a single item when using list indexes in attributePath
// e.g. `attribute('myCollectionProp[0]').set(...)`
// (not exclusive to `.set(...)` but all updateActions)
if (/\[\d+\]$/.test(attributePath)) {
attribute = toDbOne(values[0], getPropertyPath(propertyMetadata, attributePath), alterCollectionPropertyMetadataForSingleItem(propertyMetadata))
} else {
attribute = toDbOne(values[0], propertyMetadata)
Expand Down
12 changes: 11 additions & 1 deletion src/dynamo/request/update/update.request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('update request', () => {
expect(req.params.ReturnValues).toEqual('UPDATED_OLD')
})

it('should add operations', () => {
it('should add operation 1', () => {
const now = new Date()
const request = new UpdateRequest(<any>null, UpdateModel, 'myId')
request.operations(update<UpdateModel>('lastUpdated').set(now))
Expand All @@ -64,6 +64,16 @@ describe('update request', () => {
':lastUpdated': { S: now.toISOString() },
})
})
it('should add operation 2', () => {
const request = new UpdateRequest(<any>null, UpdateModel, 'myId')
request.operations(update('numberValues[1]').incrementBy(42))

expect(request.params.UpdateExpression).toBe('SET #numberValues[1] = #numberValues[1] + :numberValues_at_1')
expect(request.params.ExpressionAttributeNames).toEqual({ '#numberValues': 'numberValues' })
expect(request.params.ExpressionAttributeValues).toEqual({
':numberValues_at_1': { N: '42' },
})
})

it('should allow to add multiple operations', () => {
const request = new UpdateRequest(<any>null, UpdateModel, 'myId').operations(
Expand Down

0 comments on commit 9eb6569

Please sign in to comment.