Skip to content
This repository was archived by the owner on Oct 21, 2020. It is now read-only.
This repository was archived by the owner on Oct 21, 2020. It is now read-only.

Custom types #119

Closed
Closed

Description

@divyenduz

Introduction

Prisma 2 defines core primitive types (in this document used interchangeably with Prisma scalar), all connectors implement this types natively or as best effort implementation. There are a couple of questions I have on how this would play with custom types.

Use Cases

A custom type that cannot be backed by a Prisma scalar

To support custom types, we can create a type alias in the Prisma schema file and back it by a Prisma core type. Let us use Postgres's BIGINT as an example.

We could define this type as following but assume that the Int core type of connector's programming language (Say we are writing Postgres connector in JS) is not big enough for the underlying data source's (in this case Postgres's) BigInt

// Not possible in a JS connector as JS integer cannot hold Postgres's BIGINT (Yet, soon™: https://v8.dev/blog/bigint)
type LargeNumber = Int @pg.BIGINT

So, we defined a type and use it like this backing a BIGINT with a String:

type LargeNumber = String @pg.BIGINT

model Planet {
  id Int @id
  population LargeNumber
}

These lines have enough information to generate a Photon client that has the following API (say, for create):

photon.planet.create({
  data: {
	  population: "123123212"
  }
})

This usage might require coercion in Photon or the query engine or the data source itself. What/where it exactly happens needs further spec.

Related notes:

  • Same question can be asked for types that are not representable with an existing Prisma core type like BLOB. Will we add more Prisma core primitive types?
  • How does type Json = String @pg.json behave if the user input is not valid JSON → Does photon or some other part of stack reject it? What part of stack identifies an input variable as JSON and tries to coerce/parse it?

Related spec issue for capabilities: #8

Activity

  1. matthewmueller commented on Sep 3, 2019

    @matthewmueller
    Contributor

    Screenshot 2019-09-03 16 22 19

    Database capabilities are a dependency to the generators. Inside the database capabilities we'll have a list like this:

    "datatypes": {
      "bigint": "String",
      "serial": "Int"
    }

    I'm not sure it make much sense to even allow type aliases (at least until a use case comes up). I know we've talked about this list being virtually converted to prisma's schema language on the fly for VSCode's "Go To Definition", but I'm not sure if we should allow the user to arbitrarily define types like type BigNumber = String @pg.bigint.

  2. added
    kind/discussion
    spec/change
    on Sep 27, 2019
  3. malekjaroslav commented on Nov 19, 2019

    @malekjaroslav

    Hi guys, do you have some information about progress on this? I just hit a point where I want to use DATE type in PostgreSQL to be able to use native sorting functionality but it is not possible. Fetching all data and then sorting them on the server seems to be a very inefficient solution.

  4. janpio commented on Aug 22, 2020

    @janpio
    Contributor

    Replaced by prisma/orm#446

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

    area/prisma-client-jsarea/query-enginekind/discussionspec/change

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions