In this episode, we're diving into how to use enums to handle endpoint frequencies in our app. Instead of using random integers for frequency values in our database, we set up an enum to keep things organized and consistent. This way, we have a central place where all possible frequency options (like 1 minute, 5 minutes, 30 minutes, etc.) are defined, making our code easier to maintain and less prone to errors.
We go through creating an EndpointFrequency
enum, storing it in its own directory, and defining the different frequency options using seconds as values (for example, ONE_MINUTE = 60
, FIVE_MINUTES = 5*60
, etc.). This not only makes the code more readable, but it also helps when we need to display user-friendly labels in the UI or validate input.
After setting up the enum, we update our database migration for the endpoints table so that the default frequency value now comes from the enum—no more hardcoded magic numbers! We see how to reference the enum value for, say, five minutes, and use it as the default in the table.
By the end of this video, we’re already using our enum to power migrations and set defaults, laying the groundwork to use these values all over the app, whether it’s for validation or generating dropdowns in forms. If you’re new to enums, don’t worry—we go step by step and you’ll see just how useful this approach can be as your app grows.