Custom data types for fields
See original GitHub issueHi, our company is moving from PHP to Node ecosystem and I am impressed by this project. Great work and great thinking especially! ❤️
Problem
However, there is one thing that feels natural to me when mapping raw rows from database to sophisticated and type-safe objects: custom data types. I spent the last three hours reading Prisma’s manual and googling related issues but I don’t see any open issue about this topic.
Use cases:
-
I found issues related to unsupported data types like https://github.com/prisma/prisma/issues/255 or https://github.com/prisma/prisma/issues/5001.
-
In our application we use data types like currencies, IBANs, time zone offsets, IP addresses, EANs, locale territories, VAT numbers, and many others that are usually stored as simple chars/varchars in the database, but are represented with a more refined data type in the application (like some class instance).
-
You might want to use better alternatives for TIMESTAMP or DATETIME types than JavaScript’s native Date class and map such database types to eg. a Luxon instance.
Suggested solution
All three of these cases can be handled by allowing to define a custom column data type that:
- is represented by some native type in the database (for migrations),
- is implemented as a custom deserialization function (for selects) and serialization function (for inserts/updates) by the application.
In PHP we use Doctrine and this is what I have in mind: https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/cookbook/custom-mapping-types.html . (Yes, Doctrine is a full-blown ORM library but IMO this idea can be used nevertheless.)
Thanks to this feature people would be able to create own custom mappings for column types that have not been supported by Prisma yet (as a temporary workaround), as well as use some eg. “IP address” type without the need for explicit in-application mapping (from the object returned from Prisma to object with better data types) for better developer experience.
The closest ideas I found: https://github.com/prisma/prisma/issues/446#issuecomment-566959483 (if you add the (de)serialization mapping functions), https://github.com/prisma/prisma/issues/3618 (could be solved by custom data types), https://github.com/prisma/specs/issues/119 (replaced by https://github.com/prisma/prisma/issues/446 which I don’t think is the same).
What we would need:
- A way how to define the custom data type in schema (something like this):
type VatNumber = String @custom("vatNumber") @db.Char(11)
model Company {
id Int @default(autoincrement()) @id
name String @unique
vatNumber VatNumber
}
- A way how to inject the custom
vatNumber
type serialization and deserialization functions to Prisma’s attributes mapping functionality.
The expected behavior is that when reading some company row from the database, the vatNumber
attribute in the object is going to be some eg. VatNumber
class instance (as implemented by the deserialization function). Also, when inserting/updating I am going to be able to use the VatNumber
instance directly without needing to serialize it to a string at first.
So what do you think? Is it something that is completely out of the scope of this project? Or would you consider this to be something useful? Or is there some other issue with the same idea that I simply missed?
Thanks! 😃
PS: The first thing I naturally tried to do was to use the VatNumber
type in the schema to see what happens and I got this error:
error: Type "VatNumber" is neither a built-in type, nor refers to another model, custom type, or enum.
That has got me thinking: what did you mean by this “custom type” in the error message? 😉
Issue Analytics
- State:
- Created 3 years ago
- Reactions:48
- Comments:13
Top GitHub Comments
+1 for custom types. Would really improve schema design!
I would also very much appreciate if you implemented this feature. I have various use cases for this feature but the most pressing one at the moment is this:
String
type in Prisma schema. It would be nice to have an ability to define a mapper which would convert it toDate
in JS but store it in aTEXT
field in ISO date format.