Skip to content

Code-first Prisma schema API #227

Closed as not planned
Closed as not planned

Description

@nikolasburk

As requested in this issue, we're currently debating if/how we should provide a code-first API to build up a Prisma schema.

Citing the original arguments (pro code-first) from @HosseinAgha:

  • Please re read your article on the problems of schema first development.
  • I worry that in time you reach the same conclusion and you end up with a schema language that tries to solve everything but is not as good as a real programming language.
  • I think having a real general purpose programming language at your disposal is always a good idea. (take Webpack config or many other tools for example)
  • I understand that Prisma is a cross language tool (Go driver for example) but it is also written in JS and Generator and Lift need node to run. So I see no harm in having a schema.prisma.js option.

Note that a good foundation for a code-first API might be the Prisma SDK. This issue is used to discuss the possibilities and pros/cons of the approaches.

Activity

  1. changed the title [-]Discussion: Schema-first vs Code-first Prisma schema[/-] [+]Discussion: Schema-first vs Code-first Prisma schema API[/+] on Jul 24, 2019
  2. janpio commented on Jul 24, 2019

    @janpio
  3. pantharshit00 commented on Jul 25, 2019

    @pantharshit00
    Contributor

    I think if provide this approach as an option the schema file should be a generated artefact anyways to get a better overview of database structure in a readable format.

  4. Akkuma commented on Jul 25, 2019

    @Akkuma

    One thing pointed out in that issue is with env variables, which I've ran into playing with prisma2 is that docker postgres & prisma2 would use different env variables. This means I either need another env variable composed of my docker postgres variables, I need to combine my env variables to generate a new env variable for prisma2, or the schema needs to support multiple env variables at once so I could build the url inline.

    If this were just a js file, I could load my normal .env files and build up the postgres url without resorting to needing new schema features, a .env loader that could build from other variables, or providing another env variable.

  5. pinned this issue on Jul 28, 2019
  6. chrisui commented on Jul 30, 2019

    @chrisui

    Hey! Been asked to raise my use case for, at least optionally, being able to not embed env/config variables into bundles. I originally raised this in #287.

    In our pipeline we only ever generate stateless builds. This means that you can take a build from a development branch, through staging and production without ever rebuilding. Ie. The code you develop, test and QA is exactly the same as what your users are exposed to in production.

    In practice this essentially boils down to never compile configuration into your application bundles and simply allow runtime configuration - commonly through environment variables. For example see the recommendation on heroku for their pipleines.

    Right now prisma generate breaks these design principles by forcing the evaluation of env() expressions in the schema definition at build time.

    It would be great to have a mode whereby when generating your runtime you could configure it to be stateless so that env("MY_ENV") is compiled to process.env.MY_ENV rather than evaluated at generation time.

  7. HosseinAgha commented on Jul 31, 2019

    @HosseinAgha

    I think another benefit of a JS based schema definition (code-first approach) is that we don't need to discuss issues like "how to have a modular schema?" (#92) anymore as node.js module system already handles it for us.

  8. matthewmueller commented on Aug 5, 2019

    @matthewmueller
    Contributor

    You've sparked a lively debate internally about this decision ☺️

    The reason we went with a declarative format rather than a code-first format is that we're describing the layout of your datasource, we're not providing implementations for this schema (unlike graphql's resolvers). In this sense, we're more like JSON schema or terraform than GraphQL. We also want to be able to migrate your database based on this schema file.

    We do understand that you may want to simply generate database clients and bring your own migration engine, so for that we're planning to introduce the Prisma SDK to work with Prisma without generating a schema.

  9. marcus-sa commented on Aug 6, 2019

    @marcus-sa

    A decorator based approach like https://typegraphql.ml does with GraphQL types would be nice, so you wouldn't have to use multiple classes for the same purpose :)

  10. HosseinAgha commented on Aug 6, 2019

    @HosseinAgha

    It is very good to see that you value community opinions so much.
    @matthewmueller I understand the points you make here. The Lift needs a static snapshot of current schema to make migrations.
    I still think having a JavaScript compatible schema like JSON schema is a good idea (at least as an alternative to current schema language). Lift can simply save a snapshot of the JSON object to the migrations folder.
    I think the best and most successful example for my idea is Webpack config file. Finally you can generate the prisma schema file for readability.

    IMHO by introducing a new schema definition language:

    • the developers have to learn yet another language
    • you have to support that language and add features to it
    • only benefit here is readability and more expressive schema declaration

    At the end of the day it is the trade-off that you as the platform developers should consider.

  11. jednano commented on Aug 8, 2019

    @jednano

    Please support a TypeScript, code-first schema!

    Benefits of a TypeScript, code-first schema

    1. The TS API parses both TS and JS files, providing a solid AST, from which a Prisma schema can be generated. You might even be able to leverage some type inference when parsing JS.
    2. DRY, single source of truth.
    3. Learn one language. Apply it everywhere. TypeScript all the things!
    4. It's the missing link to writing a truly full-stack TypeScript application (see above).
    5. Improve confidence by generating TS definitions for client-side applications (and more).
    6. We will have achieved nirvana, the ultimate state of enlightenment.
  12. terion-labs commented on Aug 20, 2019

    @terion-labs

    I don't like this style of definitions:

    const User = objectType('User', t => {
      t.id('id')
      t.string('name', { nullable: true })
    })
    
    const Query = objectType('Query', t => {
      t.field('user', 'User', {
        args: { id: idArg() },
        resolve: (_, { id })
      })
    })
    

    It is complex, needs a lot of learning from developer, it's not really easy readable, etc.

    I loved SDL-first approach paired with Prisma and graphql-import: this allowed me to make a toolset that allows to generate resolvers over schema and spin up services with rich CRUD interface in matter of minutes after data definition.

    Now with lift, photon and nexus it seems I can't do this, and this is a big problem for migration of my toolsets to Prisma2

    One thing that I think is mandatory is for photon to generate not only programmatic crud-client, but crud schema also, to make it possible to import crud definitions in application schema. This saves A TON of time, seriously

  13. 45 remaining items

  14. changed the title [-]Discussion: Schema-first vs Code-first Prisma schema API[/-] [+]Code-first Prisma schema API[/+] on Dec 16, 2019
  15. unpinned this issue on Dec 16, 2019
  16. iddan commented on Jul 30, 2020

    @iddan

    In the midtime, we released a library implementing the proposed AST and code generator: https://github.com/amplication/prisma-schema-dsl

  17. janpio commented on Jul 30, 2020

    @janpio
  18. dustinlacewell commented on Nov 14, 2021

    @dustinlacewell

    Can there be a status update? I have a project which would really benefit from a end-to-end code-first approach.

  19. alexn-s commented on Oct 9, 2022

    @alexn-s

    is there any progress on this manner?

  20. alexn-s commented on Oct 9, 2022

    @alexn-s

    for anybody still looking for a solution:

    i found a library https://github.com/ridafkih/schemix that

    allows you to programmatically create Prisma schemas using TypeScript

  21. igalklebanov commented on Dec 26, 2025

    @igalklebanov
    Contributor

    Hey 👋

    Cloaing this for staleness and being dusty.

  22. locked and limited conversation to collaborators on Dec 26, 2025
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: schematopic: sdk

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions