Skip to content

Separate prisma files to manage models #92

Closed

Description

@hyochan

In prisma1, I used to import multiple datamodels in prisma.yml like below.

  - datamodel.prisma
  - datamodel2.prisma
  - datamodel3.prisma

How can I do this in Prisma2 when there is only one file project.prisma when I generated the boilerplate.

image

Activity

  1. changed the title [-][Question] How to use multiple schemas in prisma[/-] [+][Question] How to seperate prisma files to manage models[/+] on Jul 4, 2019
  2. pantharshit00 commented on Jul 4, 2019

    @pantharshit00
    Contributor
  3. schickling commented on Jul 9, 2019

    @schickling
    Member

    @hyochan we've updated the schema spec with a concrete syntax proposal to support importing from other .prisma files. You can see the spec here.

    I've copied over the snippet for convenience. I'd love to hear your thoughts whether this is what you're looking for or whether you have other suggestions/requirements. 🙏

    schema.prisma

    import "./post.prisma"
    
    model User {
      posts Post[]
    }
    

    post.prisma

    model Post {
      title String @pg.varchar(42)
      body  String
    }
    
  4. added
    kind/feature
    on Jul 18, 2019
  5. steebchen commented on Jul 31, 2019

    @steebchen
    Contributor

    Since the schema is global anyway, why not do it the v1 way and include multiple prisma files in the config and maybe even supporting globs? This should also be easier to implement, as the files just have to be joined together, no import/parsing/require/resolve stuff needed. I also think that with a huge schema it can get cumbersome to import several types, especially considering that the IDE/editor can't parse and help with those imports (for now I guess).

  6. matthewmueller commented on Jul 31, 2019

    @matthewmueller
    Contributor

    Hey @steebchen!

    So we're not planning on re-introducing the prisma.yml file for Prisma 2 because we want to have the option to consolidate all the Prisma-related configuration into one file. Given this constraint, we're left with the following options:

    1. Pass in schema file paths / globs via the CLI everywhere we need the schema (basically every command)
    2. Introduce Go-style imports where folders gets concatenated together. We'd also need someway to bring in base schemas from other folders.
    3. Introduce a import "user.schema" to the schema.

    It also ties into a discussion about explicit vs. implicit. Go's packages follow an implicit concatenation rule vs. something like Node.js where you explicitly include files via require("./myfile").

    Do you have any preference or thoughts on this?

  7. HosseinAgha commented on Jul 31, 2019

    @HosseinAgha

    @matthewmueller could you explain how Go-style import work in this scenario?

  8. matthewmueller commented on Jul 31, 2019

    @matthewmueller
    Contributor

    @HosseinAgha yep, given this directory structure:

    app/

    prisma/
      schema.prisma
      post/
        video.prisma
        picture.prisma
        text.prisma
    

    post/video.prisma, post/picture.prisma, post/text.prisma would all be joined as 1 schema without any additional syntax, so you can reference models and types within the post/ as if it's in one file.


    We'd still probably need to introduce syntax to support things like base.prisma importing the post package.

    schema.prisma

    import "./post"

    Which would pull in the models from the whole ./post directory.

  9. jmadson commented on Jul 31, 2019

    @jmadson

    In addition to what @matthewmueller mentioned above, would it be possible to support doing the import in the format of a node glob string?
    For example, if I were to organize my code into separate folders with one model per file:

    models/person.prisma
    models/address.prisma
    ...
    

    Or into feature-specific "modules" directory of some sort:

    modules/person/model.prisma
    modules/person/object-type.ts
    modules/address/model.prisma
    modules/address/object-type.prisma
    

    Then in the root schema.prisma file, it would be nice to be able to pull in all of the *.prisma files using a glob string such as:
    import models/*.prisma or import modules/**/*.prisma

    We have over 400 tables so splitting them out and then importing them with a single glob string would be ideal.

  10. changed the title [-][Question] How to seperate prisma files to manage models[/-] [+]Seperate prisma files to manage models[/+] on Dec 2, 2019
  11. Aperrix commented on Dec 4, 2019

    @Aperrix

    Still no news regarding the development of this feature or an alternative?
    I love everything that has been done on prisma2, i would really like to migrate 😞

  12. scriptcoded commented on Dec 15, 2019

    @scriptcoded

    The links to the docs above seem to be broken, and I can't seem to find any info about this in the docs. Is there any other information on this anywhere?

  13. 12 remaining items

  14. khuezy commented on Mar 12, 2022

    @khuezy

    package.json

      "scripts": {
        "gen:schema": "cd ./prisma/schemas && cat *.prisma >> ../schema.prisma && npx prisma format"
      }

    Thanks! Probably want to use the single ">" right? Otherwise it'll amend the same models on subsequent commands.

  15. deyvisonborges commented on Mar 13, 2022

    @deyvisonborges

    @khuezy
    > redirects output to a file, overwriting the file.

    >> redirects output to a file appending the redirected output at the end.

  16. khuezy commented on Mar 13, 2022

    @khuezy

    But if you ran the script 2x, you'd get duplicate entries and prisma format would complain.
    This also doesn't work too well if you have the prisma VSCode extension b/c it'll complain that String[]: "The current connector does not support lists of primitive types" because of the missing datasource db provider.

  17. bdombro commented on Mar 13, 2022

    @bdombro

    @khuezy you need to use '>>' so that the files are combined. Otherwise they would overwrite each other. You'd prob want to delete the file first so that old entries are cleared. Also, if you run it and leave the schema VS should be happy I would think.

    "scripts": {
        "gen:schema": "cd ./prisma/schemas && rm -rf ../schema.prisma && cat *.prisma >> ../schema.prisma && npx prisma format"
      }
    
  18. khuezy commented on Mar 13, 2022

    @khuezy

    cat *.prisma > ../schema.prisma would combine all the .prisma content into a single schema.prisma file, so you wouldn't need to delete the schema.prisma file right? Or am I missing something.

    I ended up moving all my models into a single schema.prisma until prisma adds support for separate files. I was still getting syntax errors in VSCode.

  19. bdombro commented on Mar 13, 2022

    @bdombro

    You may be right

  20. deyvisonborges commented on Mar 14, 2022

    @deyvisonborges

    other method
    awk 1 ./prisma/schemas/*.prisma >> ./prisma/schema.prisma && npx prisma format

  21. RafaelKr commented on Mar 15, 2022

    @RafaelKr
    Contributor

    other method awk 1 ./prisma/schemas/*.prisma >> ./prisma/schema.prisma && npx prisma format

    @deyvisonborges as already stated by @khuezy above you should use > instead of >>
    >> will append the output to an existing file. So if you run it 5 times you will have all models 5 times in your schema.prisma.
    > will "overwrite" the contents of schema.prisma <= this is what you want.

    There is a detailed description of output redirection: https://linuxhandbook.com/redirection-linux/

  22. taylor-lindores-reeves commented on Mar 23, 2022

    @taylor-lindores-reeves

    @matthewmueller is this something in the pipeline?

  23. btd1337 commented on Jun 19, 2022

    @btd1337
    // directory structure
    
    prisma
      |_ schemas
      |   |_ client.prisma
      |   |_ db.prisma
      |   |_ model1.prisma
      |   |_ model2.prisma
      |   |_ model3.prisma
      |_ schema.prisma
    
    // package.json
    
    {
      "scripts": {
        "gen:schema": "awk 1 ./prisma/schemas/*.prisma > ./prisma/schema.prisma && npx prisma format",
      }
    }

    Single > character to recreate schema.prisma file without duplicates over the next generates

  24. Nomia commented on Jun 20, 2023

    @Nomia
    // directory structure
    
    prisma
      |_ schemas
      |   |_ client.prisma
      |   |_ db.prisma
      |   |_ model1.prisma
      |   |_ model2.prisma
      |   |_ model3.prisma
      |_ schema.prisma
    
    // package.json
    
    {
      "scripts": {
        "gen:schema": "awk 1 ./prisma/schemas/*.prisma > ./prisma/schema.prisma && npx prisma format",
      }
    }

    Single > character to recreate schema.prisma file without duplicates over the next generates

    but, how do u relate each other if there is relation between in different files?

  25. mdarifulislamroni521 commented on Apr 28, 2024

    @mdarifulislamroni521

    Any official updates here?

  26. janpio commented on Apr 29, 2024

    @janpio
    Contributor

    The feature request is being tracked here: #2377

  27. locked as off topic and limited conversation to collaborators on Apr 29, 2024
  28. removed their assignment
    on Apr 29, 2024
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: schema

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions