Postgres and Redis
@exo/kit/infra exports createDb (postgres.js) and createRedis (ioredis). Peers: postgres and ioredis. Never import it from browser code.
import { createDb, createRedis } from '@exo/kit/infra';
export const { sql: db, query: dbQuery, tryQuery: dbTry, jsonb } = createDb({
url: env.databaseUrl,
globalKey: '__db',
reportError: telemetry.reportError,
});
export const redis = createRedis({
url: env.redisUrl,
globalKey: '__redis',
reportError: telemetry.reportError,
});
Destructure#
The functions are bound to the instance, so you can destructure them and call them from anywhere.
Not configured#
With url: null the accessors still work, and every call takes the unavailable branch:
- a query answers
null, - a cache read misses,
- a lock is granted.
Nothing is reported, because having no database is configuration, not a fault.
tryQuery#
query returns null for three different cases: not configured, threw, and returned nothing. Use tryQuery where those must be told apart, for example in a ledger that must run exactly once and must not read a swallowed error as "already handled". It returns a DbOutcome.
globalKey keeps one connection pool across hot reloads in development.