Skip to content

Commit 1d5c087

Browse files
fix(conditionExpression): change how the operands are given for IN operation (single values instead
1 parent e6d0d3e commit 1d5c087

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/dynamo/expression/condition-expression-builder.spec.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,18 +270,15 @@ describe('expressions', () => {
270270
const condition = ConditionExpressionBuilder.buildFilterExpression(
271271
'myCollection',
272272
'IN',
273-
[['myCollection', 'myOtherValue']],
273+
[['myValue', 'myOtherValue']],
274274
undefined,
275275
undefined
276276
)
277-
expect(condition.statement).toBe('#myCollection IN (:myCollection)')
277+
expect(condition.statement).toBe('#myCollection IN (:myCollection_0, :myCollection_1)')
278278
expect(condition.attributeNames).toEqual({ '#myCollection': 'myCollection' })
279-
// 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
280-
// expect(condition.attributeValues).toEqual({
281-
// ':myCollection': { L: [{ S: 'myCollection' }, { S: 'myOtherValue' }] },
282-
// })
283279
expect(condition.attributeValues).toEqual({
284-
':myCollection': { SS: ['myCollection', 'myOtherValue'] },
280+
':myCollection_0': { S: 'myValue' },
281+
':myCollection_1': { S: 'myOtherValue' },
285282
})
286283
})
287284

src/dynamo/expression/condition-expression-builder.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,20 @@ export class ConditionExpressionBuilder {
151151
existingValueNames: string[] | undefined,
152152
propertyMetadata: PropertyMetadata<any> | undefined
153153
): Expression {
154-
const mappedValues = Mapper.toDbOne(values[0], propertyMetadata)
154+
const attributeValues: AttributeMap = (<any[]>values[0])
155+
.map(value => Mapper.toDbOne(value, propertyMetadata))
156+
.reduce(
157+
(result, mappedValue: AttributeValue, index: number) => {
158+
result[`${valuePlaceholder}_${index}`] = mappedValue
159+
return result
160+
},
161+
<AttributeMap>{}
162+
)
155163

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

159166
return {
160-
statement: `${namePlaceholder} IN (${Object.keys(attributeValues)})`,
167+
statement: `${namePlaceholder} IN (${inStatement})`,
161168
attributeNames,
162169
attributeValues,
163170
}
@@ -283,6 +290,10 @@ export class ConditionExpressionBuilder {
283290
)
284291
}
285292
break
293+
case 'IN':
294+
if (!Array.isArray(values[0])) {
295+
throw new Error('the provided value for IN operator must be an array')
296+
}
286297
}
287298
}
288299
}

0 commit comments

Comments
 (0)