![]() ![]() ![]() The first step is actually creating the view in the database. ![]() Wouldn’t it be great if Ecto could just treat streak data with the same elegant interface I can use with Habits and CheckIns? Happily, it can! Creating the View with a Migration Awesome! Except that if I want to use this data in an Elixir function-perhaps to query the longest streak for habit-I have to copy and paste this big ball of SQL into an. +-+-+-įor every “streak”, it tells me which habit it belongs to (which makes it easy to query streaks for a given habit), when the streak started, ended, and how many days it lasted. Unfortunately, asking nicely looks like this: I only store habits and check-ins, but the database can figure out and tell us the streaks if we ask nicely. I don’t store “streaks” as a unique thing in the database. The intention-based on Jerry Seinfeld’s “don’t break the chain” motivational strategy-is that if I see that I’ve flossed 100 days in a row, I’m more likely to floss on the 101st. record that you’ve done it) once each day, and a streak is the number of days in a row that you’ve checked in. In a side project of mine, an Elixir/Phoenix app for tracking daily habits called (imaginatively) Habits, the most complicated data logic involves figuring out “streaks” of check-ins. They provide a way to treat a complex query as if it were a simple table, and with no extra work, you can build Ecto schemas on top of database views to bring the power of that abstraction into your Elixir code! Database views are a great way to provide a simplified abstraction over complicated data logic. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |