amplify-category-api icon indicating copy to clipboard operation
amplify-category-api copied to clipboard

AmplifyOutputs.json ignores Multiple Secondary Index Queries using the same SecondaryIndex Field

Open ChristopherGabba opened this issue 1 year ago • 3 comments

Environment information

System:
  OS: macOS 14.5
  CPU: (10) arm64 Apple M2 Pro
  Memory: 158.91 MB / 16.00 GB
  Shell: /bin/zsh
Binaries:
  Node: 22.7.0 - /opt/homebrew/bin/node
  Yarn: 1.22.22 - /opt/homebrew/bin/yarn
  npm: 10.8.2 - /opt/homebrew/bin/npm
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/auth-construct: 1.3.0
  @aws-amplify/backend: 1.2.0
  @aws-amplify/backend-auth: 1.1.3
  @aws-amplify/backend-cli: 1.2.5
  @aws-amplify/backend-data: 1.1.3
  @aws-amplify/backend-deployer: 1.1.0
  @aws-amplify/backend-function: 1.3.4
  @aws-amplify/backend-output-schemas: 1.2.0
  @aws-amplify/backend-output-storage: 1.1.1
  @aws-amplify/backend-secret: 1.1.0
  @aws-amplify/backend-storage: 1.1.2
  @aws-amplify/cli-core: 1.1.2
  @aws-amplify/client-config: 1.3.0
  @aws-amplify/deployed-backend-client: 1.4.0
  @aws-amplify/form-generator: 1.0.1
  @aws-amplify/model-generator: 1.0.5
  @aws-amplify/platform-core: 1.0.7
  @aws-amplify/plugin-types: 1.2.1
  @aws-amplify/sandbox: 1.2.0
  @aws-amplify/schema-generator: 1.2.1
  aws-amplify: 6.5.3
  aws-cdk: 2.154.1
  aws-cdk-lib: 2.154.1
  typescript: 5.3.3
AWS environment variables:
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Data packages

├─┬ @aws-amplify/[email protected]
│ └─┬ @aws-amplify/[email protected]
│   └── @aws-amplify/[email protected]
└─┬ @aws-amplify/[email protected]
  └─┬ @aws-amplify/[email protected]
    └── @aws-amplify/[email protected]

Description

I am trying to make multiple queries using the same SecondaryIndex but different sortKeys:

User: a
    .model({
      id: a.id().required(),
      birthdate: a.string().required(),
      email: a.email().required(),
      firstName: a.string().required(),
      lastName: a.string().required(),
      username: a.string().required(),
      phoneNumber: a.hasOne("PhoneNumber", "userId"),
      pushToken: a.string(),
      profileImageS3Path: a.string(),
      profileImageBlurhash: a.string(),
      status: a.ref("UserStatus").required(),
    })
    .secondaryIndexes((index) => [
      index("status").queryField("listActiveUsersByFirstname").sortKeys(["firstName"]), // HERE
      index("status").queryField("listActiveUsersByLastname").sortKeys(["lastName"]), // HERE
      index("status").queryField("listActiveUsersByUsername").sortKeys(["username"]), // HERE
      index("email").name("byEmail").queryField("listUsersByEmail"),
    ])
    .authorization((allow) => [allow.publicApiKey()]),

But when I run npx ampx sandbox or I push the commit to the the backend console, the amplify_outputs.json file just ignores the second two secondary indexes:

"attributes": [
            {
              "type": "model",
              "properties": {}
            },
            {
              "type": "key",
              "properties": {
                "fields": [
                  "id"
                ]
              }
            },
            {
              "type": "key",
              "properties": {
                "name": "byEmail",
                "queryField": "listUsersByEmail",
                "fields": [
                  "email"
                ]
              }
            },
            {
              "type": "key",
              "properties": {
                "name": "usersByStatusAndFirstName",
                "queryField": "listActiveUsersByFirstname",
                "fields": [
                  "status",
                  "firstName"
                ]
              }
            },
            {
              "type": "auth",
              "properties": {
                "rules": [
                  {
                    "allow": "public",
                    "provider": "apiKey",
                    "operations": [
                      "create",
                      "update",
                      "delete",
                      "read"
                    ]
                  }
                ]
              }
            }
          ],
          "primaryKeyInfo": {
            "isCustomPrimaryKey": false,
            "primaryKeyFieldName": "id",
            "sortKeyFieldNames": []
          }
        },

As you can see, it only made the listActiveUsersByFirstName. I can confirm as well when I try to run these other two queries, I get a function does not exist type of error.

I've tried making one secondaryIndex query like so:

    .secondaryIndexes((index) => [
      index("status")
        .queryField("listActiveUsersByName")
        .sortKeys(["firstName", "lastName", "username"]),
      index("email").name("byEmail").queryField("listUsersByEmail"),
    ])

But the query doesn't seem to be working:

const results = await client.models.User.listActiveUsersByName(
          {
            status: UserStatus.ACTIVE,
            firstNameLastNameUsername: {
              beginsWith: {
                firstName: capitalizeFirstLetter(word),
                lastName: capitalizeFirstLetter(word),
                username: capitalizeFirstLetter(word),
              },
            },
          },
          { selectionSet: userSelectionSet },
        )

I'm trying to get results for each category (firstname, lastname, and username) individually, not combined.

ChristopherGabba avatar Aug 28 '24 13:08 ChristopherGabba