I was able to get sorting to work via a lazy loading nested query using composite identifiers. Using secondary index with a sort key did not work.
Demo Schema:
const schema = a.schema({
Team: a
.model({
name: a.string().required(),
players: a.hasMany("Player", ["teamId"]),
})
.authorization((allow) => [allow.publicApiKey()]),
Player: a
.model({
name: a.string().required(),
position: a.string(),
teamId: a.id().required(),
jerseyNumber: a.integer().required(),
team: a.belongsTo("Team", ["teamId"]),
createdAt: a.datetime(),
})
.authorization((allow) => [allow.publicApiKey()])
.identifier(["teamId", "jerseyNumber"]),
});
Demo Code:
// First get the team
const { data: teamData } = await client.models.Team.get({ id: team.id });
if (teamData) {
// Then lazy load the players relationship
// Players are sorted by jersey number
const playersResult = await teamData.players();
const playersData = playersResult.data;
console.log(playersData)
}
Oddly enough, using a selection set to populate players doesn't result in players sort by jersey number:
const teamWithPlayersSelectionSet = ["id", "name", "players.*"] as const;
// Players are not sorted by jersey number
const { data } = await client.models.Team.get(
{ id: team.id },
{
selectionSet: teamWithPlayersSelectionSet,
}
);