Repository navigation
Replies: 3 comments 2 replies
|
Hey @jezsung 👋, When you say:
what exactly do you want to be in the array? Do you want an array of the possible enum values? |
2 replies
|
@jezsung this is a very crude example but it helped me // path: prisma/generators/enum-array.ts
import pkg from "@prisma/generator-helper";
import { writeFileSync } from "node:fs";
import { resolve } from "node:path";
const { generatorHandler } = pkg;
generatorHandler({
onManifest() {
return {
defaultOutput: "../generated/enums.ts",
prettyName: "Prisma Enums Generator",
};
},
async onGenerate(options) {
// Build JS content
let content = "// THIS FILE IS AUTO-GENERATED FROM PRISMA ENUMS\n\n";
options.dmmf.datamodel.enums.forEach((enm) => {
const name = enm.name;
const values = enm.values.map((v) => `"${v.name}"`).join(", ");
content += `export const ${name}EnumArray = [${values}] as const;\n\n`;
});
writeFileSync(resolve("generated", "enums.ts"), content, "utf-8");
},
});If you have this in your schema // path: prisma/schema.prisma
generator enums {
provider = "tsx ./prisma/generators/enum-array.ts"
}
enum BlogStatus {
published
drafted
}
enum BlogCategory {
Company
Product
Tutorial
Engineering
Opinion
Research
Launch
Culture
Marketing
Event
FAQ
Storytelling
Policy
Playground
}Once you generate, you get the resulting code... // path: generated/enums.ts
// THIS FILE IS AUTO-GENERATED FROM PRISMA ENUMS
export const BlogStatusEnumArray = ["published", "drafted"] as const;
export const BlogCategoryEnumArray = ["Company", "Product", "Tutorial", "Engineering", "Opinion", "Research", "Launch", "Culture", "Marketing", "Event", "FAQ", "Storytelling", "Policy", "Playground"] as const;I hope this helps out |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment



Uh oh!
There was an error while loading. Please reload this page.
I'm looking for the simplest solution. The one possible solution I came up with is
But the generated array is an array of string. It doesn't fully utilize Typescript. I want it to be an array of the enum type.
All reactions