This example demonstrates NpgsqlRest's reverse proxy feature, which allows you to forward HTTP requests to upstream services and optionally process the responses in PostgreSQL.
When a proxy endpoint function has no proxy response parameters, NpgsqlRest:
- Forwards the request to the upstream service
- Returns the upstream response directly to the client
- Does NOT open a database connection (saves connection pool resources)
-- Passthrough: no DB connection opened
create function ai_health()
returns void
language sql as $$select$$;
comment on function ai_health is 'proxy http://localhost:3001/health';When a proxy endpoint function has proxy response parameters (_proxy_body, _proxy_status_code, etc.), NpgsqlRest:
- Forwards the request to the upstream service
- Passes the upstream response to your PostgreSQL function
- Your function can transform, enrich, cache, or validate the response
-- Transform: response is passed to PostgreSQL for processing
create function ai_summarize(
_text text,
_proxy_body text default null,
_proxy_success boolean default null
)
returns json
language plpgsql as $$
begin
-- Cache the result, enrich with metadata, etc.
return _proxy_body::json;
end;
$$;
comment on function ai_summarize is 'proxy POST http://localhost:3001/summarize';┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Client │────>│ NpgsqlRest │────>│ AI Service │
│ (Browser) │<────│ (Proxy) │<────│ (Bun) │
└─────────────┘ └──────┬──────┘ └─────────────┘
│
┌─────v─────┐
│ PostgreSQL │
│ (Cache) │
└───────────┘
cd examples/10_proxy_ai_service
bun run upstreambun run db:upbun run devNavigate to http://127.0.0.1:8080
| Endpoint | Mode | Description |
|---|---|---|
GET /ai/health |
Passthrough | Health check - no DB connection |
POST /ai/summarize |
Transform | Summarize text with caching |
POST /ai/sentiment |
Transform | Sentiment analysis with caching |
POST /ai/analyze |
Transform | Full analysis (summary + sentiment + keywords) |
GET /cache/stats |
Regular | View cache statistics |
DELETE /cache/clear |
Regular | Clear the cache |
The proxy feature is enabled in config.json:
{
"NpgsqlRest": {
"ProxyOptions": {
"Enabled": true,
"Host": "http://localhost:3001",
"DefaultTimeout": "00:00:30"
}
}
}- API Gateway: Route requests to different microservices
- Caching Layer: Cache expensive external API calls in PostgreSQL
- Data Enrichment: Combine external data with local database data
- Rate Limiting: Track and limit API calls per user
- Response Transformation: Modify external API responses before returning
- Audit Logging: Log all external API interactions
- Fallback Handling: Return cached data when upstream is unavailable
| File | Description |
|---|---|
upstream/server.ts |
Bun server simulating an AI text analysis service |
sql_10/*.sql |
PostgreSQL schema and proxy endpoint functions |
config.json |
NpgsqlRest configuration with proxy enabled |
src/app.ts |
Frontend TypeScript application |
public/index.html |
Demo web interface |