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

JSONB Examples? #48

Open
CloudNiner opened this issue Feb 12, 2024 · 2 comments
Open

JSONB Examples? #48

CloudNiner opened this issue Feb 12, 2024 · 2 comments

Comments

@CloudNiner
Copy link

Hi -- dug through the code, tests, and README and didn't see any JSONB examples.

We've effectively resorted to large RawDangerous() blocks which is a concern.

Any chance the README could provide at minimum a few examples for using this library to work with JSONB queries?

@jp-u
Copy link

jp-u commented Aug 14, 2024

JSONB examples would be really nice!

@jp-u
Copy link

jp-u commented Aug 22, 2024

I had some success using "JSONB Containment and Existence". This can be accomplished using the contains method, which maps to the @> (duck) operator, and was able to avoid using RawDangerous.

Suppose you have a JSONB column called parameters. You could build a filter using JSONB containment of another JSON document.

V("parameters").contains(json.dumps({"pokemon_type":"fire"}))

If instead pokemon_types is a list of strings, the following would match where pokemon_types includes both "fire" and "water".

V("parameters").contains(json.dumps({"pokemon_types":["fire", "water"]}))

If you wanted to match where pokemon_types includes either "fire" OR "water", you can match against a JSON document containing each one:

funcs.OR(
    V("parameters").contains(json.dumps({"pokemon_types":["fire"]})),
    V("parameters").contains(json.dumps({"pokemon_types":["water"]}))

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

No branches or pull requests

2 participants