Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library to parse schema.prisma #515

Open
olup opened this issue Sep 7, 2019 · 14 comments
Open

Library to parse schema.prisma #515

olup opened this issue Sep 7, 2019 · 14 comments
Labels
domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/feature A request for a new feature. topic: dmmf topic: schema topic: sdk

Comments

@olup
Copy link

olup commented Sep 7, 2019

Having read #318 and toying myself with the idea of a simple admin panel to create models with fields and relations, generate a nexus-prisma revolvers file and trigger database migrations (akin to Strapi or 8base, but much simpler) I was wondering if you open-sourced a library to parse the schema.prisma into something usable by code (I mean, ultimately, a javascript object) ? That would permit third party developer to create tools to plug into the prisma 2 environment.

Also, I was wondering why this format have been chosen over simple JSON or YAML/TOML structure ?

My love goes to this project.

@olup
Copy link
Author

olup commented Sep 7, 2019

Actually, I am guessing generators are a way to do that...

@pantharshit00
Copy link
Contributor

pantharshit00 commented Sep 9, 2019

We are aiming for a Prisma SDK release that will provide a general library for this purpose. (https://github.com/prisma/specs/tree/master/sdk-js/generators)

Right now all of our datamodel parsing logic is written in Rust(https://github.com/prisma/prisma-engine/tree/master/libs/datamodel) so you will need an RPC call to the binary to retrieve a parsed datamodel but SDK will simplify this flow.

Actually right now photon also gives you the parsed dmmf(datamodel meta format) as an exported variable which you can consume for now.

@pantharshit00 pantharshit00 added the kind/feature A request for a new feature. label Sep 9, 2019
@schickling
Copy link
Member

Looping in @Weakky as he's working on the Prisma SDK.

timsuchanek pushed a commit that referenced this issue Apr 14, 2020
Fix Error's meta property typing

Former-commit-id: 8dcb00e
timsuchanek pushed a commit that referenced this issue Apr 14, 2020
Fix Error's meta property typing

Former-commit-id: 8dcb00e
Former-commit-id: 58e907d
@jbmusso
Copy link

jbmusso commented Apr 25, 2020

Just discussed this with @Weakky recently. Our use case: we need to programmatically make some tweaks to the introspected schema, tweaks which Prisma doesn't (and arguably shouldn't) natively support:

  • The database has almost no foreign key, though the db schema consistently uses a column name convention across tables which makes it trivial to recreate relations out of column names.
  • The database has numerical enums which we could either prefix with a fixed constant, or rename.

We could either:

  • Simply parse the schema and write something else to the file system, that'd be fairly easy.
  • Hook into Prisma with a low-level/advanced mode postIntrospection phase in which we inject things into the AST: some custom JS/TS we are responsible of gets executed, and we're set.

Database schema is currently managed by Sequelize, and the first step of transitioning to Prisma would be to keep Sequelize manage the schema while we progressively port queries to Prisma, until Prisma manages the rest.

@pantharshit00
Copy link
Contributor

pantharshit00 commented Apr 27, 2020

This is something that is not documented but you can use the @prisma/sdk package to parse the datamodel into dmmf(datamodel meta format) right now

import { getDMMF } from '@prisma/sdk'

const dmmf = getDMMF({ datamodel: "model User { id String @id }" })

Note that this is subject to change without any notification. Also, note that SDK doesn't come with a DMMF printer.

@michaellzc
Copy link

michaellzc commented May 22, 2020

This is something that is not documented but you can use the @prisma/sdk package to parse the datamodel into dmmf(datamodel meta format) right now

import { getDMMF } from '@prisma/sdk'

const dmmf = getDMMF({ datamodel: "model User { id String @id }" })

Note that this is subject to change without any notification. Also, note that SDK doesn't come with a DMMF printer.

I am playing around with the getDMMF method to see if there is a better way to automatically transform the model name and field name from snake_case to camelCase.

My idea is to transform the model and field name in the DMMF.Datamodel.models from snake_case to camelCase, then modify necessary attributes in the model to @map the modified field back to the original one. Once the transformation is done, I can just deserialize the model back to the Prisma schema file. (I am aware that there's no built-in deserializer in @prisma/sdk, so I implemented one myself)

However, I can't seem to find anything in the output dmmf to represent the @map attribute. Is this expected? I wonder how Prisma handle the @map attribute in DMMF.

Here's the input schema and the dump of the DMMF, https://gist.github.com/ExiaSR/7e7c8a56bee768410f5ea024da39d9e9

For now I guess I could add an additional property in DMMF.Field to represent the @map attribute, then make my deserializer work with it.

@olup
Copy link
Author

olup commented May 23, 2020

@ExiaSR i'll be interested in seeing your deserialiser to prisma schema file. Is it published somewhere ?

@michaellzc
Copy link

michaellzc commented May 23, 2020

@ExiaSR i'll be interested in seeing your deserialiser to prisma schema file. Is it published somewhere ?

@olup It's undergoing internal approval for Open Source, so stay tuned (expecting sometime next week).

@michaellzc
Copy link

@ExiaSR i'll be interested in seeing your deserialiser to prisma schema file. Is it published somewhere ?

@olup https://github.com/IBM/prisma-schema-transformer

@m4rvr
Copy link

m4rvr commented Sep 13, 2020

Is there anything like the deserializer planned for the @prisma/sdk? Would be really cool to feed in the DMMF and get a complete schema.prisma (string?) back. :)

@bombillazo
Copy link

An exposed and documented Prisma serializer/deserializer would be great for the community to build tools around Prisma. It would be great to pass the schema through a custom transformer function to output a GraphQL schema, to name a very common use case.

@janpio janpio changed the title Library to parse schema.prisma Library to parse schema.prisma Nov 18, 2021
@florianmartens
Copy link

I agree, this would fuel innovation and truely distinguish Prisma from other ORMs.

@dineshgadge
Copy link

This library does a fairly decent job of parsing, modifying and reprinting the prisma schema: https://github.com/MrLeebo/prisma-ast

@trevorr
Copy link

trevorr commented Sep 15, 2022

We just released another complete Prisma schema parser/formatter written in Typescript as well: https://github.com/loancrate/prisma-schema-parser

It doesn't have the fluent schema builder of the one above, but is more directly based on the PEG grammar of the Prisma engine. It also has helpers for accessing attribute values, tracking of source locations, and 100% test coverage.

We used it for migrating to a GraphQL SDL-centric workflow, where Prisma schema, API schema, validation, documentation, etc. are based on a core SDL annotated with directives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/feature A request for a new feature. topic: dmmf topic: schema topic: sdk
Projects
None yet
Development

No branches or pull requests