Skip to content

Commit

Permalink
fix(conditionExpression): change how the operands are given for IN op…
Browse files Browse the repository at this point in the history
…eration (single values instead
  • Loading branch information
michaelwittwer committed Oct 12, 2017
1 parent e6d0d3e commit 1d5c087
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
11 changes: 4 additions & 7 deletions src/dynamo/expression/condition-expression-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,18 +270,15 @@ describe('expressions', () => {
const condition = ConditionExpressionBuilder.buildFilterExpression(
'myCollection',
'IN',
[['myCollection', 'myOtherValue']],
[['myValue', 'myOtherValue']],
undefined,
undefined
)
expect(condition.statement).toBe('#myCollection IN (:myCollection)')
expect(condition.statement).toBe('#myCollection IN (:myCollection_0, :myCollection_1)')
expect(condition.attributeNames).toEqual({ '#myCollection': 'myCollection' })
// TODO review not sure if the attribute should be L(ist) or S(et) or if both are supported, they both executed successfuly against dynamodb when testing but with unexpected result
// expect(condition.attributeValues).toEqual({
// ':myCollection': { L: [{ S: 'myCollection' }, { S: 'myOtherValue' }] },
// })
expect(condition.attributeValues).toEqual({
':myCollection': { SS: ['myCollection', 'myOtherValue'] },
':myCollection_0': { S: 'myValue' },
':myCollection_1': { S: 'myOtherValue' },
})
})

Expand Down
19 changes: 15 additions & 4 deletions src/dynamo/expression/condition-expression-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,20 @@ export class ConditionExpressionBuilder {
existingValueNames: string[] | undefined,
propertyMetadata: PropertyMetadata<any> | undefined
): Expression {
const mappedValues = Mapper.toDbOne(values[0], propertyMetadata)
const attributeValues: AttributeMap = (<any[]>values[0])
.map(value => Mapper.toDbOne(value, propertyMetadata))
.reduce(
(result, mappedValue: AttributeValue, index: number) => {
result[`${valuePlaceholder}_${index}`] = mappedValue
return result
},
<AttributeMap>{}
)

const attributeValues: AttributeMap = {}
attributeValues[valuePlaceholder] = <any>mappedValues
const inStatement = (<any[]>values[0]).map((value: any, index: number) => `${valuePlaceholder}_${index}`).join(', ')

return {
statement: `${namePlaceholder} IN (${Object.keys(attributeValues)})`,
statement: `${namePlaceholder} IN (${inStatement})`,
attributeNames,
attributeValues,
}
Expand Down Expand Up @@ -283,6 +290,10 @@ export class ConditionExpressionBuilder {
)
}
break
case 'IN':
if (!Array.isArray(values[0])) {
throw new Error('the provided value for IN operator must be an array')
}
}
}
}
Expand Down

0 comments on commit 1d5c087

Please sign in to comment.