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
merged 4 commits into from Jan 12, 2017

Conversation

Projects
None yet
1 participant
@stevegrossi
Owner

stevegrossi commented Jan 12, 2017

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

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

This comment has been minimized.

@stevegrossi

stevegrossi Jan 12, 2017

Owner

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 Jan 12, 2017

Owner

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

1 check passed

continuous-integration/codeship Build succeeded
Details

@stevegrossi stevegrossi deleted the stevegrossi/streaks_view branch Jan 12, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment