Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify the Typescript type for a column, specifically a String column #7081

Open
mmahalwy opened this issue May 14, 2021 · 6 comments
Open
Labels
domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/feature A request for a new feature. topic: enum "type"/block `enum`

Comments

@mmahalwy
Copy link

mmahalwy commented May 14, 2021

Problem

Is there a way to use a String column as an enum type without creating the enum type on the db? While the enum feature is useful, making changes will require new migrations which are not ideal for often-changing enum values. I have seen in other frameworks (see this Rails gem) that use a string column but limit the values that can be set to the column via an enum.

For example, given this enum that often changes:

enum FormFieldType {
  Input
  Textarea
  Select
  ...
}

schema.prisma:

model FormField {
name String
type  String  @cast(FormFieldType)
}

This would cast a TS type (notice type key not a string):

type FormField = {
  name: string;
  type: FormFieldType;
}

This is also great when creating or updating the model where type column has an enum type, disallowing non-allowed values.

prisma.formField.create({ type: 'foo' }) // ts error
prisma.formField.create({ type:  FormFieldType.Input }) // all good!

Suggested solution

Suggestion noted above. Allow a directive for the schema to specify the cast type. Another option is to override this in the options when initializing the PrismaClient class.

Alternatives

Noted above.

Additional context

There are probably some similarities to this issue: #3219. The ask here is to override the generated Typescript types that are generated through the schema.

This also applied to array string columns where a developer may want to set the values that are allowed to be set within the array. FormFieldType[] as an example.

@mmahalwy mmahalwy changed the title Cast the Typescript type for a String column for non-DB enums Specify the Typescript type for a column, specifically a String column May 14, 2021
@janpio janpio added kind/feature A request for a new feature. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. topic: enum "type"/block `enum` labels May 18, 2021
@janpio
Copy link
Contributor

janpio commented May 18, 2021

Prisma does not support this functionality right now. A string is a string, and an enum is an enum. You could probably prevent adding unwanted values via a simple middleware, but that would not give you the type safety you are suggesting above unfortunately.

@mmahalwy
Copy link
Author

@janpio Agree, a middleware could be an option but will not solve the type-safety concerns. While I agree that a string is a string, database column types don't always translate well when it comes to typescript types (string literals, as an example, or number vs bigint/float).

We could solve this issue and this issue if there was an ability to override the types generated by the schema and give developers more flexibility on controlling the types.

@janpio
Copy link
Contributor

janpio commented May 18, 2021

(Might be worth creating a separate, explicit feature request issue for the idea "ability to override the types generated by the schema and give developers more flexibility on controlling the types" standalone @mmahalwy)

@mmahalwy
Copy link
Author

@janpio that's a good point. I will create a FR and tag you in it :)

@jkepps
Copy link

jkepps commented Sep 15, 2023

Did anything ever come of this? I am running into this exact use case

@blaykelarue
Copy link

adding an additional point here that having this ability would be greatly appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/feature A request for a new feature. topic: enum "type"/block `enum`
Projects
None yet
Development

No branches or pull requests

4 participants