typescriptpostgresqlormprismaprisma2

how do I find the types to create with a Prisma client?


I have a Prisma model called 'bid' and I want to make sure my data is typed correctly to create. There are many auto-generated prisma types, but not sure of the naming convention and how to get the right type to use.

I have a bid schema like this:

model Bid {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  user      User     @relation(fields: [userId], references: [id])
  userId    String
  auction   Auction  @relation(fields: [auctionId], references: [id])
  auctionId Int
  amount    Int      @default(0)

  @@index([userId])
  @@index([auctionId])
}

this code works but I want to have some type safety on the data object

    const data = {
      userId,
      auctionId,
      amount,
    };
    await prisma.bid.create({
      data,
    });

Trying to use the intellisense on bid.create gives this:

(method) Prisma.BidDelegate<false, DefaultArgs>.create<{
    data: {
        userId: any;
        auctionId: any;
        amount: any;
    };
}>(args: {
    data: {
        userId: any;
        auctionId: any;
        amount: any;
    };
// blah blah 

no clear types given.

I tried to fish out BidCreateArgs from the Prisma object:

import { Prisma } from "@prisma/client";

...

    const data: Prisma.BidCreateArgs = {
      userId,
      auctionId,
      amount,
    };

which errors out with this:

Type 'BidCreateArgs<DefaultArgs>' is not assignable to type '(Without<BidCreateInput, BidUncheckedCreateInput> & BidUncheckedCreateInput) | (Without<BidUncheckedCreateInput, BidCreateInput> & BidCreateInput)'.

Also tried with BidCreateInput which gets more verbose errors.

So, given a model name, what is the type I can use to create with?

I also see there are a few different parent imports, maybe I'm pulling from the wrong place?

import { Prisma } from "@prisma/client";
import { PrismaClient } from "@prisma/client";

Solution

  • update: it seems I have to create the object with the connect relation handling stuff. This works:

        let bidData: Prisma.BidCreateInput = {
          amount,
          user: {
            connect: {
              id: userId,
            },
          },
          auction: {
            connect: {
              id: auctionId,
            },
          },
        };
    

    It's a bit wordy, it would be nice to be able to just create with the userId and auctionId since I have those values...