Add support for currently unsupported JSON/JSONB operators · Issue #3032 · diese...
source link: https://github.com/diesel-rs/diesel/issues/3032
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Diesel currently supports the postgres json and jsonb types. We do not provide built-in support for various operators available for these types. This is a tracking issue for adding support for these operators.
The general strategy for adding support for new operators is as following:
- Define the operator via
infix_operator!()
. The contains operator would be defined as following:infix_operator!(KeyExists, " ? ", backend: Pg);
These operators can be defined here. If there is already an existing definition, this step could be skipped. - Define a new helper type here. Helper types are a tool for diesel users to simplify type definitions. Again if there is already a matching type definition this could be skipped.
- Add a new
ExpressionMethods
extension trait for the corresponding types. This only needs to happen once forjsonb
and once forjsonb
+json
. See thePgRangeExpressionMethods
trait here as example. - Add a new method to the corresponding
ExpressionMethods
trait. See here for an example for a different operator/type. This definition should:- Use the previously defined helper type
- Contain a documentation with an example
- Check if compile test output is up to date by running
TRYBUILD=overwrite cargo test
inside ofdiesel_compile_tests
. (This is required if you've added a new operator based oninfix_operator!
- Submit a PR with the change
Operator list:
-
json/jsonb -> integer
(indexing by index, returnsjson/jsonb
) ** -
json/jsonb -> text
(indexing by key name, returnsjson/jsonb
) ** -
json/jsonb ->> integer
(indexing by index, returns field astext
) ** -
json/jsonb ->> text
(indexing by key name, returns field astext
) ** -
json/jsonb #> text[]
(indexing by path, returns field asjson/jsonb
) * -
json/jsonb #>> text[]
(indexing by path, returns field astext
) * -
jsonb @> jsonb
(checks if the second value is contained by the first value, returns aboolean
) * -
jsonb <@ jsonb
(checks if the first value is contained by the second value, returns aboolean
) * -
jsonb ? text
(checks if a string exists as top level key in the given json, returns aboolean
) * -
jsonb ?| text[]
(checks if any of the strings exists as top level key in the given json, returns aboolean
) * -
jsonb ?& text[]
(checks if all of the strings exist as top level key in the given json, returns aboolean
) * -
jsonb || jsonb
(concats two json values, returnsjsonb
) * -
jsonb - text
(remove the key given as string from the json, returnsjsonb
) ** -
jsonb - text[]
(removes all keys given as string from the json, returnsjsonb
) ** -
jsonb - integer
(removes the field with the given index from the json, returnsjsonb
) ** -
jsonb #- text[]
(removes the given path from the json, returnsjsonb
) * -
jsonb @? jsonpath
(Does JSON path return any item for the specified JSON value?, returns aboolean
) *** -
jsonb @@ jsonpath (Returns the result of a JSON path predicate check for the specified JSON value. Only the first item of the result is taken into account. If the result is not Boolean, then NULL is returned., returns a
boolean`) ***
Items marked with one *
can follow the instructions directly, items marked with **
require additional work to unify the different input types behind a common operator/function call (See here for a general example on how to abstract over expressions of different types), items marked with ***
likely require additional design work.
Please add a comment to this issue if you plan to work on a specific operator.
If there is anything unclear about how to add a support for a specific operator just ask and we will try to answer your questions.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK