#✅ - How do I set a sort key on my primary index (amplify dx)?
19 messages · Page 1 of 1 (latest)
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"])])
✅ - 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
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
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).
same idea, you just need to add createdAt to the schema, it'll still get auto-populated
like so:
User: a
.model({
username: a.string().required(),
type: a.string().required(),
createdAt: a.string().required(),
})
.secondaryIndexes((index) => [
index("type").sortKeys(["createdAt"]),
])
If createdAt is required we have to set it when create.
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
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; }'.
can you share your project's package.json?
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"
}
}
is there any way to paginate this query @verbal tundra ?
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
just give limit and ask for nextToken, it will paginate I think
This is correct, adding limit to the query will result in pagination. A nextToken will be provided in the response until there are no more items to process.