SmiffyKmc
SmiffyKmc

Reputation: 891

Can't Query DynamoDB with Begins_with for list of Items

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.

Table details

Table Columns

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

Upvotes: 2

Views: 5789

Answers (2)

Abhaya Chauhan
Abhaya Chauhan

Reputation: 1201

Here is what I can gather:

  1. You are missing a comma at the end of your IndexName line:

const params = {

TableName: process.env.DYNAMODB_DESKS_TABLE,

IndexName: 'deskName',
  1. For ExpressionAttributeValues, you need to define the data type, ie:
ExpressionAttributeValues: {
  ":deskName": { "S": "cork" }
}

Upvotes: 0

Noel Llevares
Noel Llevares

Reputation: 16037

You're getting this...

Syntax error in module 'api/desks/get': SyntaxError
    KeyConditionExpression: "#deskName = :deskName",
    ^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Unexpected identifier

because of this...

const params = {
  TableName: process.env.DYNAMODB_DESKS_TABLE,
  IndexName: 'deskName'
  KeyConditionExpression: "#deskName = :deskName",
  ExpressionAttributeNames:{
    "#deskName": "deskName"
  },
  ExpressionAttributeValues: {
    ":deskName": "cork"
  }
}

JavaScript objects require a comma after each property. You're missing one after IndexName: 'deskName'.

I would recommend the use of DocumentClient as it easily maps Javascript data types to DynamoDB data types.

Upvotes: 2

Related Questions