awslabs / dynamodb-data-mapper-js

A schema-based data mapper for Amazon DynamoDB.
https://awslabs.github.io/dynamodb-data-mapper-js/
Apache License 2.0
817 stars 106 forks source link

Issues with auto creating secondary index #193

Open RaviKAG opened 4 years ago

RaviKAG commented 4 years ago

I am having issues with creating secondary index. This is how my Entity class looks like

import { attribute, rangeKey, table, hashKey} from '@aws/dynamodb-data-mapper-annotations'; @table('Comments') export class Comment{ @hashKey({indexKeyConfigurations: { commentRefId: 'HASH' } }) commentRefId: string;

@rangeKey({ defaultProvider: () => new Date() }) createdOn: Date;

@attribute() data?: string;

@attribute() author: string;

@attribute() authorId: number;

@attribute() uuid: string; }

create table code this.dynamoDB.mapper.ensureTableExists(Comment, { readCapacityUnits: 5, writeCapacityUnits: 5}

When I run this code, it returns error saying (node:33860) UnhandledPromiseRejectionWarning: Error: No options provided for commentRefId index

aws_dataMapper_error

hxhieu commented 3 years ago

@RaviKAG according to this https://github.com/awslabs/dynamodb-data-mapper-js/issues/60#issuecomment-396792816 Your ensureTableExists must have the indexOptions block to define each of your indexes

this.dynamoDB.mapper.ensureTableExists(Comment, {
  readCapacityUnits: 5,
  writeCapacityUnits: 5,
  indexOptions: {
    commentRefId: {
      type: 'global',
      ...
    }
  }
}