Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract a database view for streaks to enable simpler and more powerful querying #4

merged 4 commits into from Jan 12, 2017


Copy link

I ❤️ ❤️ ❤️ how you can build Ecto schemas on top of database views just like you can with tables. This let me push the data logic for streaks down into the database and allow a much simpler querying syntax (no raw SQL) in the Elixir code. A refactor of the streaks query also enabled richer querying, such as for the longest streak as well as the current one:

screen shot 2017-01-12 at 5 59 21 am

|> assoc(:streaks)
|> where([s], s.end >= ^Habits.Date.yesterday)
|> select([s], s.length)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the binding syntax (used here) would be equivalent with the bindingless syntax, e.g.

|> select([:length])

but they return different results. The binding syntax returns the bare value of the selected column, while the bindingless syntax returns an entire %Streak{} struct with only the selected attribute present.

@stevegrossi stevegrossi merged commit 5508c97 into master Jan 12, 2017
@stevegrossi stevegrossi deleted the stevegrossi/streaks_view branch January 12, 2017 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.

None yet

1 participant