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.

Support for ENUMs

See original GitHub issue

Although not a part of the SQL standard, enumerated values are a common feature in domain models, are supported in some ways by most databases, and can be expressed with the TypeScript typing system. Enums in databases currently supported by sql-ts come at least in three forms:

  1. The PostgreSQL TYPE ... AS ENUM, which is a named, stand-alone data type that can be shared by multiple columns in different tables. This pretty clearly maps to a TypeScript string enum, which can then be referred to in the individual table interfaces.
  2. The MySQL ENUM(...) column type, which is anonymous. It is conceptually similar to a TypeScript string union (ENUM('a', 'b', 'c') -> 'a' | 'b' | 'c'), but perhaps using a string enum for these as well would be desirable for consistency?
  3. Using a check constraint to limit the column to certain values – the only enum implementation available for SQL Server and SQLite, and preferred by some with PostgreSQL. I don’t know if handling these would be feasible, as check constraints are a very expressive construct, and detecting whether one is to be considered an “enum” or not would probably be difficult.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
rmp135commented, Jun 8, 2020

ENUM support for Postgres has been added to 1.5.0. I don’t have a lot of experience with Postgres but I’ve think I got it working. If you reference an ENUM in a different schema or if multiple exist with the same name, use the schemaAsNamespace config option.

I may implement MySQL ENUMs as string unions as was suggested in the original ticket if there’s more interest.

0reactions
daniellemburgLWTcommented, Jun 9, 2020

Awesome. Thanks alot!

Read more comments on GitHub >

github_iconTop Results From Across the Web

enum — Support for enumerations — Python 3.11.1 ...
enum — Support for enumerations¶ · is a set of symbolic names (members) bound to unique values · can be iterated over to...
Read more >
Handbook - Enums - TypeScript
Enums allow a developer to define a set of named constants. Using enums can make it easier to document intent, or create a...
Read more >
Documentation: 15: 8.7. Enumerated Types - PostgreSQL
Enumerated (enum) types are data types that comprise a static, ordered set of values. They are equivalent to the enum types supported in...
Read more >
Extending Enums in Java - Baeldung
In this article, we discussed why enums don't support inheritance. Then we addressed how to emulate extensible enums with interfaces.
Read more >
Enum Types - Java™ Tutorials
An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must...
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