2

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

SmiffyKmc
  • 801
  • 1
  • 16
  • 34
  • What is the error or issue exacty? – Mark B Oct 03 '17 at 11:53
  • Totally forgot to add that. Sorry, I'll add it to the question now. – SmiffyKmc Oct 03 '17 at 11:54
  • @MarkB added the error there now, thanks for that! – SmiffyKmc Oct 03 '17 at 11:56
  • Is `deskName` the name of a secondary index you aren't showing in the yaml file? – Mark B Oct 03 '17 at 12:01
  • I don't think I have a secondary index. I know a lack of knowledge it the main issue here but trying to learn as I go if you get me. Should I set it as the secondary index? Is there a way to do it? Also thank you for your help! – SmiffyKmc Oct 03 '17 at 12:03
  • I am also using the Serverless framework if that clears anything up at all. – SmiffyKmc Oct 03 '17 at 12:04
  • Given your table schema, if you haven't created a secondary index named `deskName` then trying to specify an index named `deskName` in your query is obviously wrong. If the yaml file shows your entire table configuration then you would only be able to query on the `id` field. – Mark B Oct 03 '17 at 12:05
  • Oh! Yeah I can guess it's wrong alright. So if I changed the ID field to use deskName instead it should be what would fix it? Or instead of having an ID field have deskName as the 'ID' for the table? Thanks – SmiffyKmc Oct 03 '17 at 12:06
  • 1
    I really can't say for sure what would fix it without knowing exactly what your table data looks like. It sounds like you really need to read up on DynamoDB a bit more before trying to use it. – Mark B Oct 03 '17 at 12:09
  • Yeah you are right oO. Everything else was going pretty well until this issue came along xD. Mark thanks a million for all the help! – SmiffyKmc Oct 03 '17 at 12:12
  • @MarkB I fixed it up a bit and was hoping you'd be able to see something. It's driving me mad as it seems like I'm going round in circles oO. I added the table structure and details. – SmiffyKmc Oct 03 '17 at 22:17
  • Thanks again for the fast reply! Serious?! I was trying to follow the doc for DynamoDB and it said it used it. I checked ES but that seems to need an EC2 instance. Would I just be able to hook it onto the table? – SmiffyKmc Oct 04 '17 at 07:08
  • Ah it's fine. Found a way :). – SmiffyKmc Oct 04 '17 at 07:40

2 Answers2

2

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.

Noel Llevares
  • 15,018
  • 3
  • 57
  • 81
0

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" }
}
Community
  • 1
  • 1
Abhaya Chauhan
  • 1,219
  • 11
  • 8