27

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?

t8101
  • 275
  • 1
  • 3
  • 5

2 Answers2

40

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.

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Tasin Ishmam
  • 5,670
  • 1
  • 23
  • 28
4

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

ben
  • 325
  • 1
  • 5
  • 15