Documentation Index
Fetch the complete documentation index at: https://docs.xhipai.com/llms.txt
Use this file to discover all available pages before exploring further.
PostgreSQL Storage
PostgresStorage stores key-value data in PostgreSQL using akv_store table with JSONB values. It requires the pg package and is ideal for production deployments, especially when running multiple server instances.
Installation
Setup
Constructor
PostgreSQL connection string. Format:
postgresql://user:password@host:port/databaseFull Example
Schema
PostgresStorage creates a tablekv_store:
| Column | Type | Description |
|---|---|---|
namespace | TEXT | Namespace |
key | TEXT | Key within namespace |
value | JSONB | Stored value (no manual JSON stringify) |
updated_at | TIMESTAMPTZ | Last update timestamp |
(namespace, key).
Multi-Instance Deployments
PostgresStorage is designed for horizontal scaling. Multiple Node.js processes can safely share the same PostgreSQL database.Connection Pooling
Thepg driver manages a connection pool internally. For high-concurrency deployments, configure the pool size:
Load Balancer Setup
kv_store table. The (namespace, key) primary key ensures consistency, and JSONB values allow atomic updates.
Best Practices
- Use a managed PostgreSQL service (RDS, Cloud SQL, Supabase) for production
- Enable SSL for database connections:
?sslmode=requirein the connection string - Monitor connection pool usage to avoid exhaustion under load
- Run
VACUUM ANALYZE kv_storeperiodically for optimal query performance
Environment Variables
API Reference
| Method | Description |
|---|---|
initialize() | Creates kv_store table. Must be called before get/set. |
get<T>(namespace, key) | Get value. Returns null if not found. |
set<T>(namespace, key, value) | Store value. Uses JSONB for native JSON support. |
delete(namespace, key) | Remove key. |
list<T>(namespace, prefix?) | List keys. Prefix uses SQL LIKE. |
close() | Closes the connection pool. |