-
-
Notifications
You must be signed in to change notification settings - Fork 93
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
[Feature Request] SQL Server Enum Support in ZenStack #763
Comments
Hey @Val0429 , thanks for bringing this up! I wasn't aware enum is missing for SQLSever too ... Really inconvenient for me at least working with sqlite. Using an attribute to annotate is a great idea. How are you trying to tackle it today and what limitation did you run into with Zod? I'm thinking if the main thing you care about is the generated zod, maybe you can make a derivation to the generated zod schema and enforces enums there. I'm even thinking, since ZenStack is already a superset of Prisma, maybe we should just polyfill and make |
Hello @ymc9, appreciate your interest and attention to this request! Thank you! 😊
It's also the best option for me. for example, in the schema.zmodel file:
If
And in the Zod interface (for TypeScript):
Else, If the database allows enum, everything stays as usual. Most developers in this case are likely more interested in TypeScript type safety rather than whether the enum is stored inside the database. Introducing this polyfill concept in ZenStack would improve the way Prisma handles enums, making the overall experience smoother. |
I tried to breakdown the workload of this task, 1. Add Global Config for ZenStackIn Prisma, there's a global config in the
link: https://www.prisma.io/docs/reference/api-reference/command-reference To support this in ZenStack, we can have a similar setup in the
2. Modify ZenStack VSCode PluginShow the message "The current connector does not support enums" only if 3. Modify @core/prisma to Support Enum PolyfillAdjust
into:
Only do this translation if the database connector doesn't natively support enums. 4. Modify @core/zodPossibly, there might be a need to modify |
Thanks for the detailed breakdown! Really appreciate it. One of the first things that needs to be done is to interfere with how Prisma generates its TS types (those under "node_modules/.prisma" folder). In previous versions of ZenStack we've been avoiding to do this to avoid extra complexities, but I'm aware that we can't go too far without doing that. Polyf-illing enum can be a good first try on that route. |
Hi @ymc9, I believe there's a potential to make the initial attempt at the polyfill feature more effective by making some adjustments in the In the I'm suggesting this as a potential improvement, although I haven't delved deeply into the code. It's just a thought I wanted to share. |
Problem
Prisma removed support for Enum in Sqlite and SqlServer back in 2020, leading to increased boilerplate code and a lack of a single source of truth. For instance, the data model, initially defined as:
For the SqlServer case, it becomes:
This means that I lose all type definitions of UserRole in TypeScript and have to manually add them, resulting in more repetitive code and a lack of a single source of truth.
There's a lengthy discussion on this issue in the Prisma GitHub repository: Link, and it's been "open" for three years. This issue is also a roadblock for me in using Prisma until I discovered Zenstack. I believe the "custom attribute" feature in Zenstack could potentially resolve this problem.
What I've Tried
Upon delving into the ZenStack codebase, I noticed a limitation in customizing
@core/zod
to work seamlessly with my custom attribute, thereby restricting the flexibility I require.While the option of forking the code and making my own modifications is available, it comes with the drawback of potentially losing out on future updates and improvements to this excellent codebase.
Suggested solution
I propose adding a way to define an "enum" type for a field, like the "role" field in the example above. For instance:
This would generate TypeScript using
z.infer<UserModel>
as follows:This approach would allow us to maintain a single source of truth (the zmodel file) and achieve strong type safety, for example, with Typia and Nestia. It will also position ZenStack as an essential and powerful superset of Prisma.
The text was updated successfully, but these errors were encountered: