prisma / prisma/orm

Ordering By Relation Aggregates with Where Clauses

Open
#17,023 1 comment 4 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.