question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow arbitrary enum values

See original GitHub issue

Problem

Enums currently prevent certain values from being used, requiring use of @map() to correctly reflect what actually gets stored in the database.

This causes a couple of issues:

  1. There is no Prisma-provided mechanism for accessing the underlying value, as would be needed if/when the mapped value needs to be accessed / returned / shown to users.
  2. It seems unnecessary, looking at the code that is generated for the JavaScript/TypeScript client.

eg: The following are currently not allowed:

enum Place {
  1st
  2nd
  3rd
}

enum AddressUnitType {
  "Apt."
  Suite
  Unit
}

Note: enum identifiers having embedded spaces are also not allowed.

Instead, they require the use of @map("{value}") like this:

enum Place {
  first @map("1st")
  second @map("2nd")
  third @map("3rd")
}

enum AddressUnitType {
  Apt @map("Apt.")
  Suite
  Unit
}

Additionally, since Prisma does not currently provide a supported mechanism for translating between the mapped enum values and the enum identifiers, this is another layer of translation that has to be provided.

Suggested solution

Allow Prisma enums to be defined using enclosing double-quotes if needed

eg:

enum Place {
  "1st"
  "2nd"
  "3rd"
}

enum AddressUnitType {
  "Apt."
  Suite
  Unit
}

This will avoid the need to use any other translation of the identifier to the value, too.

I do imagine that the Prisma architects are being restrictive here, and wanting to take the ‘lowest common denominator’, so that, should they want to add generators for other languages, particularly those that natively provide their own enum types, and which there, have restrictions similar to what Prisma is currently imposing.

Perhaps in that case, when the project is known to only ever need JavaScript/TypeScript client generation, there should be a configuration option that can be set to indicate relaxation of the enum value restrictions, given those restrictions will never be encountered.

Alternatives

Additional context

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:61
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

32reactions
SamuelMScommented, May 29, 2021

@janpio Considering enum types in TypeScript are currently generated as key-value pairs, could we update the generated value to be the @map representation instead?

i.e. for the schema

enum Role {
  EMPLOYEE       @map(name: "Employee")
  COMPANY_ADMIN  @map(name: "Company Admin")
}

This is what gets generated:

export const Role: {
  EMPLOYEE: 'EMPLOYEE',
  COMPANY_ADMIN: 'COMPANY_ADMIN'
};

So anything that consumes the type needs to translate COMPANY_ADMIN to Company Admin. However, no translation would be necessary if the generated type were switched to this instead:

export const Role: {
  EMPLOYEE: 'Employee',
  COMPANY_ADMIN: 'Company Admin'
};
12reactions
AndrewSouthpawcommented, Dec 8, 2022

Hey folks, I just wanted to remind people that a feature not being prioritized isn’t an excuse to be rude to others. Social shaming in comments like the one above aren’t particularly effective at getting work onto a backlog. I’m sure the Prisma team understands this feature is widely desired by the community, and will be weighing that against other work they need to do to support the product and the company. If you’re paying for premium support with Prisma, use that as an avenue to provide signals. If you’re using this awesome library for free, provide the signal by +1’ing the OP and providing feedback to team members on use cases not already covered.

We’re all in this together, let’s try to be respectful. ❤️

Read more comments on GitHub >

github_iconTop Results From Across the Web

arbitrary value for enum variable - c++
No; when converting an integer to an enumeration, the enumeration value is only specified if the integer value is within range.
Read more >
Enum HOWTO — Python 3.11.1 documentation
An Enum is a set of symbolic names bound to unique values. ... The last two options enable assigning arbitrary values to enumerations; ......
Read more >
c# - Mixing arbitrary integers and enum values
Shall I conclude that any desire to mix enum values and arbitrary integers should be a signal to find a different implementation, of...
Read more >
2363-arbitrary-enum-discriminant - The Rust RFC Book
The change is minimal: allow any variant to be adorned with an explicit discriminant value, whether or not that variant has any field....
Read more >
Access a raw value for pure enum w…
formatted in the enum Taxon . for name in setSorted { let nameComp: String = name.component! let nameType: String = ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found