【问题标题】:Unable to create DynamoDB table with LSI using dynamodb-data-mapper-js无法使用 dynamodb-data-mapper-js 使用 LSI 创建 DynamoDB 表
【发布时间】:2020-01-19 04:29:56
【问题描述】:

我正在尝试使用官方 AWS 库 dynamodb-data-mapper-js 在 Node.js 中设置 DynamoDB 数据库,以创建持久模型对象和数据映射器。但是,它似乎没有提及有关使用本地二级索引或 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 或 GS​​I,文档也没有说明任何内容。

【问题讨论】:

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


    【解决方案1】:

    我发现问题是您在创建表时需要指定一些关于索引类型的选项。例如,您至少需要指定它是 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,
            },
        },
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-03
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多