Ordering By Relation Aggregates with Where Clauses
Open
Nobody has claimed this yet.
topic: client api
topic: orderByRelation
- Dominant language
- TypeScript
- Stars
- 47.6k
- Forks
- 2.5k
- Avg merge
- 21h 59m
- Merged PRs (30d)
- 95
Description
Problem
Currently Relation Aggregates only support _count with asc and desc. One would hope that it would support relation aggregates with where subqueries.
Suggested solution
The ability to add subqueries with where clauses inside _count.
Example:
orderBy: {
table2: {
table2_key: {
_count: 'desc',
where: {
foo: 'bar'
}
}
}
}
Alternatives
Right now you need to write a raw query, example:
SELECT
key1,
key2,
(SELECT COUNT(*) FROM table2 WHERE table2.key1 = 'bar') AS sortingKey
FROM table1
JOIN table2 ON table1.table2_id = table2.id
ORDER BY sortingKey DESC
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No source file, test, or entry point is named in the issue. Start by locating Prisma's relation-aggregate orderBy implementation and its tests, then define coverage for filtered _count aggregates and verify that the requested nested where syntax produces the expected ordering.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- backend-api-design, databases
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100