Express-postgres-mapbox example
Starting
First, create and populate the db:
$ createdb express-postgres-mapbox-example
$ npm run db:init
Then create a secrets.json
file with:
{
"DATABASE_USERNAME": "your database username",
"DATABASE_PASSWORD": "your database password",
"MAPBOX_ACCESS_TOKEN": "your mapbox access token"
}
Finally:
$ npm start
And visit http://localhost:3000!
Storing GeoJSON inside the database
Since we need it just for storage and not for querying, the jsonb
postgres type is enough:
function updateItineraryById({ id, geometry }) {
return db
.query(
"UPDATE itineraries SET geometry = ($2)::jsonb WHERE id = $1 RETURNING *",
[id, JSON.stringify(geometry)]
)
.then((result) => result.rows[0]);
}