Skip to content
This repository was archived by the owner on Jan 14, 2021. It is now read-only.
This repository was archived by the owner on Jan 14, 2021. It is now read-only.

Group By #1

@schickling

Description

@schickling

Problem

Most database support ways to indicate how to group results, especially in combination with usage of aggregators.
Prisma 2 currently doesn't offer any way to group results, and requires to fall back to using raw SQL queries. This defeats the benefits of having a type-safe client, and hurts code maintainability.

Suggested solution

Add support for ways to group results

Initial API example proposal:

type DynamicResult4 = {
  lastName: string
  records: User[]
  aggregate: { age: { avg: number } }
}
const groupByResult: DynamicResult4 = await prisma.users.groupBy({
  key: 'lastName',
  having: { age: { avgGt: 10 } },
  where: { isActive: true },
  first: 100,
  orderBy: { lastName: 'ASC' },
  select: {
    records: { take: 100 },
    aggregate: { age: { avg: true } },
  },
})

type DynamicResult5 = {
  raw: any
  records: User[]
  aggregate: { age: { avg: number } }
}
const groupByResult2: DynamicResult5 = await prisma.users.groupBy({
  raw: { key: 'firstName || lastName', having: 'AVG(age) > 50' },
  select: {
    records: { $first: 100 },
    aggregate: { age: { avg: true } },
  },
})

Next steps

  • Define scope of the kinds of group by statement we want to support first
  • Agree on API design
  • Implement in engines
  • Implement in client

Metadata

Metadata

Labels

kind/featureA request for a new feature.roadmap/plannedPlanned for being picked up once we're done with the WIP

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions