How to get enums in prisma client?

can I get a list of enums values of a model on the client-side like for select option?

Sample enum

enum user_type { superadmin admin user } 

I want this as a select-option on the client-side. How can I get them as JSON data?

1

2 Answers

You can access the user_type enum in your application code like this:

import {user_type } from "@prisma/client"; let foo: user_type = "superadmin"; // use like any other type/enum 

How you plan to connect this to the client-side or send it there is up to you. Typically Prisma types reside in the server-side of your code, not the client-side, so it might be difficult to import prisma types in your client code directly.

This is how Prisma defines the user_type enum under the hood.

// file: node_modules/.prisma/client/index.d.ts export const user_type: { superadmin: 'superadmin', admin: 'admin', user: 'user' }; 

You could just copy and paste this into your client-side code if you like.

0

When you generate Prisma Client, it generates TypeScript interfaces for your models and enum types.

you can do

import { PrismaClient, user_type } from '@prisma/client' 

and this will give you the user_type types declarations

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like