Skip to content

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

Merged
stevegrossi merged 4 commits into
masterfrom
stevegrossi/streaks_view
Jan 12, 2017
Merged

Extract a database view for streaks to enable simpler and more powerful querying#4
stevegrossi merged 4 commits into
masterfrom
stevegrossi/streaks_view

Conversation

@stevegrossi

Copy link
Copy Markdown
Owner

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

Comment thread web/models/habit.ex
habit
|> assoc(:streaks)
|> where([s], s.end >= ^Habits.Date.yesterday)
|> select([s], s.length)

Copy link
Copy Markdown
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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant