11/* eslint-disable @typescript-eslint/ban-ts-comment */
2- import type { DMMF as PrismaDMMF } from '@prisma/generator-helper' ;
2+ import type { DMMF , DMMF as PrismaDMMF } from '@prisma/generator-helper' ;
33import { Model } from '@zenstackhq/language/ast' ;
44import { AUXILIARY_FIELDS , getPrismaClientImportSpec , getPrismaVersion } from '@zenstackhq/sdk' ;
55import { checkModelHasModelRelation , findModelByName , isAggregateInputType } from '@zenstackhq/sdk/dmmf-helpers' ;
6- import indentString from '@zenstackhq/sdk/utils' ;
6+ import { indentString } from '@zenstackhq/sdk/utils' ;
77import path from 'path' ;
88import * as semver from 'semver' ;
99import { Project } from 'ts-morph' ;
@@ -28,6 +28,7 @@ export default class Transformer {
2828 private hasDecimal = false ;
2929 private project : Project ;
3030 private zmodel : Model ;
31+ private inputObjectTypes : DMMF . InputType [ ] ;
3132
3233 constructor ( params : TransformerParams ) {
3334 this . originalName = params . name ?? '' ;
@@ -39,6 +40,7 @@ export default class Transformer {
3940 this . enumTypes = params . enumTypes ?? [ ] ;
4041 this . project = params . project ;
4142 this . zmodel = params . zmodel ;
43+ this . inputObjectTypes = params . inputObjectTypes ;
4244 }
4345
4446 static setOutputPath ( outPath : string ) {
@@ -420,6 +422,13 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
420422 let codeBody = '' ;
421423 const operations : [ string , string ] [ ] = [ ] ;
422424
425+ // OrderByWithRelationInput's name is different when "fullTextSearch" is enabled
426+ const orderByWithRelationInput = this . inputObjectTypes
427+ . map ( ( o ) => upperCaseFirst ( o . name ) )
428+ . includes ( `${ modelName } OrderByWithRelationInput` )
429+ ? `${ modelName } OrderByWithRelationInput`
430+ : `${ modelName } OrderByWithRelationAndSearchRelevanceInput` ;
431+
423432 if ( findUnique ) {
424433 imports . push (
425434 `import { ${ modelName } WhereUniqueInputObjectSchema } from '../objects/${ modelName } WhereUniqueInput.schema'`
@@ -431,22 +440,22 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
431440 if ( findFirst ) {
432441 imports . push (
433442 `import { ${ modelName } WhereInputObjectSchema } from '../objects/${ modelName } WhereInput.schema'` ,
434- `import { ${ modelName } OrderByWithRelationInputObjectSchema } from '../objects/${ modelName } OrderByWithRelationInput .schema'` ,
443+ `import { ${ orderByWithRelationInput } ObjectSchema } from '../objects/${ orderByWithRelationInput } .schema'` ,
435444 `import { ${ modelName } WhereUniqueInputObjectSchema } from '../objects/${ modelName } WhereUniqueInput.schema'` ,
436445 `import { ${ modelName } ScalarFieldEnumSchema } from '../enums/${ modelName } ScalarFieldEnum.schema'`
437446 ) ;
438- codeBody += `findFirst: z.object({ ${ selectZodSchemaLineLazy } ${ includeZodSchemaLineLazy } where: ${ modelName } WhereInputObjectSchema.optional(), orderBy: z.union([${ modelName } OrderByWithRelationInputObjectSchema , ${ modelName } OrderByWithRelationInputObjectSchema .array()]).optional(), cursor: ${ modelName } WhereUniqueInputObjectSchema.optional(), take: z.number().optional(), skip: z.number().optional(), distinct: z.array(${ modelName } ScalarFieldEnumSchema).optional() }),` ;
447+ codeBody += `findFirst: z.object({ ${ selectZodSchemaLineLazy } ${ includeZodSchemaLineLazy } where: ${ modelName } WhereInputObjectSchema.optional(), orderBy: z.union([${ orderByWithRelationInput } ObjectSchema , ${ orderByWithRelationInput } ObjectSchema .array()]).optional(), cursor: ${ modelName } WhereUniqueInputObjectSchema.optional(), take: z.number().optional(), skip: z.number().optional(), distinct: z.array(${ modelName } ScalarFieldEnumSchema).optional() }),` ;
439448 operations . push ( [ 'findFirst' , origModelName ] ) ;
440449 }
441450
442451 if ( findMany ) {
443452 imports . push (
444453 `import { ${ modelName } WhereInputObjectSchema } from '../objects/${ modelName } WhereInput.schema'` ,
445- `import { ${ modelName } OrderByWithRelationInputObjectSchema } from '../objects/${ modelName } OrderByWithRelationInput .schema'` ,
454+ `import { ${ orderByWithRelationInput } ObjectSchema } from '../objects/${ orderByWithRelationInput } .schema'` ,
446455 `import { ${ modelName } WhereUniqueInputObjectSchema } from '../objects/${ modelName } WhereUniqueInput.schema'` ,
447456 `import { ${ modelName } ScalarFieldEnumSchema } from '../enums/${ modelName } ScalarFieldEnum.schema'`
448457 ) ;
449- codeBody += `findMany: z.object({ ${ selectZodSchemaLineLazy } ${ includeZodSchemaLineLazy } where: ${ modelName } WhereInputObjectSchema.optional(), orderBy: z.union([${ modelName } OrderByWithRelationInputObjectSchema , ${ modelName } OrderByWithRelationInputObjectSchema .array()]).optional(), cursor: ${ modelName } WhereUniqueInputObjectSchema.optional(), take: z.number().optional(), skip: z.number().optional(), distinct: z.array(${ modelName } ScalarFieldEnumSchema).optional() }),` ;
458+ codeBody += `findMany: z.object({ ${ selectZodSchemaLineLazy } ${ includeZodSchemaLineLazy } where: ${ modelName } WhereInputObjectSchema.optional(), orderBy: z.union([${ orderByWithRelationInput } ObjectSchema , ${ orderByWithRelationInput } ObjectSchema .array()]).optional(), cursor: ${ modelName } WhereUniqueInputObjectSchema.optional(), take: z.number().optional(), skip: z.number().optional(), distinct: z.array(${ modelName } ScalarFieldEnumSchema).optional() }),` ;
450459 operations . push ( [ 'findMany' , origModelName ] ) ;
451460 }
452461
@@ -557,11 +566,11 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
557566 if ( aggregate ) {
558567 imports . push (
559568 `import { ${ modelName } WhereInputObjectSchema } from '../objects/${ modelName } WhereInput.schema'` ,
560- `import { ${ modelName } OrderByWithRelationInputObjectSchema } from '../objects/${ modelName } OrderByWithRelationInput .schema'` ,
569+ `import { ${ orderByWithRelationInput } ObjectSchema } from '../objects/${ orderByWithRelationInput } .schema'` ,
561570 `import { ${ modelName } WhereUniqueInputObjectSchema } from '../objects/${ modelName } WhereUniqueInput.schema'`
562571 ) ;
563572
564- codeBody += `aggregate: z.object({ where: ${ modelName } WhereInputObjectSchema.optional(), orderBy: z.union([${ modelName } OrderByWithRelationInputObjectSchema , ${ modelName } OrderByWithRelationInputObjectSchema .array()]).optional(), cursor: ${ modelName } WhereUniqueInputObjectSchema.optional(), take: z.number().optional(), skip: z.number().optional(), ${ aggregateOperations . join (
573+ codeBody += `aggregate: z.object({ where: ${ modelName } WhereInputObjectSchema.optional(), orderBy: z.union([${ orderByWithRelationInput } ObjectSchema , ${ orderByWithRelationInput } ObjectSchema .array()]).optional(), cursor: ${ modelName } WhereUniqueInputObjectSchema.optional(), take: z.number().optional(), skip: z.number().optional(), ${ aggregateOperations . join (
565574 ', '
566575 ) } }),`;
567576 operations . push ( [ 'aggregate' , modelName ] ) ;
0 commit comments