Skip to content

Commit

Permalink
feat(client): add more jsdoc to Prisma Client (#4388)
Browse files Browse the repository at this point in the history
  • Loading branch information
williamluke4 committed Dec 17, 2020
1 parent e490fdf commit 02e8011
Show file tree
Hide file tree
Showing 8 changed files with 1,941 additions and 365 deletions.

Large diffs are not rendered by default.

15 changes: 2 additions & 13 deletions src/packages/client/src/generation/TSClient/Args.ts
@@ -1,8 +1,7 @@
import indent from 'indent-string'
import { Generatable } from './Generatable'
import { DMMF } from '../../runtime/dmmf-types'
import { ExportCollector, topLevelArgsJsDocs } from './helpers'
import pluralize from 'pluralize'
import { ExportCollector, getArgFieldJSDoc } from './helpers'
import { getIncludeName, getModelArgName, getSelectName } from '../utils'
import { InputField } from './Input'
import { TAB_SIZE } from './constants'
Expand All @@ -18,18 +17,8 @@ export class ArgsType implements Generatable {
const { action, args } = this
const { name } = this.model

const singular = name
const plural = pluralize(name)

for (const arg of args) {
if (
action &&
topLevelArgsJsDocs[action] &&
topLevelArgsJsDocs[action][arg.name]
) {
const comment = topLevelArgsJsDocs[action][arg.name](singular, plural)
arg.comment = comment
}
arg.comment = getArgFieldJSDoc(this.model, action, arg)
}

const selectName = getSelectName(name)
Expand Down
2 changes: 1 addition & 1 deletion src/packages/client/src/generation/TSClient/Input.ts
@@ -1,9 +1,9 @@
import indent from 'indent-string'
import { DMMF } from '../../runtime/dmmf-types'
import {
argIsInputType,
GraphQLScalarToJSTypeTable,
JSOutputTypeToInputType,
argIsInputType,
} from '../../runtime/utils/common'
import { uniqueBy } from '../../runtime/utils/uniqueBy'
import { TAB_SIZE } from './constants'
Expand Down
54 changes: 26 additions & 28 deletions src/packages/client/src/generation/TSClient/Model.ts
Expand Up @@ -26,7 +26,7 @@ import {
} from '../utils'
import { ArgsType, MinimalArgsType } from './Args'
import { Generatable, TS } from './Generatable'
import { ExportCollector, getMethodJSDoc } from './helpers'
import { ExportCollector, getArgFieldJSDoc, getArgs, getGenericMethod, getMethodJSDoc, wrapComment } from './helpers'
import { InputType } from './Input'
import { ModelOutputField, OutputType } from './Output'
import { SchemaOutputType } from './SchemaOutput'
Expand Down Expand Up @@ -76,7 +76,7 @@ export class Model implements Generatable {
this.collector,
),
)
} else if (action !== 'groupBy') {
} else if (action !== 'groupBy' && action !== 'aggregate') {
argsTypes.push(
new ArgsType(
field.args,
Expand Down Expand Up @@ -115,7 +115,10 @@ export class Model implements Generatable {
export type ${groupByArgsName} = {
${indent(
groupByRootField.args
.map((arg) => new InputField(arg, false, arg.name === 'by').toTS())
.map((arg) => {
arg.comment = getArgFieldJSDoc(model, DMMF.ModelAction.groupBy, arg)
return new InputField(arg, false, arg.name === 'by').toTS()
})
.concat(
groupByType.fields
.filter((f) => f.outputType.location === 'outputObjectTypes')
Expand Down Expand Up @@ -252,15 +255,23 @@ ${
export type ${aggregateArgsName} = {
${indent(
aggregateRootField.args
.map((arg) => new InputField(arg).toTS())
.map((arg) => {
arg.comment = getArgFieldJSDoc(model, DMMF.ModelAction.aggregate, arg)
return new InputField(arg).toTS()
})
.concat(
aggregateType.fields.map((f) => {
let data = ''
const comment = getArgFieldJSDoc(model, DMMF.ModelAction.aggregate, f.name)
data += comment ? wrapComment(comment) + '\n' : ''
if (f.name === 'count') {
return `${f.name}?: true`
}
return `${f.name}?: ${getAggregateInputType(
(f.outputType.type as DMMF.OutputType).name,
)}`
data +=`${f.name}?: true`
} else {
data +=`${f.name}?: ${getAggregateInputType(
(f.outputType.type as DMMF.OutputType).name,
)}`
}
return data
}),
)
.join('\n'),
Expand Down Expand Up @@ -395,41 +406,30 @@ export class ModelDelegate implements Generatable {
)
const previewFeatures = this.generator?.previewFeatures ?? []
const groupByEnabled = previewFeatures.includes('groupBy')

// TODO: The following code needs to be split up and is a mess
return `\
export interface ${name}Delegate {
${indent(
actions
.map(
([actionName]: [any, any]): string =>
`${getMethodJSDoc(actionName, mapping, model)}
${actionName}<T extends ${getModelArgName(name, actionName)}>(
args${
actionName === DMMF.ModelAction.findMany ||
actionName === DMMF.ModelAction.findFirst ||
actionName === DMMF.ModelAction.deleteMany
? '?'
: ''
}: Subset<T, ${getModelArgName(name, actionName)}>
${actionName}${getGenericMethod(name, actionName)}(
${getArgs(name, actionName)}
): ${getSelectReturnType({ name, actionName, projection: Projection.select })}`,
)
.join('\n'),
TAB_SIZE,
)}
/**
* Count
*/
${getMethodJSDoc(DMMF.ModelAction.count, mapping, model)}
count(args?: Omit<${getModelArgName(
name,
DMMF.ModelAction.findMany,
)}, 'select' | 'include'>): Promise<number>
${
groupByEnabled
? `/**
* Group By
*/
? `
${getMethodJSDoc(DMMF.ModelAction.groupBy, mapping, model)}
groupBy<T extends ${getGroupByArgsName(
name,
)}>(args: Subset<T, ${getGroupByArgsName(
Expand All @@ -438,9 +438,7 @@ ${actionName}<T extends ${getModelArgName(name, actionName)}>(
: ``
}
/**
* Aggregate
*/
${getMethodJSDoc(DMMF.ModelAction.aggregate, mapping, model)}
aggregate<T extends ${getAggregateArgsName(
name,
)}>(args: Subset<T, ${getAggregateArgsName(
Expand Down

0 comments on commit 02e8011

Please sign in to comment.