#✅ - How do I set a sort key on my primary index (amplify dx)?

19 messages · Page 1 of 1 (latest)

craggy abyss
#

I just want to be able to sort on the name when I do a listExample query:

Example: a.model({
id: a.id().required(),
name: a.string().required(),
})

verbal tundra
#

you need to create a separate field to index on to make name a sort key

for example:

  Example: a
    .model({
      id: a.id().required(),
      type: a.string().required(),
      name: a.string().required(),
    })
    .secondaryIndexes((index) => [index("type").sortKeys(["name"])])
icy thunderBOT
#

✅ - How do I set a sort key on my primary index (amplify dx)?

#

Answer selected!
```you need to create a field to index on to make name a sort key

for example:

  Example: a
    .model({
      id: a.id().required(),
      type: a.string().required(),
      name: a.string().required(),
    })
    .secondaryIndexes((index) => [index("type").sortKeys(["name"])])

Kudos to @verbal tundra!
#1240646543296696322 message

verbal tundra
#

here's an example of the query and results with both sortDirections of ASC and DESC

  const listExamples = async () => {
    const { data: examples } = await client.models.Example.listByTypeAndName(
      {
        type: "Test",
      },
      { sortDirection: "DESC" }
    );

    console.log(examples);
  };
#

so you bucket records together using the secondary index and then you can sort the results of a Query with name as the sort key

craggy abyss
#

Just to complicate things slightly: Say I wanted to sort on the "createdAt" which is automatically generated (consider a scenario like only 10 newest comments or similar).

verbal tundra
slate obsidian
verbal tundra
#

Hmm, I didn’t have to set it, all those values you see in the response were auto populated at creation despite my schema marking it required 🤔 should be fine as long as the resolver Amplify generates is setting the value

slate obsidian
# verbal tundra Hmm, I didn’t have to set it, all those values you see in the response were auto...

How do you set it?

In my case, I've got type error because it needs createdAt when create

This is the model:

const Activity = a
  .model({
    createdAt: a.string().required(),
    distance: a.float(),
    duration: a.float(),
    runner: a.string().authorization(allow => [allow.owner().to(['read', 'delete'])]),
  })
  .secondaryIndexes((index) => [
    index('runner').sortKeys(['createdAt']),
  ])

And this is front-end code:

await client.models.Activity.create({
  distance: 10,
  duration: 10,
})

with error:

Argument of type '{ distance: number; duration: number; }' is not assignable to parameter of type '{ id?: string | undefined; createdAt: string; distance?: Nullable<number> | undefined; duration?: Nullable<number> | undefined; runner?: Nullable<string> | undefined; owner?: string | undefined; }'.
verbal tundra
#

can you share your project's package.json?

slate obsidian
#

Here is it:

{
  "name": "amplify-next-pages-template",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@aws-amplify/ui-react": "^6.1.11",
    "aws-amplify": "^6.2.0",
    "next": "14.1.4",
    "react": "^18",
    "react-dom": "^18"
  },
  "devDependencies": {
    "@aws-amplify/backend": "^1.0.0",
    "@aws-amplify/backend-cli": "^1.0.1",
    "@types/node": "^20",
    "@types/react": "^18",
    "@types/react-dom": "^18",
    "aws-cdk": "^2.137.0",
    "aws-cdk-lib": "^2.137.0",
    "constructs": "^10.3.0",
    "esbuild": "^0.20.2",
    "eslint": "^8",
    "eslint-config-next": "14.1.4",
    "tsx": "^4.7.2",
    "typescript": "^5.4.5"
  }
}
scarlet gull
#

is there any way to paginate this query @verbal tundra ?

ruby oar
#

Is there any idea to use these sort queries in Amplify frontend libraries ? ie: Amplify.API.query(ModelQueries.scan(Todo.classtype, where: Todo.SORTFIELD.eq(...)))

It will increase our productivity

ruby oar
verbal tundra