-
Notifications
You must be signed in to change notification settings - Fork 64
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
jsonb special query operators #197
Comments
To along with this, I just ran in to an issue with rails. If you want to check that a jsonb column has a specific key, postgres uses User.where("tags ? 'active'").count #=> 100
User.where("tags ? '?'", "active").count #=> Wrong number of bind args exception For Avram, it would be super helpful to have a method that does this for us, but in a type safe and secure way. # This would be nice. Or something along these lines.
UserQuery.new.tags.has_key("active").select_count |
I like the idea of using method missing to create query methods. UserQuery.new.tags.active_key("active").select_count This would be a little bit of magic but I think it would fit into the API well. |
I'd vote against using |
Agreed. I like the suggestion @wontruefree, but there was some talk floating around of method missing going away at some point. Plus, we want to avoid random bits of "magic". I think we may be able to even get a bit more type-safe with this if #695 gets approved. In the case of the example, Just half-baked, but something like: struct UserTagsJSON
include JSON::Serializable
property active : Bool
end
# We know tags would be a serialized object
# If we pass a block, then send the serialized instance down
# and build a query from that...
UserQuery.new.tags(&.active(true)).select_count No clue how that'd work, but might be kinda neat and definitely unique. |
We can now map to jsonb fields using
JSON::Any
in our migrations and columns, but currently querying on those fields is limited to normal WHERE syntax, and some hefty crystal:Postgres supports several different operators for doing special queries on JSON https://www.postgresql.org/docs/current/functions-json.html
We need some type safe ways to do things like "Give me all users that have dark_mode as their theme preference" and such.
The text was updated successfully, but these errors were encountered: