zrcadlo / undercurrent_api

The Backend of your Dreams: API for dream recording app
0 stars 0 forks source link

Improve location capabilities #17

Closed lfborjas closed 4 years ago

lfborjas commented 4 years ago

For ease of serialization/extensibility, maybe have an address JSONB field with some uniform/minimal subset of fields to store, something like: {city,state,country}, making queries like undercurrent_dev=# select '{"city": null, "state": "NY"}' @> '{"state": "NY"}'::jsonb; possible. Would allow us to transmit a single Address field in the API, with some validation/transformation at the edge but nothing too crazy; unsure what to do about querying -- take ?address=someJsonString, or components?

This is a bit more flexible than storing in separate columns, and easier to work with on both the Haskell and JS sides (one datum/type,) but obviously quite a bit more wasteful as far as the database is concerned. As far as performance querying, the GIN index with jsonb_path_ops should do nicely, just like for emotions.

Notes for the App

The app could use the Place Autocomplete API by Google, but I personally like the apparently simpler alternative presented by Algolia Places API

I like the Algolia API because it provides simple JSON (only when using their places.js) like:

{
  "query": "manhattan",
  "suggestion": {
    "name": "Manhattan",
    "administrative": "New York",
    "county": "New York County",
    "country": "United States of America",
    "countryCode": "us",
    "type": "city",
    "latlng": {
      "lat": 40.7834,
      "lng": -73.9663
    },
    "highlight": {
      "name": "<em>Manhattan</em>",
      "administrative": "New York",
      "country": "United States of America",
      "county": "<em>Manhattan</em> (New York County)"
    },
    "value": "Manhattan, New York, United States of America"
  }
}

vs. the big ol' object that google returns. Also, algolia is cheaper -- $0.4 per 1000 requests than google autocomplete -- $17 per 1000 requests.

NOTE: Algolia has both an API (with JS clients and regular REST endpoints,) and the places.js abstraction. We should settle on one of those, as the responses are different -- if we can make the places.js work, that'd be fantastic.

Notes for the backend

Maybe as a first approach, we just do the following

[1] https://postgis.net/workshops/postgis-intro/geography.html [2] https://postgis.net/docs/ST_DWithin.html