无法使用 dynamodb-data-mapper-js 创建带有 LSI 的 DynamoDB 表

我正在尝试使用官方 AWS 库dynamodb-data-mapper-js在 Node.js 中设置一个 DynamoDB 数据库来创建持久模型对象和数据映射器。但是,它似乎没有提及使用本地辅助索引或 GSI 创建表的任何内容。


我在 Github 中发现了一个关于如何使用 LSI 创建表的参考资料,提供的示例解释了引用 arangeKey和声明 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 的内容。


慕码人2483693
浏览 180回答 1
1回答

墨色风雨

我发现问题是在创建表时需要指定一些关于索引类型的选项。例如,您至少需要指定它是 LSI 还是 GSI,以及索引必须包含哪些键。mapper.createTable(Item, {&nbsp; &nbsp; readCapacityUnits: 5,&nbsp; &nbsp; writeCapacityUnits: 5,&nbsp; &nbsp; indexOptions: {&nbsp; &nbsp; &nbsp; &nbsp; LocalIndexExample: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'local',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; projection: 'all',&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; GlobalIndexExample: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'global',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; projection: ['createdBy', 'createdAt'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readCapacityUnits: 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writeCapacityUnits: 3,&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; },})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript