Hi there, people! Currently building a POC using ...
# orm-help
l
Hi there, people! Currently building a POC using Prisma to present it to my team as an alternative to TypeORM. Our stack includes NestJS and we have a user module with authentication. We also use entities with class-transformer. The Prisma generated types works great and it’s really helpful, but in this case I have been using @Exclude() on my entities to remove the password. But it’s not working properly, not excluding the password. My files currently look like this:
Copy code
import { ApiProperty } from '@nestjs/swagger';
import { User } from '@prisma/client';
import { Exclude } from 'class-transformer';

export class UserEntity implements User {
  ...

  @Exclude()
  password: string;

  ...

  // If I add this constructor and call `return new UserEntity(user) at the end of my services it works, when using TypeORM that wasn't necessary.
  constructor(partial: Partial<UserEntity>) {
    Object.assign(this, partial);
  }
}
A simple service I wrote just to validade password exclusion
Copy code
@Injectable()
export class GetUserService {
  constructor(private prisma: PrismaService) {}

  async get(id: string): Promise<UserEntity> {
    const user = await this.prisma.user.findUnique({ where: { id } });

    // Again, in order for the @Exclude option to work, I have to do this.
    return new UserEntity(user);
  }
}
NOTE: I have Nest global serialization enabled on the app. My goal is to have password removed from the response without having to add the constructor in the entity, I did check Prisma Docs and read about excluding fields, but the options there are to open to errors just as this one I’m using. Manually removing password using the select option also gives room to error, as I have to add all other fields every time, excluding only password. So, anyone have other suggestions on how I can solve this, am I missing something? Appreciate feedback.
1
s
So you have
ClassSerializerInterceptor
set as a global interceptor in Nest app, right? I see you are returning an instance of a class, so class serializer interceptor which uses
class-transformer
should be able to process
@Exclude
annotated properties on your class instance.
n
Hi @Lucas Pelegrino 👋 We do have a Feature Request for Exclude: #5042 Does this guide for excluding password work for you: Guide?
s
@Lucas Pelegrino the other possible way you could do this is to use middleware: https://www.prisma.io/docs/concepts/components/prisma-client/middleware
🤘🏻 1