This is my current lambda function, which return all data from Table "Banner".
getBanner.js
'use strict'
const AWS = require('aws-sdk');
exports.handler = async function (event, context, callback) {
const documentClient = new AWS.DynamoDB.DocumentClient();
let responseBody = "";
let statusCode = 0;
const params = {
TableName : "Banner",
};
try{
const data = await documentClient.scan(params).promise();
responseBody = JSON.stringify(data.Items);
statusCode = 200
}catch(err){
responseBody = `Unable to get products: ${err}`;
statusCode = 403
}
const response = {
statusCode: statusCode,
headers:{
"Content-Type": "application/json",
'Access-Control-Allow-Origin': '*', // Required for CORS support to work
},
body: responseBody
}
return response
}
It is the sample returned data
[
{
Order: 123,
Year: 2000
...
},
{
Order: 77,
Year: 2007
...
}
]
I understand If I want to do some query for attribute order, I need to change to following.
...
var params = {
TableName : "Banner",
KeyConditionExpression: "#od = :yyyy",
ExpressionAttributeNames:{
"#od": "order"
},
ExpressionAttributeValues: {
":yyyy": 1
}
};
...
What if I want to do the following?
There may be some use cases which make me need to do query for another attribute, or multiple attributes.
How should I do have a function which can handle the queryStringParameters dynamically?
Updated based on the comment, but another error occurs
'use strict'
const AWS = require('aws-sdk');
exports.handler = async function (event, context, callback) {
const documentClient = new AWS.DynamoDB.DocumentClient();
let responseBody = "";
let statusCode = 0;
console.log(event)
const queryStringParams = event.queryStringParameters;
console.log(queryStringParams)
let KeyConditionExpression = ''
let ExpressionAttributeNames={};
let ExpressionAttributeValues = {};
for (const property in queryStringParams) {
KeyConditionExpression += ` #${property} = :${property} ,`;
ExpressionAttributeNames['#'+property] = property ;
ExpressionAttributeValues[':'+property]=queryStringParams[property];
}
KeyConditionExpression= KeyConditionExpression.slice(0, -1);
const params = {
TableName : "Banner",
KeyConditionExpression: KeyConditionExpression,
ExpressionAttributeNames: ExpressionAttributeNames,
ExpressionAttributeValues: ExpressionAttributeValues
};
console.log(params) //Response A
try{
const data = await documentClient.scan(params).promise();
responseBody = JSON.stringify(data.Items);
statusCode = 200
}catch(err){
responseBody = `Unabel to get products: ${err}`;
statusCode = 403
}
const response = {
statusCode: statusCode,
headers:{
"Content-Type": "application/json",
'Access-Control-Allow-Origin': '*', // Required for CORS support to work
},
body: responseBody
}
return response
}
Unabel to get products: ValidationException: ExpressionAttributeNames can only be specified when using expressions
This is the value of the ‘params’ obtained from cloudwatch Response A
{
TableName: 'Banner',
KeyConditionExpression: ' #order = :order , #year = :year ',
ExpressionAttributeNames: { '#order': 'order', '#year': 'year' },
ExpressionAttributeValues: { ':order': '1', ':year': '2007' }
}