How to type Date types with Prisma and JSON blobs?

555 Views Asked by At

There seems to be an issue with prisma's serialization of JSON blobs wrt Date types. I wonder if anyone else has seen this and has some guidance/workaround. This is for a JSON field with Planetscale which is basically MySQL driver.

I have an object with a Date field, that I want to encode. This is a blob of data coming back from an external API

eg my object tp has a field defined typed a Date:

trained_at: Date;

in my prisma schema for tunePrompt I have a JSON field:

model TunePrompt {
   apiData                 Json?   // from external API

But when I try to write to that apiData JSON field:

      const data = {
        apiData: tp,
      }
      const newPrompt = await prisma.tunePrompt.create({ data })
Property 'trained_at' is incompatible with index signature.
Type 'Date' is not assignable to type 'InputJsonValue | null | undefined'.",

if I were to do JSON.stringify(tp) this works without error, but then I get double escaped JSON.

The generated types are something like:

  export type TunePromptCreateInput = {
    apiData?: NullableJsonNullValueInput | InputJsonValue
  }

the only workaround i've found is to type the Date fields as a string, but I'm sure this is going to lead to other parsing problems later.

Maybe I can look into typing the JSON blob, but I don't think that would solve the issue as its the serialization I think that's the problem.

0

There are 0 best solutions below