Skip to content

Instantly share code, notes, and snippets.

@wonderbeyond
Last active July 21, 2023 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wonderbeyond/cb8133c2f7ebe0ccde33d74140e389fa to your computer and use it in GitHub Desktop.
Save wonderbeyond/cb8133c2f7ebe0ccde33d74140e389fa to your computer and use it in GitHub Desktop.
Live easily with jsonb under SQLAlchemy
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