简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM