import indent from 'indent-string' ; import path from 'path' ; import type { DMMFClass } from '../runtime/dmmf' ; import { DMMF } from '../runtime/dmmf-types' ; export enum Projection { select = 'select' , include = 'include' } export function getScalarsName ( modelName ) { return `${ modelName }Scalars` ; } export function getPayloadName ( modelName ) { return `${ modelName }GetPayload` ; } export function getSelectName ( modelName ) { return `${ modelName }Select` ; } export function getAggregateName ( modelName ) { return `Aggregate${ capitalize ( modelName ) }` ; } export function getGroupByName ( modelName ) { return `${ capitalize ( modelName ) }GroupByOutputType` ; } export function getAvgAggregateName ( modelName ) { return `${ capitalize ( modelName ) }AvgAggregateOutputType` ; } export function getSumAggregateName ( modelName ) { return `${ capitalize ( modelName ) }SumAggregateOutputType` ; } export function getMinAggregateName ( modelName ) { return `${ capitalize ( modelName ) }MinAggregateOutputType` ; } export function getMaxAggregateName ( modelName ) { return `${ capitalize ( modelName ) }MaxAggregateOutputType` ; } export function getCountAggregateInputName ( modelName ) { return `${ capitalize ( modelName ) }CountAggregateInputType` ; } export function getCountAggregateOutputName ( modelName ) { return `${ capitalize ( modelName ) }CountAggregateOutputType` ; } export function getAggregateInputType ( aggregateOutputType ) { return aggregateOutputType . replace ( /OutputType$/ , 'InputType' ) ; } export function getGroupByArgsName ( modelName ) { return `${ capitalize ( modelName ) }GroupByArgs` ; } export function getGroupByPayloadName ( modelName ) { return `Get${ capitalize ( modelName ) }GroupByPayload` ; } export function getAggregateArgsName ( modelName ) { return `${ capitalize ( modelName ) }AggregateArgs` ; } export function getAggregateGetName ( modelName ) { return `Get${ capitalize ( modelName ) }AggregateType` ; } export function getAggregateScalarGetName ( modelName ) { return `Get${ capitalize ( modelName ) }AggregateScalarType` ; } export function getIncludeName ( modelName ) { return `${ modelName }Include` ; } export function getDefaultName ( modelName ) { return `${ modelName }Default` ; } export function getFieldArgName ( field ) { return getArgName ( ( field . outputType . type as DMMF . OutputType ) . name , field . outputType . isList ) ; } export function getArgName ( name , isList ) { if ( ! isList ) { return `${ name }Args` ; } return `${ name }FindManyArgs` ; } export function getModelArgName ( modelName , action ? ) { if ( ! action ) { return `${ modelName }Args` ; } switch ( action ) { case DMMF . ModelAction . findMany : return `${ modelName }FindManyArgs` ; case DMMF . ModelAction . findUnique : return `${ modelName }FindUniqueArgs` ; case DMMF . ModelAction . findFirst : return `${ modelName }FindFirstArgs` ; case DMMF . ModelAction . upsert : return `${ modelName }UpsertArgs` ; case DMMF . ModelAction . update : return `${ modelName }UpdateArgs` ; case DMMF . ModelAction . updateMany : return `${ modelName }UpdateManyArgs` ; case DMMF . ModelAction . delete : return `${ modelName }DeleteArgs` ; case DMMF . ModelAction . create : return `${ modelName }CreateArgs` ; case DMMF . ModelAction . createMany : return `${ modelName }CreateManyArgs` ; case DMMF . ModelAction . deleteMany : return `${ modelName }DeleteManyArgs` ; case DMMF . ModelAction . groupBy : return `${ modelName }GroupByArgs` ; case DMMF . ModelAction . aggregate : return getAggregateArgsName ( modelName ) ; case DMMF . ModelAction . count : return `${ modelName }CountArgs` ; } } export function getDefaultArgName ( dmmf , modelName , action ) { const mapping = dmmf . mappings . modelOperations . find ( ( m ) => m . model === modelName ) ! ; const fieldName = mapping [ action ] ; const operation = getOperation ( action ) ; const queryType = operation === 'query' ? dmmf . queryType : dmmf . mutationType ; const field = queryType . fields . find ( ( f ) => f . name === fieldName ) ! ; return ( field . args [ 0 ] . inputTypes [ 0 ] . type as DMMF . InputType ) . name ; } export function getOperation ( action ) { if ( action === DMMF . ModelAction . findMany || action === DMMF . ModelAction . findUnique ) { return 'query' ; } return 'mutation' ; } param modelName param fieldName param mapping export function renderInitialClientArgs ( actionName , fieldName , mapping ) { return `
  dmmf,
  fetcher,
  '${ getOperation ( actionName ) }',
  '${ fieldName }',
  '${ mapping . plural }.${ actionName }',
  args || {},
  [],
  errorFormat,
  measurePerformance\n` ; } export function getFieldTypeName ( field ) { if ( typeof field . outputType . type === 'string' ) { return field . outputType . type ; } return field . outputType . type . name ; } export function getType ( name , isList , isOptional ? ) { return name + ( isList ? '[]' : '' ) + ( isOptional ? ' | null' : '' ) ; } export function getFieldType ( field ) { return getType ( getFieldTypeName ( field ) , field . outputType . isList ) ; } interface SelectReturnTypeOptions { name : string ; actionName : DMMF . ModelAction ; renderPromise ? : boolean ; hideCondition ? : boolean ; isField ? : boolean ; fieldName ? : string ; projection : Projection ; } param name param actionName export function getSelectReturnType ( { name , actionName , renderPromise = true , hideCondition = false , isField = false , } ) { if ( actionName === 'count' ) { return `Promise<number>` ; } if ( actionName === 'aggregate' ) return `Promise<${ getAggregateGetName ( name ) }<T>>` ; const isList = actionName === DMMF . ModelAction . findMany ; if ( actionName === 'deleteMany' || actionName === 'updateMany' || actionName === 'createMany' ) { return `PrismaPromise<BatchPayload>` ; } /**
     * Important: We handle findMany or isList special, as we don't want chaining from there
     */ if ( isList || hideCondition ) { const listOpen = isList ? 'Array<' : '' ; const listClose = isList ? '>' : '' ; const promiseOpen = renderPromise ? 'PrismaPromise<' : '' ; const promiseClose = renderPromise ? '>' : '' ; return `CheckSelect<T, ${ promiseOpen }${ listOpen }${ name }${ listClose }${ promiseClose }, ${ promiseOpen }${ listOpen }${ getPayloadName ( name ) }<T>${ listClose }${ promiseClose }>` ; } if ( actionName === 'findFirst' || actionName === 'findUnique' ) { if ( isField ) { return `CheckSelect<T, Prisma__${ name }Client<${ getType ( name , isList ) } | null >, Prisma__${ name }Client<${ getType ( getPayloadName ( name ) + '<T>' , isList ) } | null >>` ; } return `HasReject<GlobalRejectSettings, LocalRejectSettings, '${ actionName }', '${ name }'> extends True ? CheckSelect<T, Prisma__${ name }Client<${ getType ( name , isList ) }>, Prisma__${ name }Client<${ getType ( getPayloadName ( name ) + '<T>' , isList ) }>> : CheckSelect<T, Prisma__${ name }Client<${ getType ( name , isList ) } | null >, Prisma__${ name }Client<${ getType ( getPayloadName ( name ) + '<T>' , isList ) } | null >>` ; } return `CheckSelect<T, Prisma__${ name }Client<${ getType ( name , isList ) }>, Prisma__${ name }Client<${ getType ( getPayloadName ( name ) + '<T>' , isList ) }>>` ; } export function isQueryAction ( action , operation ) { if ( ! ( action in DMMF . ModelAction ) ) { return false ; } const result = action === DMMF . ModelAction . findUnique || action === DMMF . ModelAction . findMany ; return operation === 'query' ? result : ! result ; } export function capitalize ( str ) { return str [ 0 ] . toUpperCase ( ) + str . slice ( 1 ) ; } export function indentAllButFirstLine ( str , indentation ) { const lines = str . split ( '\n' ) ; return lines [ 0 ] + '\n' + indent ( lines . slice ( 1 ) . join ( '\n' ) , indentation ) ; } export function getRelativePathResolveStatement ( outputDir , cwd ? ) { if ( ! cwd ) { return 'undefined' ; } return `path.resolve(__dirname, ${ JSON . stringify ( path . relative ( outputDir , cwd ) ) })` ; } function flatten ( array ) { return Array . prototype . concat . apply ( [ ] , array ) ; } export function flatMap < T , U > ( array , callbackFn , thisArg ? ) { return flatten ( array . map ( callbackFn , thisArg ) ) ; } param arr export function unique < T > ( arr ) { const { length } = arr ; const result = [ ] ; const seen = new Set ( ) ; loop : for ( let i = 0 ; i < length ; i ++ ) { const value = arr [ i ] ; if ( seen . has ( value ) ) { continue loop ; } seen . add ( value ) ; result . push ( value ) ; } return result ; }