无法使用dynamodb-data-mapper-js与LSI创建DynamoDB表

时间:2019-09-18 09:04:40

标签: javascript node.js typescript amazon-web-services amazon-dynamodb

我正在尝试使用官方AWS库dynamodb-data-mapper-js在Node.js中设置DynamoDB数据库,以创建持久性模型对象和数据映射器。但是,看起来好像什么都没有提及使用Local Sencondary Indexes或GSI创建表。

我发现an issue in Github带有有关如何使用LSI创建表的参考,所提供的示例解释了引用rangeKey与声明LSI之间的区别:

@table('items')
class Item {
  @hashKey({ // <-- this is your normal hash key (shared by table and of LSI)
    indexKeyConfigurations:{
      ItemIdIndex: 'HASH' // The key (ItemIdIndex) is the name of the index; the value is the key type ('HASH' or 'RANGE')
    }
  })
  itemId: string;

  @rangeKey() // <-- this is your normal range key (not part of LSI)
  displayName: string;

  @attribute({
    // And this other attribute acts as the LSI's RangeKey
    indexKeyConfigurations: {
      ItemIdIndex: 'RANGE'
    }
  })
  foo: string;

  @attribute()
  bar: string;
}

但是,我创建了一个具有完全相同的表模型的表,并且出现以下错误:

Error: No options provided for ItemIdIndex index at indexDefinitions 

我无法获得更多信息,即使他们说支持LSI或GSI,该文档也没有提及任何有关LSI或GSI的信息。

1 个答案:

答案 0 :(得分:0)

我发现问题是创建表时需要指定一些有关索引类型的选项。例如,您至少需要指定它是LSI还是GSI,以及索引必须包含哪些键。

mapper.createTable(Item, {
    readCapacityUnits: 5,
    writeCapacityUnits: 5,
    indexOptions: {
        LocalIndexExample: {
            type: 'local',
            projection: 'all',
        },
        GlobalIndexExample: {
            type: 'global',
            projection: ['createdBy', 'createdAt'],
            readCapacityUnits: 2,
            writeCapacityUnits: 3,
        },
    },
})