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 new JSON::Any criteria methods for querying jsonb columns #1015

Merged
merged 1 commit into from
Mar 23, 2024

Conversation

jwoertink
Copy link
Member

@jwoertink jwoertink commented Mar 23, 2024

Ref #197

This PR adds in 3 new criteria method for JSON::Any columns

  • has_key which performs WHERE jsonb ? 'some_key'.
  • has_any_keys which performs WHERE jsonb ?| '{"this_key", "or_that_key"}'
  • has_all_keys which performs WHERE jsonb ?& '{"this_key", "and_this_key"}'

This makes doing queries on JSON columns a little easier since you can't do raw WHERE queries on these because they use the ? character which the PG shard uses as a placeholder ref

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

UserQuery.new.preferences.has_key("theme")
UserQuery.new.preferences.has_any_keys(["theme", "color_mode"])
UserQuery.new.preferences.has_all_keys(["theme", "style"])

Side note: I'm not having this PR close the issue because I want to add 2 more in a separate PR to perform @> and <@

@jwoertink jwoertink merged commit 4dd49ea into main Mar 23, 2024
9 checks passed
@jwoertink jwoertink deleted the issues/197_a branch March 23, 2024 19:35
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