diff --git a/docs/peewee/sqlite_ext.rst b/docs/peewee/sqlite_ext.rst index 6e8140ad7..106fbde96 100644 --- a/docs/peewee/sqlite_ext.rst +++ b/docs/peewee/sqlite_ext.rst @@ -840,6 +840,35 @@ APIs JSON object at the given location. See also :py:meth:`JSONField.tree`. +.. py:class:: JSONBField(json_dumps=None, json_loads=None, ...) + + Field-class suitable for use with data stored on-disk in ``jsonb`` format + (available starting Sqlite 3.45.0). This field-class should be used with + care, as the data may be returned in it's encoded format depending on how + you query it. For example: + + .. code-block:: pycon + + >>> KV.create(key='a', value={'k1': 'v1'}) + + >>> KV.get(KV.key == 'a').value + b"l'k1'v1" + + To get the JSON value, it is necessary to use ``fn.json()`` or the helper + :py:meth:`JSONBField.json` method: + + .. code-block:: pycon + + >>> kv = KV.select(KV.value.json()).get() + >>> kv.value + {'k1': 'v1'} + + +.. py:class:: JSONBPath(field[, path=None]) + + Subclass of :py:class:`JSONPath` for working with ``jsonb`` data. + + .. py:class:: SearchField([unindexed=False[, column_name=None]]) Field-class to be used for columns on models representing full-text search diff --git a/playhouse/sqlite_ext.py b/playhouse/sqlite_ext.py index 6ecabde49..49dc7084e 100644 --- a/playhouse/sqlite_ext.py +++ b/playhouse/sqlite_ext.py @@ -270,6 +270,9 @@ def db_value(self, value): value = fn.jsonb(self._json_dumps(value)) return value + def json(self): + return fn.json(self) + def extract(self, *paths): paths = [Value(p, converter=False) for p in paths] return fn.jsonb_extract(self, *paths)