simonw / datasette-json-preview

Preview of new JSON default format for Datasette
https://github.com/simonw/datasette/issues/782
1 stars 0 forks source link

Implement array-by-default, ?_extra=next_url/total/pagination concept #2

Open simonw opened 3 years ago

simonw commented 3 years ago

I'm leaning back in the direction of a flat JSON array of objects as the default - this:

/fixtures/roadside_attractions.json

Would return:

[
    {
      "pk": 1,
      "name": "The Mystery Spot",
      "address": "465 Mystery Spot Road, Santa Cruz, CA 95065",
      "latitude": 37.0167,
      "longitude": -122.0024
    },
    {
      "pk": 2,
      "name": "Winchester Mystery House",
      "address": "525 South Winchester Boulevard, San Jose, CA 95128",
      "latitude": 37.3184,
      "longitude": -121.9511
    },
    {
      "pk": 3,
      "name": "Burlingame Museum of PEZ Memorabilia",
      "address": "214 California Drive, Burlingame, CA 94010",
      "latitude": 37.5793,
      "longitude": -122.3442
    },
    {
      "pk": 4,
      "name": "Bigfoot Discovery Museum",
      "address": "5497 Highway 9, Felton, CA 95018",
      "latitude": 37.0414,
      "longitude": -122.0725
    }
]

To get the version that includes pagination information you would use the ?_extra= parameter. For example:

/fixtures/roadside_attractions.json?_extra=total&_extra=next_url

{
  "rows": [
    {
      "pk": 1,
      "name": "The Mystery Spot",
      "address": "465 Mystery Spot Road, Santa Cruz, CA 95065",
      "latitude": 37.0167,
      "longitude": -122.0024
    },
    {
      "pk": 2,
      "name": "Winchester Mystery House",
      "address": "525 South Winchester Boulevard, San Jose, CA 95128",
      "latitude": 37.3184,
      "longitude": -121.9511
    },
    {
      "pk": 3,
      "name": "Burlingame Museum of PEZ Memorabilia",
      "address": "214 California Drive, Burlingame, CA 94010",
      "latitude": 37.5793,
      "longitude": -122.3442
    },
    {
      "pk": 4,
      "name": "Bigfoot Discovery Museum",
      "address": "5497 Highway 9, Felton, CA 95018",
      "latitude": 37.0414,
      "longitude": -122.0725
    }
  ],
  "total": 4,
  "next_url": null
}

ANY usage of the ?_extra= parameter would turn the list into an object with a "rows" key.

Opting in to the total is nice because it's actually expensive to run a count, so only doing a count if the user requests it feels good.

But... having to add ?_extra=total&_extra=next_url for the common case of wanting both the total count and the URL to get the next page of results is a bit verbose. So maybe support aliases, like ?_extra=paginated which is a shortcut for ?_extra=total&_extra=next_url?

Originally posted by @simonw in https://github.com/simonw/datasette/issues/782#issuecomment-782708938

simonw commented 3 years ago

Demos: