You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Amplify:如何在Schema中指定GSI的投影属性?

解决方案

1. 直接通过Schema指定GSI投影属性

Amplify的@index指令支持通过projection参数配置GSI的投影属性,完全不需要手动修改解析器或AWS控制台操作。你只需要在现有的@index注解里添加投影配置即可,支持三种投影类型:

  • KEYS_ONLY:仅投影主键和GSI的键
  • ALL:投影所有属性
  • INCLUDE:指定要投影的具体属性列表

修改后的Schema示例:

input AMPLIFY {
  globalAuthRule: AuthRule = { allow: public }
}
type Item
  @model
   @auth(
    rules: [
      { allow: owner, ownerField: "sub", operations: [create, update, delete] }
      { allow: private, operations: [read] }
      { allow: public, operations: [read] }
    ]
  ) {
  pk: String! @primaryKey(sortKeyFields: ["sk"])
  sk: String!
  companyName: String
  itemType: String
    @index(
      name: "companyList"
      sortKeyFields: ["companyName"]
      queryField: "listCompanies"
      # 配置投影属性,这里以INCLUDE指定具体属性为例
      projection: { type: "INCLUDE", attributes: ["companyName", "otherAttributeOne"] }
    )
  otherAttributeOne: String
  otherAttributeTwo: String
  otherAttributeThree: String
}

修改后执行amplify push,Amplify会自动更新DynamoDB表的GSI投影配置,同步到对应的环境中。

2. 若必须通过控制台配置的情况

如果因为某些原因只能通过AWS控制台修改GSI投影,那么每个Amplify环境都需要单独配置。因为Amplify的每个环境对应独立的DynamoDB表,dev和prod是完全隔离的资源,控制台对dev表的修改不会自动同步到prod表。

如果不想每次部署新环境都手动操作,可以使用Amplify的资源覆盖(override)功能,在项目中编写override脚本(比如amplify/backend/api/<api-name>/override.ts)来固化GSI的投影配置,这样每次部署不同环境时,Amplify会自动应用这个配置,无需手动操作控制台。

内容的提问来源于stack exchange,提问作者DevOverflow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 15:33:14