Skip to content

Support Unsupported Types inside Prisma Client (not just Prisma Migrate) #5683

Open

Description

@cyrus-za

Problem

There are dozens of types on different databases that prisam does not currently support

One such type that our company is currently using a lot is the Postgres Interval Type

Suggested solution

Rather than addressing each of these types (which still needs to be done in the long run), find a solution to let the user (dev) hook into prisma with a callback function to support the type. Think of how graphqljs (and nexus) does custom scalars. In the nexus docs you will find that there's a callback hook for parseValue, serialize and parseLiteral

What if we could do the same with unsupported types in prisma. Let's say we got a prisma schema with a TimeEntry model using postgres, with duration column that uses interval type on postgres.

model TimeEntry {
  id                  Int                     @id @default(autoincrement())
  duration            Unsupported("interval")
  createdAt           DateTime?               @default(now())
  updatedAt           DateTime?               @default(now())
  deletedAt           DateTime?
}

It would be great if we could read the timeEntry including the unsupported duration column with code such as below:

const findAll = async () => 
  prisma.timeEntry.findMany({
    customColumns: {
      duration: {
        castTo: 'text' // adds duration::text in postgres query,
        parse: moment.duration // custom function to parse the output from postgres before returning the promise
      }
    }
  })

Additional context

See #4045 for related discussion, especially my comment here

Activity

  1. added
    kind/feature
    topic: Unsupported
    on Feb 18, 2021
  2. benknab commented on Feb 24, 2021

    @benknab

    This would also help developers ensure transactionality while using the prisma API when a model with an unsupported type has to be updated together with regular prisma models because right now both nested writes and prisma.$transaction are insufficient for this use case.
    See discussion: #5804

  3. benjamintd commented on Mar 15, 2021

    @benjamintd

    From a developer point of view, and using postgis fields as an example, here's what I think the API could look like:

    const findAll = async () => 
      prisma.point.findMany({
        where: {field: "value"}, 
        include: {
          geometry: {
            cast: column => `ST_AsGeoJSON(${column})`,
            parse: JSON.parse
          }
        }
      })
    
    const writeOne = async () => 
      prisma.point.create({
        field: "value",
        geometry: {
          value: {type: "Point", coordinates: [1.23, 3.45]}
          cast: content => `ST_GeomFromGeoJSON(${JSON.stringify(content)})` 
        }
      })
  4. changed the title [-]Support `unsupportedTypes` inside prisma client (not just prisma migrate)[/-] [+]Support `Unsupported` Types inside Prisma Client (not just Prisma Migrate)[/+] on Feb 6, 2022
  5. stevenkissack commented on Mar 10, 2022

    @stevenkissack

    In order to not open another ticket relating the Unsupported field support, I'll post some related information and hope the team settle on the best workaround that won't cause them pain in the long-term..

    Related Issue for the underlying engine doing the type conversion: prisma/quaint#64

    Schema level casting:

    // Idea 1 - Prisma is an abstraction of the database, so why not leverage this:
    randomLocation @Unsupported("point")
    randomLocationVirtual String @convert(field: randomLocation, select: “$::TEXT”) // $ references the value of the field?
    
    // Idea 2 - leave the dev to format on select/update
    randomLocation String @Unsupported("point")
    
    // Janpio’s suggestion:
    randomLocation Unsupported("point") @client.cast/fallback(string)
    
    // Similar but cleaner?
    randomLocation Unsupported("point") @cast(type: String)
    

    The more I look at the typing, the more I see this is not mainly a PostGIS type problem, without a solution then you'll always be chasing your tail to support new types, especially if the plan is to support more databases.

    A big question is if you can provide the needed flexibility in the schema definition alone, or if as suggested above, you need the devs to write the handlers.

    String casting could also be implemented behind a previewFeature such as "unsupported-string-autocast" to fix this until you have time for a long-term solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/featuretopic: Unsupportedtopic: client api

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions