Support for ENUMs
See original GitHub issueAlthough 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:
- 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. - 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? - 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:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
Awesome. Thanks alot!