1
1
/* 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' ;
3
3
import { Model } from '@zenstackhq/language/ast' ;
4
4
import { AUXILIARY_FIELDS , getPrismaClientImportSpec , getPrismaVersion } from '@zenstackhq/sdk' ;
5
5
import { checkModelHasModelRelation , findModelByName , isAggregateInputType } from '@zenstackhq/sdk/dmmf-helpers' ;
6
- import indentString from '@zenstackhq/sdk/utils' ;
6
+ import { indentString } from '@zenstackhq/sdk/utils' ;
7
7
import path from 'path' ;
8
8
import * as semver from 'semver' ;
9
9
import { Project } from 'ts-morph' ;
@@ -28,6 +28,7 @@ export default class Transformer {
28
28
private hasDecimal = false ;
29
29
private project : Project ;
30
30
private zmodel : Model ;
31
+ private inputObjectTypes : DMMF . InputType [ ] ;
31
32
32
33
constructor ( params : TransformerParams ) {
33
34
this . originalName = params . name ?? '' ;
@@ -39,6 +40,7 @@ export default class Transformer {
39
40
this . enumTypes = params . enumTypes ?? [ ] ;
40
41
this . project = params . project ;
41
42
this . zmodel = params . zmodel ;
43
+ this . inputObjectTypes = params . inputObjectTypes ;
42
44
}
43
45
44
46
static setOutputPath ( outPath : string ) {
@@ -420,6 +422,13 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
420
422
let codeBody = '' ;
421
423
const operations : [ string , string ] [ ] = [ ] ;
422
424
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
+
423
432
if ( findUnique ) {
424
433
imports . push (
425
434
`import { ${ modelName } WhereUniqueInputObjectSchema } from '../objects/${ modelName } WhereUniqueInput.schema'`
@@ -431,22 +440,22 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
431
440
if ( findFirst ) {
432
441
imports . push (
433
442
`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'` ,
435
444
`import { ${ modelName } WhereUniqueInputObjectSchema } from '../objects/${ modelName } WhereUniqueInput.schema'` ,
436
445
`import { ${ modelName } ScalarFieldEnumSchema } from '../enums/${ modelName } ScalarFieldEnum.schema'`
437
446
) ;
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() }),` ;
439
448
operations . push ( [ 'findFirst' , origModelName ] ) ;
440
449
}
441
450
442
451
if ( findMany ) {
443
452
imports . push (
444
453
`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'` ,
446
455
`import { ${ modelName } WhereUniqueInputObjectSchema } from '../objects/${ modelName } WhereUniqueInput.schema'` ,
447
456
`import { ${ modelName } ScalarFieldEnumSchema } from '../enums/${ modelName } ScalarFieldEnum.schema'`
448
457
) ;
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() }),` ;
450
459
operations . push ( [ 'findMany' , origModelName ] ) ;
451
460
}
452
461
@@ -557,11 +566,11 @@ export const ${this.name}ObjectSchema: SchemaType = ${schema} as SchemaType;`;
557
566
if ( aggregate ) {
558
567
imports . push (
559
568
`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'` ,
561
570
`import { ${ modelName } WhereUniqueInputObjectSchema } from '../objects/${ modelName } WhereUniqueInput.schema'`
562
571
) ;
563
572
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 (
565
574
', '
566
575
) } }),`;
567
576
operations . push ( [ 'aggregate' , modelName ] ) ;
0 commit comments