Skip to content

Commit 55e5cd5

Browse files
committed
feat: Generate other output types
1 parent 42d143d commit 55e5cd5

26 files changed

+503
-170
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ npx prisma generate
4040
- `{feature}` - model name in dashed case or 'prisma' if unknown
4141
- `{name}` - name of model/input/arg
4242
- `{dasherizedName}` - dashed-case name of model/input/arg without suffix
43-
- `{type}` - short type name (model, input)
43+
- `{type}` - short type name (model, input, args, output)
4444
- `combineScalarFilters` - Combine nested/nullable scalar filters to single
4545
(default: `true`)
4646
- `atomicNumberOperations` - Atomic number operations,

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
},
5050
"dependencies": {
5151
"@prisma/generator-helper": "^2.6.2",
52-
"boolean": "^3.0.1",
5352
"get-relative-path": "^1.0.2",
5453
"pupa": "^2.0.1",
5554
"to-kebab": "^1.0.7",
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Field, Int, ObjectType } from '@nestjs/graphql';
2+
3+
import { ArticleAvgAggregate } from './article-avg-aggregate.output';
4+
import { ArticleMaxAggregate } from './article-max-aggregate.output';
5+
import { ArticleMinAggregate } from './article-min-aggregate.output';
6+
import { ArticleSumAggregate } from './article-sum-aggregate.output';
7+
8+
@ObjectType()
9+
export class AggregateArticle {
10+
@Field(() => Int, {
11+
nullable: true,
12+
description: undefined,
13+
})
14+
count?: number | null;
15+
16+
@Field(() => ArticleAvgAggregate, {
17+
nullable: true,
18+
description: undefined,
19+
})
20+
avg?: ArticleAvgAggregate | null;
21+
22+
@Field(() => ArticleSumAggregate, {
23+
nullable: true,
24+
description: undefined,
25+
})
26+
sum?: ArticleSumAggregate | null;
27+
28+
@Field(() => ArticleMinAggregate, {
29+
nullable: true,
30+
description: undefined,
31+
})
32+
min?: ArticleMinAggregate | null;
33+
34+
@Field(() => ArticleMaxAggregate, {
35+
nullable: true,
36+
description: undefined,
37+
})
38+
max?: ArticleMaxAggregate | null;
39+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Field, Float, ObjectType } from '@nestjs/graphql';
2+
3+
@ObjectType()
4+
export class ArticleAvgAggregate {
5+
@Field(() => Float, {
6+
nullable: true,
7+
description: undefined,
8+
})
9+
favoritesCount?: number | null;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Field, Int, ObjectType } from '@nestjs/graphql';
2+
3+
@ObjectType()
4+
export class ArticleMaxAggregate {
5+
@Field(() => Int, {
6+
nullable: true,
7+
description: undefined,
8+
})
9+
favoritesCount?: number | null;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Field, Int, ObjectType } from '@nestjs/graphql';
2+
3+
@ObjectType()
4+
export class ArticleMinAggregate {
5+
@Field(() => Int, {
6+
nullable: true,
7+
description: undefined,
8+
})
9+
favoritesCount?: number | null;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Field, Int, ObjectType } from '@nestjs/graphql';
2+
3+
@ObjectType()
4+
export class ArticleSumAggregate {
5+
@Field(() => Int, {
6+
nullable: true,
7+
description: undefined,
8+
})
9+
favoritesCount?: number | null;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Field, Int, ObjectType } from '@nestjs/graphql';
2+
3+
@ObjectType()
4+
export class AggregateComment {
5+
@Field(() => Int, {
6+
nullable: true,
7+
description: undefined,
8+
})
9+
count?: number | null;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Field, Int, ObjectType } from '@nestjs/graphql';
2+
3+
@ObjectType()
4+
export class BatchPayload {
5+
@Field(() => Int, {
6+
nullable: true,
7+
description: undefined,
8+
})
9+
count?: number | null;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Field, Int, ObjectType } from '@nestjs/graphql';
2+
3+
@ObjectType()
4+
export class AggregateTag {
5+
@Field(() => Int, {
6+
nullable: true,
7+
description: undefined,
8+
})
9+
count?: number | null;
10+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Field, Int, ObjectType } from '@nestjs/graphql';
2+
3+
import { UserAvgAggregate } from './user-avg-aggregate.output';
4+
import { UserMaxAggregate } from './user-max-aggregate.output';
5+
import { UserMinAggregate } from './user-min-aggregate.output';
6+
import { UserSumAggregate } from './user-sum-aggregate.output';
7+
8+
@ObjectType()
9+
export class AggregateUser {
10+
@Field(() => Int, {
11+
nullable: true,
12+
description: undefined,
13+
})
14+
count?: number | null;
15+
16+
@Field(() => UserAvgAggregate, {
17+
nullable: true,
18+
description: undefined,
19+
})
20+
avg?: UserAvgAggregate | null;
21+
22+
@Field(() => UserSumAggregate, {
23+
nullable: true,
24+
description: undefined,
25+
})
26+
sum?: UserSumAggregate | null;
27+
28+
@Field(() => UserMinAggregate, {
29+
nullable: true,
30+
description: undefined,
31+
})
32+
min?: UserMinAggregate | null;
33+
34+
@Field(() => UserMaxAggregate, {
35+
nullable: true,
36+
description: undefined,
37+
})
38+
max?: UserMaxAggregate | null;
39+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Field, Float, ObjectType } from '@nestjs/graphql';
2+
3+
@ObjectType()
4+
export class UserAvgAggregate {
5+
@Field(() => Float, {
6+
nullable: true,
7+
description: undefined,
8+
})
9+
countComments?: number | null;
10+
11+
@Field(() => Float, {
12+
nullable: true,
13+
description: undefined,
14+
})
15+
rating?: number | null;
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Field, Float, Int, ObjectType } from '@nestjs/graphql';
2+
3+
@ObjectType()
4+
export class UserMaxAggregate {
5+
@Field(() => Int, {
6+
nullable: true,
7+
description: undefined,
8+
})
9+
countComments?: number | null;
10+
11+
@Field(() => Float, {
12+
nullable: true,
13+
description: undefined,
14+
})
15+
rating?: number | null;
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Field, Float, Int, ObjectType } from '@nestjs/graphql';
2+
3+
@ObjectType()
4+
export class UserMinAggregate {
5+
@Field(() => Int, {
6+
nullable: true,
7+
description: undefined,
8+
})
9+
countComments?: number | null;
10+
11+
@Field(() => Float, {
12+
nullable: true,
13+
description: undefined,
14+
})
15+
rating?: number | null;
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Field, Float, Int, ObjectType } from '@nestjs/graphql';
2+
3+
@ObjectType()
4+
export class UserSumAggregate {
5+
@Field(() => Int, {
6+
nullable: true,
7+
description: undefined,
8+
})
9+
countComments?: number | null;
10+
11+
@Field(() => Float, {
12+
nullable: true,
13+
description: undefined,
14+
})
15+
rating?: number | null;
16+
}

src/example/user/user.resolver.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { PrismaClient } from '@prisma/client';
44
import * as Prisma from '@prisma/client';
55

66
import { AggregateUserArgs } from '../../@generated/user/aggregate-user.args';
7+
import { AggregateUser } from '../../@generated/user/aggregate-user.output';
78
import { UserUpdateInput } from '../../@generated/user/user-update.input';
89
import { UserWhereInput } from '../../@generated/user/user-where.input';
910
import { User } from '../../@generated/user/user.model';
@@ -48,10 +49,11 @@ export class UserResolver {
4849
return null;
4950
}
5051

51-
@Query(() => [User])
52+
@Query(() => AggregateUser)
5253
async userAggregate(@Args() args: AggregateUserArgs, @Info() info) {
5354
console.log('args', args);
5455
const result = await prisma.user.aggregate(args as Prisma.AggregateUserArgs);
5556
console.log('result', result);
57+
return result;
5658
}
5759
}

src/feature-name.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
type FeatureNameArgs = {
2+
name: string;
3+
models: string[];
4+
fallback: string;
5+
};
6+
7+
export function featureName(args: FeatureNameArgs) {
8+
const { name, models } = args;
9+
let result = args.fallback;
10+
for (const keyword of splitKeywords) {
11+
const [test] = name.split(keyword, 1);
12+
if (models.includes(test)) {
13+
result = test;
14+
return result;
15+
}
16+
}
17+
for (const keyword of endsWithKeywords) {
18+
const [test] = name.split(keyword).slice(-1);
19+
if (models.includes(test)) {
20+
result = test;
21+
return result;
22+
}
23+
}
24+
for (const [start, end] of middleKeywords) {
25+
const test = name.slice(start.length).slice(0, -end.length);
26+
if (models.includes(test)) {
27+
result = test;
28+
return result;
29+
}
30+
}
31+
return result;
32+
}
33+
34+
const splitKeywords = [
35+
'CreateInput',
36+
'CreateMany',
37+
'CreateOneWithout',
38+
'CreateWithout',
39+
'DistinctField',
40+
'Filter',
41+
'ManyWithout',
42+
'OrderByInput',
43+
'RelationFilter',
44+
'ListRelationFilter',
45+
'ScalarWhereInput',
46+
'UpdateInput',
47+
'UpdateMany',
48+
'UpdateOneRequiredWithout',
49+
'UpdateOneWithout',
50+
'UpdateWith',
51+
'UpsertWith',
52+
'UpsertWithout',
53+
'WhereInput',
54+
'WhereUniqueInput',
55+
'AvgAggregate',
56+
'SumAggregate',
57+
'MinAggregate',
58+
'MaxAggregate',
59+
].sort((a, b) => b.length - a.length);
60+
61+
const endsWithKeywords = [
62+
'Aggregate',
63+
'aggregate',
64+
'createOne',
65+
'deleteMany',
66+
'deleteOne',
67+
'findMany',
68+
'findOne',
69+
'updateMany',
70+
'updateOne',
71+
'upsertOne',
72+
];
73+
74+
const middleKeywords = [
75+
['Aggregate', 'Args'],
76+
['CreateOne', 'Args'],
77+
['DeleteMany', 'Args'],
78+
['DeleteOne', 'Args'],
79+
['FindMany', 'Args'],
80+
['FindOne', 'Args'],
81+
['UpdateMany', 'Args'],
82+
['UpdateOne', 'Args'],
83+
['UpsertOne', 'Args'],
84+
];

0 commit comments

Comments
 (0)