import pluralize from 'pluralize' ; import { DMMF } from '../../runtime/dmmf-types' ; import { capitalize , lowerCase } from '../../runtime/utils/common' ; import { getAggregateArgsName , getModelArgName , unique } from '../utils' ; import type { JSDocMethodBodyCtx } from './jsdoc' ; import { JSDocs } from './jsdoc' ; export function getMethodJSDocBody ( action , mapping , model ) { const ctx = { singular : capitalize ( mapping . model ) , plural : capitalize ( mapping . plural ) , firstScalar : model . fields . find ( ( f ) => f . kind === 'scalar' ) , method : `prisma.${ lowerCase ( mapping . model ) }.${ action }` , action , mapping , model , } ; const jsdoc = JSDocs [ action ] ?. body ( ctx ) ; return jsdoc ? jsdoc : '' ; } export function getMethodJSDoc ( action , mapping , model ) { return wrapComment ( getMethodJSDocBody ( action , mapping , model ) ) ; } export function getGenericMethod ( name , actionName ) { if ( actionName === 'count' ) { return '' ; } if ( actionName === 'aggregate' ) { return `<T extends ${ getAggregateArgsName ( name ) }>` ; } if ( actionName === 'findFirst' || actionName === 'findUnique' ) { return `<T extends ${ getModelArgName ( name , actionName ) },  LocalRejectSettings = T["rejectOnNotFound"] extends RejectOnNotFound ? T['rejectOnNotFound'] : undefined>` ; } const modelArgName = getModelArgName ( name , actionName ) ; if ( ! modelArgName ) { console . log ( { name , actionName } ) ; } return `<T extends ${ modelArgName }>` ; } export function getArgs ( name , actionName ) { if ( actionName === 'count' ) { return `args?: Omit<${ getModelArgName ( name , DMMF . ModelAction . findMany ) }, 'select' | 'include'>` ; } if ( actionName === 'aggregate' ) { return `args: Subset<T, ${ getAggregateArgsName ( name ) }>` ; } return `args${ actionName === DMMF . ModelAction . findMany || actionName === DMMF . ModelAction . findFirst || actionName === DMMF . ModelAction . deleteMany || actionName === DMMF . ModelAction . createMany ? '?' : '' }: SelectSubset<T, ${ getModelArgName ( name , actionName ) }>` ; } export function wrapComment ( str ) { return `/**\n${ str . split ( '\n' ) . map ( ( l ) => ' * ' + l ) . join ( '\n' ) }\n**/` ; } export function getArgFieldJSDoc ( type ? , action ? , field ? ) { if ( ! field || ! action || ! type ) return ; const fieldName = typeof field === 'string' ? field : field . name ; if ( JSDocs [ action ] && JSDocs [ action ] ?. fields [ fieldName ] ) { const singular = type . name ; const plural = pluralize ( type . name ) ; const comment = JSDocs [ action ] ?. fields [ fieldName ] ( singular , plural ) ; return comment as string ; } return undefined ; } export function escapeJson ( str ) { return str . replace ( /\\n/g , '\\\\n' ) . replace ( /\\r/g , '\\\\r' ) . replace ( /\\t/g , '\\\\t' ) ; } export class ExportCollector { symbols : string [ ] = [ ] ; addSymbol ( symbol ) { this . symbols . push ( symbol ) ; } getSymbols ( ) { return unique ( this . symbols ) ; } }