Skip to content

Commit 450d6a6

Browse files
committed
added docs for JSON, JSONB, and the arrow function
1 parent 9d27dec commit 450d6a6

File tree

3 files changed

+72
-12
lines changed

3 files changed

+72
-12
lines changed

docs/src/piccolo/schema/column_types.rst

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,56 @@ Timestamp
134134
=========
135135

136136
.. autoclass:: Timestamp
137+
138+
-------------------------------------------------------------------------------
139+
140+
****
141+
JSON
142+
****
143+
144+
Storing JSON can be useful in certain situations, for example - raw API
145+
responses, data from a Javascript app, and for storing data with an unknown or
146+
changing schema.
147+
148+
====
149+
JSON
150+
====
151+
152+
.. autoclass:: JSON
153+
154+
=====
155+
JSONB
156+
=====
157+
158+
.. autoclass:: JSONB
159+
160+
arrow
161+
=====
162+
163+
``JSONB`` columns have an ``arrow`` function, which is useful for retrieving
164+
a subset of the JSON data, and for filtering in a where clause.
165+
166+
.. code-block:: python
167+
168+
# Example schema:
169+
class Booking(Table):
170+
data = JSONB()
171+
172+
Booking.create_table().run_sync()
173+
174+
# Example data:
175+
Booking.insert(
176+
Booking(data='{"name": "Alison"}'),
177+
Booking(data='{"name": "Bob"}')
178+
).run_sync()
179+
180+
# Example queries
181+
>>> Booking.select(
182+
>>> Booking.id, Booking.data.arrow('name').as_alias('name')
183+
>>> ).run_sync()
184+
[{'id': 1, 'name': '"Alison"'}, {'id': 2, 'name': '"Bob"'}]
185+
186+
>>> Booking.select(Booking.id).where(
187+
>>> Booking.data.arrow('name') == '"Alison"'
188+
>>> ).run_sync()
189+
[{'id': 1}]

piccolo/columns/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
from .column_types import ( # noqa
1+
from .column_types import ( # noqa: F401
22
Boolean,
33
Decimal,
44
Float,
55
ForeignKey,
66
Integer,
77
Interval,
8+
JSON,
9+
JSONB,
810
Numeric,
911
PrimaryKey,
1012
Real,
@@ -15,6 +17,6 @@
1517
UUID,
1618
Varchar,
1719
)
18-
from .base import Column, ForeignKeyMeta, Selectable # noqa
19-
from .base import OnDelete, OnUpdate # noqa
20-
from .combination import And, Or, Where # noqa
20+
from .base import Column, ForeignKeyMeta, Selectable # noqa: F401
21+
from .base import OnDelete, OnUpdate # noqa: F401
22+
from .combination import And, Or, Where # noqa: F401

piccolo/columns/column_types.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,9 +1046,13 @@ def __getattribute__(self, name: str):
10461046

10471047
class JSON(Column): # lgtm[py/missing-equals]
10481048
"""
1049-
Used for storing JSON strings. In Postgres, the JSON can be queried
1050-
directly. The data is stored as a string. This is preferable to JSONB
1051-
if you just want to store and retrieve JSON without querying it directly.
1049+
Used for storing JSON strings. The data is stored as text. This can be
1050+
preferable to JSONB if you just want to store and retrieve JSON without
1051+
querying it directly. It works with SQLite and Postgres.
1052+
1053+
:param default:
1054+
Either a JSON string can be provided, or a Python ``dict`` or ``list``
1055+
which is then converted to a JSON string.
10521056
10531057
"""
10541058

@@ -1079,13 +1083,14 @@ def __init__(
10791083

10801084
class JSONB(JSON):
10811085
"""
1082-
Used for storing JSON strings. In Postgres, the JSON can be queried
1083-
directly. The data is stored in a binary format, which makes querying
1084-
faster, but insertion and retrieval slower (as it needs to be converted
1085-
to and from the binary format).
1086+
Used for storing JSON strings - Postgres only. The data is stored in a
1087+
binary format, and can be queried. Insertion can be slower (as it needs to
1088+
be converted to the binary format). The benefits of JSONB generally
1089+
outweigh the downsides.
1090+
10861091
"""
10871092

1088-
def arrow(self, key: str) -> JSON:
1093+
def arrow(self, key: str) -> JSONB:
10891094
"""
10901095
Allows part of the JSON structure to be returned - for example,
10911096
for {"a": 1}, and a key value of "a", then 1 will be returned.

0 commit comments

Comments
 (0)