Search code examples
typescriptamazon-dynamodbaws-sdkdynamodb-queries

getall with aws-sdk/lib-dynamodb in typescript


My database about pokemon has this schema:

id: pikachu (pokemon's name), content: Pokemon (object), height: 3

I would like to get the pokemon with the biggest height, in order to do this I want to get all the pokemon in my database and later "reduce" the array to get the biggest.

I am using this aws library: @aws-sdk/lib-dynamodb

How can I create a QueryCommand to get all values? I don't need any KeyConditions but AWS says that I have to include them.

  new QueryCommand({
    TableName: this.tableName,
  }),
);
const array =
  response?.Items?.map(
    item =>
      new PokemonDB({
        pokemon: item.content,
        height: item.height,
      }),
  ) ?? [];

const max = array.reduce(function (prev, current) {
  return prev.height > current.height ? prev : current;
});

return max.height;

The error is:

ValidationException: Either the KeyConditions or KeyConditionExpression parameter must be specified in the request.

Solution

  • DynamoDB is fundamentally a key/value database. All operations has to provide the hash/partition key (PK), and optionally a sort key. There is no such thing as efficiently querying all items without providing a PK, as you would have to resort to a full table scan to achieve that.

    Typically, data modelling in Dynamo is using what is typically referred to as a "single table design". So, you can have a table that looks something like this:

    PK SK myVal
    user_123_friends friend_456 x
    user_123_friends friend_789 y
    user_456_friends friend_123 z

    In this setup, you can do a query with the PK user_123_friends and then say that the SK should start with friend_, to list all the friends for user 123, without resorting to things like full table scans etc.

    So, more generally speaking, you typically end up storing "normalized" data, i.e. stuff is stored multiple times in various ways in your table(s). The way you store stuff depends completely on your access patterns.

    I can recommend this talk by Rick Houlihan to get into the intricacies of data modelling with Dynamo :) https://www.youtube.com/watch?v=HaEPXoXVf2k