crystal-db
crystal-db copied to clipboard
Allow the use of enums
I often want one or more properties of my DB-backed models to be constrained to a low-cardinality set of values, which can be known at compile time. Usually I end up storing them in strings, but then I can't lean on the Crystal type system to help me constrain those values throughout my application.
Since the JSON module supports Crystal enums, I looked into adding support for them here, as well. With this I can remove the translation layer between the HTTP layer and the DB layer if the only difference between them is an enum.
I was originally going to add a macro so you'd do something like DB.register_type MyEnum, but then I remembered that Enum is itself a Crystal type that represents all enums to the exclusion of all else, which made it really simple to support without doing any extra work.
Found a bug in the error message while using this, so I just pushed up a couple commits to touch that up.