Skip to content

EPIC: Support for native database types #446

Closed

Description

@divyenduz

Prisma handles values based on core types and best effort implementation as mentioned in this spec: https://github.com/prisma/specs/tree/master/schema#data-types

However, many databases support custom data types like JSON, timestamp without time zone, date, binary formats etc.

Prisma should provide a mechanism to utilize these DB specific primitives.

Related issues:

Possibly related:

  • Using Prisma enums results in setting DB-level default value #741

Spec issue prisma/specs#119

Activity

  1. added
    kind/feature
    on Aug 28, 2019
  2. johannesschobel commented on Sep 24, 2019

    @johannesschobel
    Contributor

    I would also love to see types for varchar(x), smallint, longtext and so on. If a respective database does not support these types, it should "fallback" to the most appropriate type (i.e., smallint -> int.

    In Laravel (PHP) they follow a similar approach as can be seen here in the docs:
    https://laravel.com/docs/5.8/migrations#creating-columns
    They just map to the "database equivalent of FLOAT" (or whatever).

    In the grammars for the database engines (i.e., postgres, mysql, ...) this is handled as follows (example for MediumInteger)
    PostgreSQL: https://github.com/laravel/framework/blob/6.x/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php#L487-L496
    MySQL: https://github.com/laravel/framework/blob/6.x/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php#L501-L510
    SQLite: https://github.com/laravel/framework/blob/6.x/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php#L484-L493

    What do you think?

    Can we (community) participate and support you in implementing such Grammars for the different Database Engines? If so, i can surely create such mappings and provide a PR? Would love to contribute to this awesome project!

    All the best,
    Johannes

  3. matthewmueller commented on Oct 2, 2019

    @matthewmueller
    Contributor

    @johannesschobel it seems from these grammar files that they still provide common core types, just abstract over them (e.g. jsonb maps to text in sqlite).

    Are there grammars that don't implement certain features where the database can't properly store the data type? I guess I'm wondering if this is a common, shared interface or it's database-specific.

  4. johannesschobel commented on Oct 2, 2019

    @johannesschobel
    Contributor

    In Laravel, you have to write "Migration Files", where you define your database structure. In this migration files, you can add fields and define respective types (i.e., json, string, integer, and so on.. )
    You can take a look at how to create a migration here: https://laravel.com/docs/5.8/migrations#creating-columns

    The Grammar files i linked above, are some kind of "mapping" for these Laravel types. That means, that laravel, for example, provides types (i.e., json) that may not be supported by your chosen database system (sqlite, mysql, ...). This "mapping" is then handled by the underlying grammar file.

    I think, the grammars implement a certain interface where all these Laravel Types are defined and then are implemented in the concrete grammar.. But not sure

    I hope this helps?! Do you require further information?

  5. matthewmueller commented on Oct 2, 2019

    @matthewmueller
    Contributor

    @johannesschobel got it. thanks for clarifying, so in a sense they "upgrade" databases by adding features (e.g. jsonb for sqlite) . This works if all your database clients understand this agreement.

    What I was curious about is if Laravel changes the migration API based on the database. So for example, they remove $table->jsonb if you're working with an SQLite database. This is more what we are considering.

    We could always add more core datatypes to improve granularity and maybe we should. But it's a tradeoff. More datatypes means more surface area to "upgrade" for other databases. It also means that if we add a new core datatype tomorrow, we'll have to upgrade all datasources to support it.

  6. tomhoule commented on Oct 2, 2019

    @tomhoule
    Contributor

    See this comment https://github.com/prisma/lift/issues/116#issuecomment-537534050 for a case where we could need what is currently one core data type (String) to two database column types (varchar and text) due to database limitations.

  7. johannesschobel commented on Oct 4, 2019

    @johannesschobel
    Contributor

    Dear @matthewmueller ,

    thanks again for your reply on this. In some kind of way, Laravel enables new datatypes for underlying databases, yes. However, from 5.0 till 5.8 version (about 4 years) they did not change those Laravel Types - they only added new features (e.g., $table->json) when features where supported in a stable way from the database engines or can be mapped to "more basic datatypes" (i.e., Text) if no appropriate better type exist.

    I really like the Laravel approach, as it - basically - is just an additional mapper / abstraction layer for the database types. This makes it incredibly easy to just add new databases "on the go". Changes to these grammars are quite limited (you can take a look at the git history of such files).

    It is debatable, if all features provided by Laravel Grammars should be "ported" or not (i.e., naming conventions for index, ...) or if Prisma just takes the idea of mapping internal types to database specific types. Those grammars could also be extended quite easily if you have the need to specify a convention for naming your index structures...


    Further, i don't think, that the Laravel Migration API changed significantly over time. Nevertheless, even if you would change your migration idea, you can still "correct" it in the grammars, right? 😆

  8. matthewmueller commented on Oct 4, 2019

    @matthewmueller
    Contributor

    @johannesschobel Would you prefer these "upgrades" versus a grammar that's specific to the database?

    For example, you could also have a grammar that doesn't implement $table->json for SQLite, but does for Postgres.

    Would you see this as being an improvement or a step backwards?

  9. johannesschobel commented on Oct 4, 2019

    @johannesschobel
    Contributor

    i think, that each specific Grammar (i.e., PostgreSQLGrammar, SQLiteGrammar) should extend a BaseGrammar, which may implement a common interface.

    The abstract BaseGrammar could have some "default one-fits-all" mappings. For example, the BaseGrammar could map the Prisma internal datatype JSON by default to a TEXT datatype in the database. However, PostgreSQL offers a "native json support", therefore this "mapping method" may be overwritten in the PostgreSQLGrammar to map it to JSON instead.

  10. matthewmueller commented on Oct 4, 2019

    @matthewmueller
    Contributor

    @johannesschobel what would you do for very postgres-specific types and extensions like polygon, ipaddress & citext?

  11. johannesschobel commented on Oct 4, 2019

    @johannesschobel
    Contributor

    I think, i would add some kind of getFallbackType() method in the BaseGrammar which is extended by all other database-specific grammars.

    This fallbackType returns, for example text as default type... Of course, this fallback Type can be overwritten for a specific implementation (i.e, the PostgreSQLGrammar may use LargeText as fallbackType, while SQLiteGrammar uses Text).

    If, for example, the developer defines IPAddress or Polygon as "Prisma Datatype, and respective Datatype is not defined for the database, i would fallback to the fallbackType. For PostgreSQL this would be LargeText` for example.

    At a later point in time, you could just add the getIPAddressType() method to the BaseGrammar or whatever specific grammar and map it to a proper database field type. If, for example, SQLite still does not support geo types, just omit it - it will then fallback to the default..

    What do you think?

  12. changed the title [-]Support for custom types[/-] [+]EPIC: Support for custom types[/+] on Dec 13, 2019
  13. changed the title [-]EPIC: Support for custom types[/-] [+]EPIC: Support for native database types[/+] on Dec 18, 2019
  14. ctrlplusb commented on Dec 18, 2019

    @ctrlplusb
    Contributor

    I like the idea of being able to support underlying native types.

    I saw an example of a cool idea somewhere that would provide a lot more flexibility in this regard.

    scalar CIText = String @raw("citext")
    
    model User {
      id  String @id
      email CIText
    }

    I really like this derived type idea. Something to keep in mind when spec'ing.

  15. 26 remaining items

  16. matthewmueller commented on Aug 27, 2020

    @matthewmueller
    Contributor

    Thanks for the lively discussion everyone! Closing in favor of #3447.

  17. willin commented on Sep 3, 2020

    @willin

    @mavilein which version to support? using v2.6.0 now, found not yet.

  18. pantharshit00 commented on Sep 3, 2020

    @pantharshit00
    Contributor

    @willin The work is still ongoing and the discussion from this issue was summarised on #3447 and that issue is now tracking this

  19. poorvi-vaish commented on Jun 1, 2023

    @poorvi-vaish

    can we use Buffer data type for images?

  20. janpio commented on Jun 7, 2023

    @janpio
    Contributor

    Please open a discussion for this question @poorvi-vaish. Thank you.

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

Metadata

Metadata

Assignees

Labels

kind/featuretopic: native database types

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions