I've been trying to query data from the DynamoDB for 2 days now. Driving me insane.
I have a table for desks in an office. Say there is two offices, Cork and Dublin. I have a column called 'deskName' which would have names like 'cork1', 'cork2', 'dub1', 'dub2'. I want to be able to query the table for Items which contain 'cork' or 'dub' as they are in the same table but I don't want to SCAN the whole table back.
CODE:
const params = {
TableName: process.env.DYNAMODB_DESKS_TABLE,
//KeyConditionExpression: '#deskName = :deskName',
KeyConditionExpression: "begins_with(#deskName, :deskName)",
ExpressionAttributeNames: {
"#deskName": "deskName"
},
ExpressionAttributeValues: {
":deskName": "cork"
}
}
dynamodb.query(params, (error, result) => {
if (error) {
console.error(error);
callback(null, {
statusCode: error.statusCode || 501,
headers: {'Content-Type': 'text/plain'},
body: 'Couldn\'t get desks'
});
return;
}
const response = {
statusCode: 200,
body: JSON.stringify(result.Item)
};
callback(null, response);
});
YAML:
HotDeskDesksDBTable:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: Retain
Properties:
AttributeDefinitions:
-
AttributeName: deskName
AttributeType: S
KeySchema:
-
AttributeName: deskName
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:provider.environment.DYNAMODB_DESKS_TABLE}
ERROR:
ValidationException: Query key condition not supported
I managed to get one item coming back when I had the condition = 'cork-1'. I want to get every item that begins with 'cork'.
Thank you
deskName
the name of a secondary index you aren't showing in the yaml file? – Mark B