5

I am using Vogels, DynamoDB data mapper for NodeJS. I am querying against a global index according to the Vogels' documentation. All I have done is, created a model with a global secondary index like this:

let MyModel = vogels.define('MyModel', {
  hashKey: 'uuid',
  timestamps: true,
  updatedAt: 'updated_at',
  createdAt: 'created_at',
  schema: MyModelBaseSchema,
  indexes : [{ 
    hashKey : 'gameTitle', rangeKey : 'topScore', name : 'GameTitleIndex', type : 'global'
  }]
});

and querying against this index

MyModel.query('game 1')
  .usingIndex('GameTitleIndex')
  .loadAll()
  .select("COUNT");

When running any tests it shows an exception ValidationException: The table does not have the specified index: GameTitleIndex

According to the documentation, this is all I have to do to get data. Is there anything which I have missed to query this index? Any answers will be appreciated. Thanks in advance.

NoSQLKnowHow
  • 4,449
  • 23
  • 35
Ayushi Sood
  • 101
  • 1
  • 8

2 Answers2

3

– In case you are using Serverless framework with serverless-dynamodb-local and serverless-offline plugins (for local testing).

– And in case you are created new Local Secondary Index, but the command shown below still not showing it under LocalSecondaryIndexes configuration node:

aws dynamodb describe-table --table-name YOUT_TABLE_NAME --endpoint-url http://localhost:8000

– And in case you are receiving error ValidationException: The table does not have the specified index: YOUR_INDEX_NAME when using code similar to shown below:

  query(uid, id) {
    const params = {
      TableName: YOUR_TABLE_NAME,
      IndexName: YOUR_LSI_NAME,
      KeyConditionExpression: "#uid = :uid and #id = :id",
      ExpressionAttributeNames: {
        "#uid": "uid",
        "#id": "id",
      },
      ExpressionAttributeValues: { ":uid": uid, ":id": id },
    };
    return DB.query(params).promise();
  }

Then most likely you need to delete local DynamoDB database file and restart services with serverless offline start command. This will force recreation of local DynamoDB database file with proper Local Secondary Indexes.


To delete local DynamoDB database file do the following:

  1. Update the serverless.yml by specifying path to DynamoDB database, like shown below:
custom:
  dynamodb:
    stages:
      - ${self:provider.stage}
    start:
      port: 8000
      inMemory: true
      migrate: true
      dbPath: "./.db" # Make sure that folder exists!
  1. Now you can go to the folder ./.db and remove file shared-local-instance.db (which is a SQLite database which mimics DynamoDB :0).

As result on next start with the command serverless offline start the up to date local DynamoDB database file will be created.

Vlad
  • 6,402
  • 1
  • 60
  • 74
1

In case someone's stuck at the same issue, here is the answer: After creating a new index in model, either local secondary index or global secondary index, Migrations are to be run. Only after then the table will have the specified index. Refer to this issue for more clarification.

Ayushi Sood
  • 101
  • 1
  • 8