crystal-db icon indicating copy to clipboard operation
crystal-db copied to clipboard

Allow the use of enums

Open jgaskins opened this issue 3 years ago • 1 comments

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.

jgaskins avatar Apr 18 '22 03:04 jgaskins

Found a bug in the error message while using this, so I just pushed up a couple commits to touch that up.

jgaskins avatar Apr 19 '22 13:04 jgaskins