Skip to content

Adds new JSON::Any criteria methods for querying jsonb columns#1015

Merged
jwoertink merged 1 commit intomainfrom
issues/197_a
Mar 23, 2024
Merged

Adds new JSON::Any criteria methods for querying jsonb columns#1015
jwoertink merged 1 commit intomainfrom
issues/197_a

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 <@

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