Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: prisma typed select #347

Merged
merged 5 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"prisma:generate-client": "npx prisma generate --generator client",
"prisma:generate-client": "npx prisma generate --generator client && yarn prisma:generate-gql-select",
"prisma:generate-gql-select": "node scripts/generate-model-select-map.js",
"prisma:generate-nest-graphql": "npx prisma generate --generator nestgraphql && eslint \"src/core/@generated/**\" --fix",
"prisma:migrate": "npx prisma migrate deploy",
"prisma:seed": "npx prisma db seed",
Expand All @@ -39,6 +40,7 @@
"@nestjs/platform-express": "^9.0.0",
"@nestjs/serve-static": "^3.0.0",
"@nestjs/terminus": "^9.2.2",
"@paljs/plugins": "^5.3.3",
"@prisma/client": "^4.13.0",
"apollo-server-express": "^3.12.0",
"bcrypt": "^5.1.0",
Expand Down
32 changes: 32 additions & 0 deletions server/scripts/generate-model-select-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs');
const path = require('path');
const { getDMMF, getSchemaPath } = require('@prisma/internals');

async function generateTypes() {
const schemaPath = await getSchemaPath();
const dmmf = await getDMMF({
datamodel: fs.readFileSync(schemaPath, 'utf-8'),
});

let content =
'// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.\n';
content += "import { Prisma } from '@prisma/client';\n\n";
content += 'export type ModelSelectMap = {\n';

for (const model of dmmf.datamodel.models) {
content += ` ${model.name}: Prisma.${model.name}Select;\n`;
}

content += '};\n';

fs.writeFileSync(
path.join(__dirname, '../src/utils/prisma-select/model-select-map.ts'),
content,
);
}

generateTypes().catch((e) => {
console.error(e);
process.exit(1);
});
2 changes: 1 addition & 1 deletion server/src/ability/ability.factory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PureAbility, AbilityBuilder, subject } from '@casl/ability';
import { PureAbility, AbilityBuilder } from '@casl/ability';
import { createPrismaAbility, PrismaQuery, Subjects } from '@casl/prisma';
import { Injectable } from '@nestjs/common';
import {
Expand Down
4 changes: 0 additions & 4 deletions server/src/core/comment/comment.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Module } from '@nestjs/common';
import { CommentService } from './services/comment.service';
import { CommentResolver } from './resolvers/comment.resolver';
import { CommentRelationsResolver } from './resolvers/comment-relations.resolver';
import { CommentThreadTargetService } from './services/comment-thread-target.service';
import { CommentThreadResolver } from './resolvers/comment-thread.resolver';
import { CommentThreadRelationsResolver } from './resolvers/comment-thread-relations.resolver';
import { CommentThreadService } from './services/comment-thread.service';

@Module({
Expand All @@ -13,9 +11,7 @@ import { CommentThreadService } from './services/comment-thread.service';
CommentThreadService,
CommentThreadTargetService,
CommentResolver,
CommentRelationsResolver,
CommentThreadResolver,
CommentThreadRelationsResolver,
],
exports: [CommentService, CommentThreadService, CommentThreadTargetService],
})
Expand Down

This file was deleted.

22 changes: 0 additions & 22 deletions server/src/core/comment/resolvers/comment-relations.resolver.ts

This file was deleted.

This file was deleted.

This file was deleted.

29 changes: 22 additions & 7 deletions server/src/core/comment/resolvers/comment-thread.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { CommentThreadService } from '../services/comment-thread.service';
import { prepareFindManyArgs } from 'src/utils/prepare-find-many';
import { UpdateOneCommentThreadArgs } from 'src/core/@generated/comment-thread/update-one-comment-thread.args';
import { Prisma } from '@prisma/client';
import {
PrismaSelector,
PrismaSelect,
} from 'src/decorators/prisma-select.decorator';

@UseGuards(JwtAuthGuard)
@Resolver(() => CommentThread)
Expand All @@ -24,7 +28,9 @@ export class CommentThreadResolver {
async createOneCommentThread(
@Args() args: CreateOneCommentThreadArgs,
@AuthWorkspace() workspace: Workspace,
): Promise<CommentThread> {
@PrismaSelector({ modelName: 'CommentThread' })
prismaSelect: PrismaSelect<'CommentThread'>,
): Promise<Partial<CommentThread>> {
const newCommentData = args.data.comments?.createMany?.data
? args.data.comments?.createMany?.data?.map((comment) => ({
...comment,
Expand All @@ -38,6 +44,7 @@ export class CommentThreadResolver {
...{ comments: { createMany: { data: newCommentData } } },
...{ workspace: { connect: { id: workspace.id } } },
},
select: prismaSelect.value,
});

return createdCommentThread;
Expand All @@ -48,10 +55,13 @@ export class CommentThreadResolver {
})
async updateOneCommentThread(
@Args() args: UpdateOneCommentThreadArgs,
): Promise<CommentThread> {
const updatedCommentThread = await this.commentThreadService.update(
args satisfies UpdateOneCommentThreadArgs as Prisma.CommentThreadUpdateArgs,
);
@PrismaSelector({ modelName: 'CommentThread' })
prismaSelect: PrismaSelect<'CommentThread'>,
): Promise<Partial<CommentThread>> {
const updatedCommentThread = await this.commentThreadService.update({
...args,
select: prismaSelect.value,
} as Prisma.CommentThreadUpdateArgs);

return updatedCommentThread;
}
Expand All @@ -60,13 +70,18 @@ export class CommentThreadResolver {
async findManyCommentThreads(
@Args() args: FindManyCommentThreadArgs,
@AuthWorkspace() workspace: Workspace,
) {
@PrismaSelector({ modelName: 'CommentThread' })
prismaSelect: PrismaSelect<'CommentThread'>,
): Promise<Partial<CommentThread>[]> {
const preparedArgs = prepareFindManyArgs<FindManyCommentThreadArgs>(
args,
workspace,
);

const result = await this.commentThreadService.findMany(preparedArgs);
const result = await this.commentThreadService.findMany({
...preparedArgs,
select: prismaSelect.value,
});

return result;
}
Expand Down
11 changes: 9 additions & 2 deletions server/src/core/comment/resolvers/comment.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { Comment } from '../../../core/@generated/comment/comment.model';
import { CreateOneCommentGuard } from '../../../guards/create-one-comment.guard';
import { Prisma } from '@prisma/client';
import { CommentService } from '../services/comment.service';
import {
PrismaSelector,
PrismaSelect,
} from 'src/decorators/prisma-select.decorator';

@UseGuards(JwtAuthGuard)
@Resolver(() => Comment)
Expand All @@ -21,12 +25,15 @@ export class CommentResolver {
async createOneComment(
@Args() args: CreateOneCommentArgs,
@AuthWorkspace() workspace: Workspace,
): Promise<Comment> {
@PrismaSelector({ modelName: 'Comment' })
prismaSelect: PrismaSelect<'Comment'>,
): Promise<Partial<Comment>> {
return this.commentService.create({
data: {
...args.data,
...{ workspace: { connect: { id: workspace.id } } },
},
} satisfies CreateOneCommentArgs as Prisma.CommentCreateArgs);
select: prismaSelect.value,
} as Prisma.CommentCreateArgs);
}
}
34 changes: 14 additions & 20 deletions server/src/core/company/company-relations.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,28 @@ import * as TypeGraphQL from '@nestjs/graphql';
import { CommentThread } from 'src/core/@generated/comment-thread/comment-thread.model';
import { Comment } from 'src/core/@generated/comment/comment.model';
import { Company } from 'src/core/@generated/company/company.model';
import { User } from 'src/core/@generated/user/user.model';
import { CompanyService } from './company.service';
import { CommentThreadService } from '../comment/services/comment-thread.service';
import { CommentService } from '../comment/services/comment.service';
import {
PrismaSelect,
PrismaSelector,
} from 'src/decorators/prisma-select.decorator';

@TypeGraphQL.Resolver(() => Company)
export class CompanyRelationsResolver {
constructor(
private readonly companyService: CompanyService,
private readonly commentThreadService: CommentThreadService,
private readonly commentService: CommentService,
) {}

@TypeGraphQL.ResolveField(() => User, {
nullable: true,
})
async accountOwner(
@TypeGraphQL.Parent() company: Company,
): Promise<User | null> {
return this.companyService
.findUniqueOrThrow({
where: {
id: company.id,
},
})
.accountOwner({});
}

@TypeGraphQL.ResolveField(() => [CommentThread], {
nullable: false,
})
async commentThreads(
@TypeGraphQL.Root() company: Company,
): Promise<CommentThread[]> {
@PrismaSelector({ modelName: 'CommentThread' })
prismaSelect: PrismaSelect<'CommentThread'>,
): Promise<Partial<CommentThread>[]> {
return this.commentThreadService.findMany({
where: {
commentThreadTargets: {
Expand All @@ -45,13 +33,18 @@ export class CompanyRelationsResolver {
},
},
},
select: prismaSelect.value,
});
}

@TypeGraphQL.ResolveField(() => [Comment], {
nullable: false,
})
async comments(@TypeGraphQL.Root() company: Company): Promise<Comment[]> {
async comments(
@TypeGraphQL.Root() company: Company,
@PrismaSelector({ modelName: 'Comment' })
prismaSelect: PrismaSelect<'Comment'>,
): Promise<Partial<Comment>[]> {
return this.commentService.findMany({
where: {
commentThread: {
Expand All @@ -63,6 +56,7 @@ export class CompanyRelationsResolver {
},
},
},
select: prismaSelect.value,
});
}

Expand Down
Loading