为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕码人2483693 2021-10-21 10:54:21
我正在尝试使用官方 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 的内容。
查看完整描述

1 回答

?
墨色风雨

TA贡献1853条经验 获得超6个赞

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

        },

    },

})


查看完整回答
反对 回复 2021-10-21
  • 1 回答
  • 0 关注
  • 179 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信