Skip to content

Commit

Permalink
[FIX] - IN clause in query (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
sravan1000 authored Jul 3, 2021
1 parent 84cca2b commit cfaf478
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/query/queryBuilder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,15 @@ const sampleCases = [
},
expected: {
KeyConditionExpression: '#PK = :pk',
FilterExpression: '#ID IN :id',
FilterExpression: '#ID in (:id1, :id2)',
ExpressionAttributeNames: {
'#PK': 'pk',
'#ID': 'id',
},
ExpressionAttributeValues: {
':pk': 'xxxx',
':id': '(yyyy,zzzz)',
':id1': 'yyyy',
':id2': 'zzzz',
},
},
},
Expand Down
13 changes: 11 additions & 2 deletions src/query/queryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,17 @@ function buildConditionExpressions<T extends object = AnyObject>(
` AND ${valueExpression}_end`,
);
} else if (operator === 'IN') {
expressionAttributeValues[valueExpression] = `(${condition.join(',')})`;
filterExpression.push(`${keyName} ${operator} ${valueExpression}`);
let expressionTemp = '';
condition.forEach((eachVal, index) => {
expressionAttributeValues[`${valueExpression}${index + 1}`] = eachVal;
if (!expressionTemp) {
expressionTemp = expressionTemp + `${valueExpression}${index + 1}`;
} else {
expressionTemp =
expressionTemp + `, ${valueExpression}${index + 1}`;
}
});
filterExpression.push(`${keyName} in (${expressionTemp})`);
} else if (operator === 'CONTAINS') {
expressionAttributeValues[valueExpression] = condition;
filterExpression.push(
Expand Down

0 comments on commit cfaf478

Please sign in to comment.