node.jspostgresqlprisma

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?


Solution

  • 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.