How to import enums from prisma?
I have an enum defined in my prisma file, but
import { User , OrganizationUserRole} from 'wasp/entities';
isn't working9 Replies
Hi @Noah. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
I apologize, but based on the provided knowledge sources, there isn't currently direct support for importing Prisma enums in Wasp. The ability to use Prisma enums is a feature that is not yet implemented in Wasp, but it's on their roadmap for future development.
According to the Github issue tracking Prisma support expansion, support for
enum
declarations in PSL (Prisma Schema Language) is one of the items they plan to add in the future.
As a workaround, one user suggested creating a seeder function that uses $executeRawUnsafe
to create the enum in the database, and upgrading to Prisma 5.10.2. However, this is not an official solution and may have limitations.
The Wasp team is aware of this limitation and plans to address it in future updates. They're working on a larger overhaul of how Prisma is used in Wasp, which should unlock many features including enum support.
For now, you may need to find alternative ways to represent the data in your Wasp project without using Prisma enums. If this feature is critical for your project, you might want to keep an eye on Wasp's upcoming releases or consider reaching out to the Wasp team on their Discord channel for more specific advice on workarounds.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @Noah, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?is the bot right here @miho ?
The bot is not 100% correct, we support
enum
s in the Prisma file.
You can't import them from wasp/entities
... you can import them from @prisma/client
but only on the server.
e.g.
import { Enum } from '@prisma/client';
@miho Can I just check something? I'm importing Prisma enum types in client-side code for type checking, e.g.
JobActions.tsx:
Is this a practise I should avoid?
If you are importing them using
import type
you are good đź‘Ť Typescript strips away the type imports which then can't cause issues in the browser. Imports of server-side values like enum
values can't be stripped and cause issues.Great, thanks. I did try importing the enums initially and discovered that wouldn't work, so I use const object literals with the same values for value checking instead.
what would be an example of that? so if, e.g., an enum had a value
API_KEY: process.env.API_KEY
it would strip that out when using import type
on the client?import type
can only be used to import types and not values. Typescript compiler strips out all type info when compiling to Javascript.
For example, our full-stack type safety relies on this. Normally we aren't allowed to import stuff from the server on the client, but if it's only types when the code reaches the browser - it no longer has type imports and it's completely valid to run in the browser.
import type
is a nice way of giving extra info to the Typescript compiler "hey, I'm only importing types here, you can get rid of this whole import statement"ah ok. I use it all the time and never knew the difference 🙂