Vakindu
Vakindu

Reputation: 815

NestJS Prisma - Types of property entities incompatible, missing or not assigned

I'm trying to update my Postgres database using Prisma ORM in NestJS (Microservices architecture). The code allows users to interact with invitation requests. But I keep getting the error: Argument of type 'Invitation' is not assignable to parameter of type 'Invitation & { work: Work; }'. Property 'work' is missing in type 'Invitation' but required in type '{ work: Work; }'.

The Full error message looks like this:

Type '{ status: "PENDING" | "ACCEPTED" | "REJECTED"; work: undefined; id: string; workId: string; coId?: string; createdAt: Date; respondedAt?: Date; owner?: string; note?: string; }' is not assignable to type '(Without<InvitationUpdateInput, InvitationUncheckedUpdateInput> & InvitationUncheckedUpdateInput) | (Without<...> & InvitationUpdateInput)'.

Type '{ status: "PENDING" | "ACCEPTED" | "REJECTED"; work: undefined; id: string; workId: string; coId?: string; createdAt: Date; respondedAt?: Date; owner?: string; note?: string; }' is not assignable to type 'Without<InvitationUncheckedUpdateInput, InvitationUpdateInput> & InvitationUpdateInput'.

Type '{ status: "PENDING" | "ACCEPTED" | "REJECTED"; work: undefined; id: string; workId: string; coId?: string; createdAt: Date; respondedAt?: Date; owner?: string; note?: string; }' is not assignable to type 'Without<InvitationUncheckedUpdateInput, InvitationUpdateInput>'. Types of property 'workId' are incompatible. Type 'string' is not assignable to type 'never'.

How can I fix this?

Thanks in advance!

The Invitation Interface:


export default class Invitation {
  id: string; 
  workId: string; 
  work: string;
  co?: string;
  status: RequestStatus;
  createdAt: Date;
  respondedAt?: Date;
  owner?: string;
  note?: string;
}

export enum RequestStatus {
  PENDING = 'PENDING',
  REJECTED = 'REJECTED',
  ACCEPTED = 'ACCEPTED',
}

The prisma invitation schema:


model Invitation {
  id              String        @id @db.Uuid
  workId          String        @db.Uuid
  work            Work          @relation(...)
  status          RequestStatus
  co              String        @db.Uuid
  owner           String
  note            String  
  createdAt       DateTime
  respondedAt     DateTime  
}

The Updating mechanics in Postgres Persistence Infrastructure:


  async update(invitation: Invitation): Promise<Invitation> {
    const entity = await this.prismaService.invitation.update({
      where: {
        id: invitation.id,
      },
      data: {
        ...invitation,
        status: RequestStatusEntity[invitation.status],
        work: undefined,
      },
      include: {
        work: true,
      },
    });

    return this.toDomain(entity);
  }

  private toDomain(
    entity: InvitationEntity & {
      work: WorkEntity;
    },
  ): Invitation {
    return Object.setPrototypeOf(
      {
        ...entity,
        status: RequestStatus[entity.status],
      },
      Invitation.prototype,
    );
  }  

Upvotes: 1

Views: 3613

Answers (1)

Ryan
Ryan

Reputation: 6327

You cannot use TypeScript enums in this case as Prisma uses string literals in the generated type.

I would suggest using Prisma's types directly instead of creating your own like this:

import { RequestStatus } from '@prisma/client'

export default class Invitation {
  id: string; 
  workId: string; 
  work: string;
  co?: string;
  status: RequestStatus;
  createdAt: Date;
  respondedAt?: Date;
  owner?: string;
  note?: string;
}

Upvotes: 2

Related Questions