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

Add new includes method for Array queries. #733

Merged
merged 2 commits into from
Oct 2, 2021
Merged

Add new includes method for Array queries. #733

merged 2 commits into from
Oct 2, 2021

Conversation

jwoertink
Copy link
Member

Fixes #640

I'm only adding the includes method for now, and with somewhat limited support. One issue I would like to figure out before going full support is how to catch this method at compile-time so you only call it on Array columns. I tried several different patterns, but there's a few issues with it that I'll open up in a separate issue.

This PR allows you query for records with Array columns where a specific value exists in that column.

# column blocked_on_pages : Array(Int64)
UserQuery.new.blocked_on_pages.includes(current_page.id).any?

If you try to call this on a non-array column, Postgres will throw a runtime error when you run the query.

Comment on lines +11 to +14
private def check_using_array!(_value : Array.class)
end

private def check_using_array!(_value)
Copy link
Member Author

Choose a reason for hiding this comment

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

These aren't actually used right now, but I wanted to leave them in as a template to the compile-time pattern. I do plan on eventually figuring out how to use it.

module Avram::IncludesCriteria(T, V)
macro included
# WHERE `value` = ANY(column)
def includes(value) : T
Copy link
Member Author

Choose a reason for hiding this comment

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

I also debated on contains, but with includes, we could also add an excludes method... What would be the opposite of contains, omits? 🤷‍♂️

Copy link
Contributor

Choose a reason for hiding this comment

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

The Avram DSL here is close enough to sql that I think includes makes sense.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, I misunderstood the purpose of this when I said that. But I still think that includes is as good a name as any. Sorry for the email spam from my github comments 🤪

Copy link
Member Author

Choose a reason for hiding this comment

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

😂 cool. Second confirmation is what I needed. Thanks

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.

New method to query for value inside of array column
2 participants