question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom data types for fields

See original GitHub issue

Hi, 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:

  1. I found issues related to unsupported data types like https://github.com/prisma/prisma/issues/255 or https://github.com/prisma/prisma/issues/5001.

  2. 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).

  3. 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:

  1. 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
}
  1. 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:open
  • Created 3 years ago
  • Reactions:48
  • Comments:13

github_iconTop GitHub Comments

14reactions
massivefermioncommented, Dec 8, 2021

+1 for custom types. Would really improve schema design!

9reactions
livthomascommented, Feb 6, 2021

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:

  • I use SQLite database in my application and I don’t like the way it stores dates. They are being stored as numeric timestamps and It’s hard to see what day it is when I open the DB directly in some DB tool. Especially, when the only thing I care about is date (without any time). Now, I have to define it as String type in Prisma schema. It would be nice to have an ability to define a mapper which would convert it to Date in JS but store it in a TEXT field in ISO date format.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Field Types - Salesforce Help
This table includes a description of each custom field type. Additional field types can appear if an AppExchange package using those field types...
Read more >
Introduction to data types and field properties - Microsoft Support
Overview of data types and field properties in Access, and detailed data type ... You can use a predefined format or build your...
Read more >
Custom Field Types - YouTrack Cloud - JetBrains
A custom field type is a format that is assigned to values that are stored in the field. This format determines how YouTrack...
Read more >
Custom object field types - Oracle Help Center
These are fields that are not standard contact or account fields. Custom fields can be used to store the data required by your...
Read more >
Custom Field Data Types - VSys One
Custom Field Data Types. Every field has a specific format for information. Which format you use will depend on the type of data...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found