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

Adds 2 new JSON critera query methods #1016

Merged
merged 4 commits into from
Mar 30, 2024
Merged

Adds 2 new JSON critera query methods #1016

merged 4 commits into from
Mar 30, 2024

Conversation

jwoertink
Copy link
Member

Fixes #197

This PR adds 2 more query methods following #1015

  • includes which performs WHERE jsonb @> '{"some":"json"}'
  • in which performs WHERE jsonb <@ '{"some":"json"}'

Also while I was in there I decided to rename the previously added Where classes from JSON.. to Jsonb.... While reading the class names that seemed to make it a little more clear for me.

class User < BaseModel
  table do
    column preferences : JSON::Any
  end
end

# query where preferences includes this json in it
UserQuery.new.preferences.includes(JSON::Any.new({"theme" => JSON::Any.new("dark")}))

# query where preferences is included in this json
UserQuery.new.preferences.in(JSON::Any.new({"theme" => JSON::Any.new("dark")}))

…riction so different json-able types can be passed in (i.e. Hash or JSON::Any)
Comment on lines +320 to +323
class JsonbIncludes < ValueHoldingSqlClause
def operator : String
"@>"
end
Copy link
Member Author

Choose a reason for hiding this comment

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

One thing I was just thinking about... This operator can be used on Arrays too. Ref... If this just became the new Includes then it would work for both Arrays and Jsonb, but that also means that the old Includes using val = ANY(col) would not be an option without some sort of flag or rename or something...

Copy link
Contributor

Choose a reason for hiding this comment

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

If they're equivalent operators, I see no reason not to simplify.

@jwoertink jwoertink merged commit f24ae0e into main Mar 30, 2024
9 checks passed
@jwoertink jwoertink deleted the issues/197_b branch March 30, 2024 16:24
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.

jsonb special query operators
2 participants