mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
?_shape=arrayfirst, closes #287
This commit is contained in:
parent
b463f60158
commit
276913b748
3 changed files with 19 additions and 1 deletions
|
|
@ -185,7 +185,9 @@ class BaseView(RenderMixin):
|
||||||
|
|
||||||
# Deal with the _shape option
|
# Deal with the _shape option
|
||||||
shape = request.args.get("_shape", "arrays")
|
shape = request.args.get("_shape", "arrays")
|
||||||
if shape in ("objects", "object", "array"):
|
if shape == "arrayfirst":
|
||||||
|
data = [row[0] for row in data["rows"]]
|
||||||
|
elif shape in ("objects", "object", "array"):
|
||||||
columns = data.get("columns")
|
columns = data.get("columns")
|
||||||
rows = data.get("rows")
|
rows = data.get("rows")
|
||||||
if rows and columns:
|
if rows and columns:
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,7 @@ options:
|
||||||
* ``?_shape=arrays`` - ``"rows"`` is the default option, shown above
|
* ``?_shape=arrays`` - ``"rows"`` is the default option, shown above
|
||||||
* ``?_shape=objects`` - ``"rows"`` is a list of JSON key/value objects
|
* ``?_shape=objects`` - ``"rows"`` is a list of JSON key/value objects
|
||||||
* ``?_shape=array`` - the entire response is an array of objects
|
* ``?_shape=array`` - the entire response is an array of objects
|
||||||
|
* ``?_shape=arrayfirst`` - the entire response is a flat JSON array containing just the first value from each row
|
||||||
* ``?_shape=object`` - the entire response is a JSON object keyed using the primary keys of the rows
|
* ``?_shape=object`` - the entire response is a JSON object keyed using the primary keys of the rows
|
||||||
|
|
||||||
``objects`` looks like this::
|
``objects`` looks like this::
|
||||||
|
|
@ -102,6 +103,10 @@ options:
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
``arrayfirst`` looks like this::
|
||||||
|
|
||||||
|
[1, 2, 3]
|
||||||
|
|
||||||
``object`` looks like this::
|
``object`` looks like this::
|
||||||
|
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -433,6 +433,17 @@ def test_table_shape_arrays(app_client):
|
||||||
] == response.json['rows']
|
] == response.json['rows']
|
||||||
|
|
||||||
|
|
||||||
|
def test_table_shape_arrayfirst(app_client):
|
||||||
|
response = app_client.get(
|
||||||
|
'/test_tables.json?' + urllib.parse.urlencode({
|
||||||
|
'sql': 'select content from simple_primary_key order by id',
|
||||||
|
'_shape': 'arrayfirst'
|
||||||
|
}),
|
||||||
|
gather_request=False
|
||||||
|
)
|
||||||
|
assert ['hello', 'world', ''] == response.json
|
||||||
|
|
||||||
|
|
||||||
def test_table_shape_objects(app_client):
|
def test_table_shape_objects(app_client):
|
||||||
response = app_client.get(
|
response = app_client.get(
|
||||||
'/test_tables/simple_primary_key.json?_shape=objects',
|
'/test_tables/simple_primary_key.json?_shape=objects',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue