Skip to content

Commit

Permalink
fix(client): aggregate types
Browse files Browse the repository at this point in the history
Closes #2959
  • Loading branch information
timsuchanek committed Jul 13, 2020
1 parent 7cd004b commit 539db28
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/packages/client/fixtures/blog/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ datasource db {
generator client {
provider = "prisma-client-js"
binaryTargets = ["native"]
experimentalFeatures = ["connectOrCreate", "aggregations"]
experimentalFeatures = ["connectOrCreate", "aggregateApi"]
}

model User {
Expand Down
11 changes: 11 additions & 0 deletions src/packages/client/src/__tests__/types/aggregate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ async function main() {
})

const { count, avg, max, min, sum } = await prisma.user.aggregate({
cursor: {
email: 'a@a.de',
},
orderBy: {
age: 'asc',
},
skip: 12,
take: 10,
where: {
age: { gt: 500 },
},
count: true,
avg: {
age: true,
Expand Down
32 changes: 22 additions & 10 deletions src/packages/client/src/generation/TSClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ export class Model implements Generatable {
return argsTypes
}
private getAggregationTypes() {
const { model } = this
const { model, mapping } = this
const aggregateType = this.dmmf.outputTypeMap[getAggregateName(model.name)]
const aggregateTypes = [aggregateType]

Expand All @@ -762,6 +762,15 @@ export class Model implements Generatable {
aggregateTypes.push(maxType)
}

const aggregateRootField = this.dmmf.queryType.fields.find(
(f) => f.name === mapping?.aggregate,
)
if (!aggregateRootField) {
throw new Error(
`Could not find aggregate root field for model ${model.name}. Mapping: ${mapping?.aggregate}`,
)
}

return `${aggregateTypes
.map((type) => new SchemaOutputType(type).toTS())
.join('\n')}
Expand Down Expand Up @@ -795,15 +804,18 @@ ${
export type ${getAggregateArgsName(model.name)} = {
${indent(
aggregateType.fields
.map((f) => {
if (f.name === 'count') {
return `${f.name}?: true`
}
return `${f.name}?: ${getAggregateInputType(
(f.outputType.type as SchemaOutputType).name,
)}`
})
aggregateRootField.args
.map((arg) => new InputField(arg).toTS())
.concat(
aggregateType.fields.map((f) => {
if (f.name === 'count') {
return `${f.name}?: true`
}
return `${f.name}?: ${getAggregateInputType(
(f.outputType.type as SchemaOutputType).name,
)}`
}),
)
.join('\n'),
tab,
)}
Expand Down

0 comments on commit 539db28

Please sign in to comment.