Last active
July 21, 2023 08:08
-
-
Save wonderbeyond/cb8133c2f7ebe0ccde33d74140e389fa to your computer and use it in GitHub Desktop.
Live easily with jsonb under SQLAlchemy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Any | |
import sqlalchemy as sa | |
from sqlalchemy.dialects.postgresql import array as pg_array, JSONB | |
def jsonb_set(target, path: list[str], val: Any): | |
""" | |
An easy wrapper over sa.func.jsonb_set(). | |
Example: | |
>>> jsonb_set(Instrument.state, ['status'], 'Running') | |
""" | |
return sa.func.jsonb_set(target, pg_array(path), sa.cast(val, JSONB)) | |
def jsonb_concat(a, b, /): | |
return a.op('||')(b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment