Keep search history
Connect Postgres to cache search results for an hour and log every search.
With DATABASE_URL set, the server caches each search result and logs each search in Postgres. Without it the server keeps no state between calls, and search_history returns db_disabled.
Steps#
Set the connection string in
.env.DATABASE_URL=postgresql://procurement:[email protected]:5432/jupiter- Start the server. At start it creates the schema
procurementand the tablessearch_cacheandsearch_logif they are missing. If the database role cannot create a schema, apply the migration once as a role that can, then grant the server's role access. The grants are at the end of the file, commented out.
psql "$DATABASE_URL" -f migrations/001_procurement_cache.sqlCall
healthand readdatabase.{ "configured": true, "schema": "procurement", "enabled": true, "error": null, "cache_ttl_s": 3600 }When
enabledisfalse,errorholds the Postgres error. The server still runs and searches without the cache.Run a search, then call
search_history.{ "source": "log", "query_contains": "hub", "since_hours": 24 }
Settings#
| Setting | Default | Meaning |
|---|---|---|
DATABASE_URL | unset | Postgres connection string. Unset turns the cache and the log off. |
PROCUREMENT_PG_SCHEMA | procurement | Schema name. It must match [a-z_][a-z0-9_]*. |
PROCUREMENT_CACHE_TTL_S | 3600 | Seconds a cached result is served. |
The migration file creates the schema procurement by name. If you set another PROCUREMENT_PG_SCHEMA, let the server create the tables, or edit the file.
What the cache key covers#
Two searches share a cached result when all of these match: query, query_zh, est_weight_kg, include_pickup, the marketplaces, sort, qty, the destination, max_results, ebay_auctions and ebay_seller_scope. deadline_s is left out of the key.
Errors#
| Message | Cause | Fix |
|---|---|---|
search history needs the database: DATABASE_URL is not set | No database is configured. | Set DATABASE_URL and restart. |
search history needs the database: <Postgres error> | The connection or the schema setup failed at start. | Fix the cause named in the message and restart. |