1.1.0 • Published 1 year ago

prisma-generator-t3-enums v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Motivation

Working with enums can be a foot gun! Theo Browne explains this well in his "TypeScript's Worst Feature (And How To Avoid It)" video.

The purpose of this is to allow Prisma to be the source of truth when it comes enums. when running prisma generate the goal is to generate an out put such as this

import * as z from 'zod'

export const language = [
  'Typescript',
  'Javascript',
  'Rust',
  'Go',
  'Python',
  'Cpp',
] as const

export type Language = typeof language[number]

export const languageSchema = z.enum(language)

This way you can reference these values in code to populate iterate over options, validate their shape, or ensure type safety.

Usage

install

zod is prety awesome, it'll be required for the z.enum() provided by the generator.

npm i prisma-generator-t3-enums zod

Prisma Schema

add the generator to the schema.prisma. You can define the output dir of the generated types by replacing the "../types" value.

generator custom_generator {
  provider = "prisma-generator-t3-enums"
  output   = "../types"
}

use npx prisma generate to generate the enum exports

This generator was bootstraped using create-prisma-generator