Skip to content

Commit

Permalink
feat(client): aggregate - put it behind aggregateApi feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
timsuchanek committed Jul 6, 2020
1 parent ac96121 commit 2c90a7f
Show file tree
Hide file tree
Showing 7 changed files with 496 additions and 465 deletions.
4 changes: 4 additions & 0 deletions src/packages/client/src/__tests__/json-dmmf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ test('JsonFilter should contain equals and not', async () => {
"name": "id",
},
],
"isOneOf": true,
"name": "UserWhereUniqueInput",
},
Object {
Expand Down Expand Up @@ -156,6 +157,7 @@ test('JsonFilter should contain equals and not', async () => {
"name": "field",
},
],
"isOneOf": false,
"name": "UserCreateInput",
},
Object {
Expand Down Expand Up @@ -185,6 +187,7 @@ test('JsonFilter should contain equals and not', async () => {
"name": "field",
},
],
"isOneOf": false,
"name": "UserUpdateInput",
},
Object {
Expand Down Expand Up @@ -214,6 +217,7 @@ test('JsonFilter should contain equals and not', async () => {
"name": "field",
},
],
"isOneOf": false,
"name": "UserUpdateManyMutationInput",
},
Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ datasource db {
}

generator client {
provider = "prisma-client-js"
output = "@prisma/client"
featureFlags = ["aggregations"]
provider = "prisma-client-js"
output = "@prisma/client"
experimentalFeatures = ["aggregateApi"]
}

// / User model comment
Expand Down
110 changes: 65 additions & 45 deletions src/packages/client/src/generation/TSClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
GraphQLScalarToJSTypeTable,
lowerCase,
JSOutputTypeToInputType,
getOutputTypeName,
} from '../runtime/utils/common'
import { InternalDatasource } from '../runtime/utils/printDatasources'
import { DatasourceOverwrite } from './extractSqliteSources'
Expand Down Expand Up @@ -358,7 +357,7 @@ ${/*new Query(this.dmmf, 'query')*/ ''}
${this.dmmf.schema.enums.map((type) => new Enum(type).toTS()).join('\n\n')}
${Object.values(this.dmmf.modelMap)
.map((model) => new Model(model, this.dmmf).toTS())
.map((model) => new Model(model, this.dmmf, this.options.generator!).toTS())
.join('\n')}
/**
Expand Down Expand Up @@ -701,6 +700,7 @@ export class Model implements Generatable {
constructor(
protected readonly model: DMMF.Model,
protected readonly dmmf: DMMFClass,
protected readonly generator?: GeneratorConfig,
) {
const outputType = dmmf.outputTypeMap[model.name]
this.outputType = new OutputType(outputType)
Expand Down Expand Up @@ -739,14 +739,8 @@ export class Model implements Generatable {

return argsTypes
}
public toTS(): string {
const { model, outputType } = this

if (!outputType) {
return ''
}

const hasRelationField = model.fields.some((f) => f.kind === 'object')
private getAggregationTypes() {
const { model } = this
const aggregateType = this.dmmf.outputTypeMap[getAggregateName(model.name)]
const aggregateTypes = [aggregateType]

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

const includeType = hasRelationField
? `\nexport type ${getIncludeName(model.name)} = {
${indent(
outputType.fields
.filter((f) => f.outputType.kind === 'object')
.map(
(f) =>
`${f.name}?: boolean` +
(f.outputType.kind === 'object' ? ` | ${getFieldArgName(f)}` : ''),
)
.join('\n'),
tab,
)}
}\n`
: ''

return `
/**
* Model ${model.name}
*/
export type ${model.name} = {
${indent(
model.fields
.filter((f) => f.kind !== 'object')
.map((field) => new OutputField(field).toTS())
.join('\n'),
tab,
)}
}
${aggregateTypes.map((type) => new SchemaOutputType(type).toTS()).join('\n')}
return `${aggregateTypes
.map((type) => new SchemaOutputType(type).toTS())
.join('\n')}
${
aggregateTypes.length > 1
Expand Down Expand Up @@ -863,6 +828,54 @@ ${
}`
: ''
}
`
}
public toTS(): string {
const { model, outputType } = this

if (!outputType) {
return ''
}

const hasRelationField = model.fields.some((f) => f.kind === 'object')

const includeType = hasRelationField
? `\nexport type ${getIncludeName(model.name)} = {
${indent(
outputType.fields
.filter((f) => f.outputType.kind === 'object')
.map(
(f) =>
`${f.name}?: boolean` +
(f.outputType.kind === 'object' ? ` | ${getFieldArgName(f)}` : ''),
)
.join('\n'),
tab,
)}
}\n`
: ''

return `
/**
* Model ${model.name}
*/
export type ${model.name} = {
${indent(
model.fields
.filter((f) => f.kind !== 'object')
.map((field) => new OutputField(field).toTS())
.join('\n'),
tab,
)}
}
${
this.generator?.experimentalFeatures.includes('aggregateApi')
? this.getAggregationTypes()
: ''
}
export type ${getSelectName(model.name)} = {
${indent(
Expand All @@ -879,7 +892,7 @@ ${indent(
${includeType}
${new PayloadType(this.outputType!).toTS()}
${new ModelDelegate(this.outputType!, this.dmmf).toTS()}
${new ModelDelegate(this.outputType!, this.dmmf, this.generator).toTS()}
// Custom InputTypes
${this.argsTypes.map(TS).join('\n')}
Expand Down Expand Up @@ -1052,6 +1065,7 @@ export class ModelDelegate implements Generatable {
constructor(
protected readonly outputType: OutputType,
protected readonly dmmf: DMMFClass,
protected readonly generator?: GeneratorConfig,
) {}
public toTS(): string {
const { fields, name } = this.outputType
Expand Down Expand Up @@ -1092,14 +1106,20 @@ ${actionName}<T extends ${getModelArgName(name, actionName)}>(
DMMF.ModelAction.findMany,
)}, 'select' | 'include'>): Promise<number>
${
this.generator?.experimentalFeatures?.includes('aggregateApi')
? `
/**
* Aggregate
*/
aggregate<T extends ${getAggregateArgsName(
name,
)}>(args: Subset<T, ${getAggregateArgsName(
name,
)}>): Promise<${getAggregateGetName(name)}<T>>
name,
)}>): Promise<${getAggregateGetName(name)}<T>>
`
: ''
}
}
/**
Expand Down
2 changes: 1 addition & 1 deletion src/packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"scripts"
],
"prisma": {
"version": "ed51c9476579e3aa52ec43079fb2a351c70f5bb7"
"version": "83ac782d5d93dcee37efeba8ccbeff596701148a"
},
"devDependencies": {
"@types/jest": "25.2.3",
Expand Down

0 comments on commit 2c90a7f

Please sign in to comment.