[Gen 2] Request to support sorting relation items
Describe the feature you'd like to request
Say there is a Team has a one-to-many relationship to Member. My goal is to get team members sorted by createdAt field.
Below is the official example to define relationships, but there is no instruction how to sort the team members like await team.members({sortDirection: 'DESC'})
const schema = a.schema({
Member: a.model({
name: a.string().required(),
// 1. Create a reference field
teamId: a.id(),
// 2. Create a belongsTo relationship with the reference field
team: a.belongsTo('Team', 'teamId'),
})
.authorization(allow => [allow.publicApiKey()]),
Team: a.model({
mantra: a.string().required(),
// 3. Create a hasMany relationship with the reference field
// from the `Member`s model.
members: a.hasMany('Member', 'teamId'),
})
.authorization(allow => [allow.publicApiKey()]),
});
const { data: members } = await team.members();
Describe the solution you'd like
Support sortKey config here members: a.hasMany('Member', 'teamId', {sortKeys: ['createdAt']}),
Describe alternatives you've considered
Add .secondaryIndexes(index => [index('teamId').sortKeys(['createdAt']).queryField('listByDate')]) to Member model, and get members by client.models.Message.listByDate({roomId}, {sortDirection: 'DESC}) instead of await team.members(),
Additional context
No response
Is this something that you'd be interested in working on?
- [ ] 👋 I may be able to implement this feature request
Would this feature include a breaking change?
- [ ] ⚠️ This feature might incur a breaking change
Hey @hangoocn, Thanks for raising this. We are marking this as a feature request for the team to evaluate further.
Having the ability to add a sort key to relationships like .hasMany would be great. I'm having the same use-case where i want to have all results for the .hasMany relationship sorted by createdAt.