-
-
Notifications
You must be signed in to change notification settings - Fork 128
Description
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:
enum UserRole {
Admin
User
}
model User {
id Int @id @default(autoincrement())
username String @unique
role UserRole
}
For the SqlServer case, it becomes:
model User {
id Int @id @default(autoincrement())
username String @unique
role String
}
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:
model User {
id Int @id @default(autoincrement())
username String @unique
role String @enum("Admin | User")
}
This would generate TypeScript using z.infer<UserModel> as follows:
enum UserRole {
Admin,
User
}
interface UserModel {
id: number;
username: string;
role: UserRole;
}
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.