Skip to content

Commit

Permalink
Remove usages of ClientActions
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelff committed Nov 8, 2022
1 parent 807994f commit bb8cdea
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 52 deletions.
33 changes: 2 additions & 31 deletions packages/client/src/generation/TSClient/Args.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import indent from 'indent-string'

import { ClientModelAction } from '../../runtime/clientActions'
import { DMMF } from '../../runtime/dmmf-types'
import { lowerCase } from '../../runtime/utils/common'
import { GenericArgsInfo } from '../GenericsArgsInfo'
Expand All @@ -16,7 +15,7 @@ export class ArgsType implements Generatable {
protected readonly args: DMMF.SchemaArg[],
protected readonly type: DMMF.OutputType,
protected readonly genericsInfo: GenericArgsInfo,
protected readonly action?: ClientModelAction,
protected readonly action?: DMMF.ModelAction,
) {}
public toTS(): string {
const { action, args } = this
Expand Down Expand Up @@ -76,8 +75,6 @@ export class ArgsType implements Generatable {
const modelArgName = getModelArgName(name, action)
if (action === DMMF.ModelAction.findUnique || action === DMMF.ModelAction.findFirst) {
return this.generateFindMethodArgs(action, name, argsToGenerate, modelArgName)
} else if (action === 'findFirstOrThrow' || action === 'findUniqueOrThrow') {
return this.generateFindOrThrowMethodArgs(action, name, modelArgName)
}

return `
Expand Down Expand Up @@ -126,26 +123,6 @@ export interface ${modelArgName}${ifExtensions(
}
`
}

private generateFindOrThrowMethodArgs(
action: 'findFirstOrThrow' | 'findUniqueOrThrow',
name: string,
modelArgName: string,
) {
// here we are assuming that base type have been already generated by corresponding
// DMMF actions. At the moment orThrow methods do not have any extra arguments, so arg
// type is just an alias for a base type
const baseTypeName = getBaseTypeName(name, action)
return `
/**
* ${name}: ${action}
*/
export type ${modelArgName}${ifExtensions(
'<ExtArgs extends runtime.Types.Extensions.Args = never>',
'',
)} = ${baseTypeName}${ifExtensions('<ExtArgs>', '')}
`
}
}

export class MinimalArgsType implements Generatable {
Expand Down Expand Up @@ -183,19 +160,13 @@ ${indent(
}
}

type ActionWithBaseType =
| DMMF.ModelAction.findFirst
| DMMF.ModelAction.findUnique
| DMMF.ModelAction.findFirstOrThrow
| DMMF.ModelAction.findUniqueOrThrow
type ActionWithBaseType = DMMF.ModelAction.findFirst | DMMF.ModelAction.findUnique

function getBaseTypeName(modelName: string, action: ActionWithBaseType): string {
switch (action) {
case DMMF.ModelAction.findFirst:
case DMMF.ModelAction.findFirstOrThrow:
return `${modelName}FindFirstArgsBase`
case DMMF.ModelAction.findUnique:
case DMMF.ModelAction.findUniqueOrThrow:
return `${modelName}FindUniqueArgsBase`
}
}
29 changes: 8 additions & 21 deletions packages/client/src/generation/TSClient/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ import type { GeneratorConfig } from '@prisma/generator-helper'
import indent from 'indent-string'
import { klona } from 'klona'

import {
type ClientModelAction,
allClientModelActions,
clientOnlyActions,
getDmmfActionName,
} from '../../runtime/clientActions'
import type { DMMFHelper } from '../../runtime/dmmf'
import { DMMF } from '../../runtime/dmmf-types'
import { lowerCase } from '../../runtime/utils/common'
Expand Down Expand Up @@ -65,8 +59,8 @@ export class Model implements Generatable {
}
protected get argsTypes(): Generatable[] {
const argsTypes: Generatable[] = []
for (const action of allClientModelActions) {
const fieldName = this.rootFieldNameForAction(action)
for (const action of Object.keys(DMMF.ModelAction)) {
const fieldName = this.rootFieldNameForAction(action as DMMF.ModelAction)
if (!fieldName) {
continue
}
Expand All @@ -80,7 +74,7 @@ export class Model implements Generatable {
} else if (action === 'findRaw' || action === 'aggregateRaw') {
argsTypes.push(new MinimalArgsType(field.args, this.type, this.genericsInfo, action as DMMF.ModelAction))
} else if (action !== 'groupBy' && action !== 'aggregate') {
argsTypes.push(new ArgsType(field.args, this.type, this.genericsInfo, action as ClientModelAction))
argsTypes.push(new ArgsType(field.args, this.type, this.genericsInfo, action as DMMF.ModelAction))
}
}

Expand All @@ -89,8 +83,8 @@ export class Model implements Generatable {
return argsTypes
}

private rootFieldNameForAction(action: ClientModelAction) {
return this.mapping?.[getDmmfActionName(action)]
private rootFieldNameForAction(action: DMMF.ModelAction) {
return this.mapping?.[action]
}

private getGroupByTypes() {
Expand Down Expand Up @@ -375,16 +369,9 @@ export class ModelDelegate implements Generatable {
* @param availableActions
* @returns
*/
private getNonAggregateActions(availableActions: ClientModelAction[]): ClientModelAction[] {
const actions = availableActions.filter(
(key) => key !== 'aggregate' && key !== 'groupBy' && key !== 'count',
) as ClientModelAction[]

for (const [clientOnlyAction, { wrappedAction }] of Object.entries(clientOnlyActions)) {
if (actions.includes(wrappedAction as DMMF.ModelAction)) {
actions.push(clientOnlyAction as ClientModelAction)
}
}
private getNonAggregateActions(availableActions: DMMF.ModelAction[]): DMMF.ModelAction[] {
const actions = availableActions.filter((key) => key !== 'aggregate' && key !== 'groupBy' && key !== 'count')

return actions
}

Expand Down

0 comments on commit bb8cdea

Please sign in to comment.