Registry / database / postgres-redis

postgres-redis

JSON →
library0.1.0jsnpmunverified

Caching layer for node-postgres (pg) that automatically caches SELECT query results in Redis. Version 0.1.0, experimental. Provides multiple hash types (farmhash32, farmhash64, blake2b512, full) for cache keys. Falls back to Postgres on Redis failure. Requires pg/pg-pool and redis/ioredis as peer dependencies. Distinct from other caching libraries by offering configurable hash strategies and per-query custom hashing.

npm install postgres-redis
INSTALL
IMPORT
SIG · POSTGRES-REDIS
P
postgres-redis
databasejavascriptv0.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

PostgresRedis
import { PostgresRedis } from 'postgres-redis'
import PostgresRedis from 'postgres-redis'
Named export, not default.
PostgresRedisAsync
import { PostgresRedisAsync } from 'postgres-redis'
const { PostgresRedisAsync } = require('postgres-redis')
Async variant for async-redis. CJS require works too.
HashTypes
import { HashTypes } from 'postgres-redis'
Enum-like object with farmhash32, farmhash64, blake2b512, full.

Shows how to create PostgresRedis instance with Pool, Redis client, and perform a cached query.

const { PostgresRedis, HashTypes } = require('postgres-redis'); const { Pool } = require('pg'); const redis = require('redis'); const pool = new Pool({ connectionString: process.env.DATABASE_URL ?? '' }); const redisClient = redis.createClient({ url: process.env.REDIS_URL ?? '' }); const postgresRedis = new PostgresRedis( pool, redisClient, { expiry: 3600, keyPrefix: 'cache:', hashType: HashTypes.farmhash64 } ); async function getData() { const res = await new Promise((resolve, reject) => { postgresRedis.query('SELECT * FROM users WHERE id = $1', [1], (err, result) => { if (err) reject(err); else resolve(result); }); }); console.log(res.rows); } getData().catch(console.error);
Debug
Known issues
gotchaStale cache: Data updated in Postgres will not invalidate Redis cache automatically. Use only for static or append-only data.
fix
Manually delete/update cache keys or set short TTL.
affects: all
gotchaHash collision risk: farmhash32 and farmhash64 are non-cryptographic and can collide with many queries. Use blake2b512 or full for safety.
fix
Use HashTypes.blake2b512 or HashTypes.full for large query sets.
affects: <=0.1.0
gotchaAsync variant requires async-redis: PostgresRedisAsync only works with async-redis, not vanilla redis or ioredis.
fix
Use async-redis package with PostgresRedisAsync, or use callbacks with PostgresRedis.
affects: <=0.1.0
gotchaNo promise support in PostgresRedis: Only callback-based query method. Use PostgresRedisAsync for promises.
fix
Use PostgresRedisAsync or wrap in util.promisify.
affects: <=0.1.0
gotchaRedis failure fallback: If Redis is down, query silently falls back to Postgres. Can mask caching issues.
fix
Monitor Redis health separately; implement logging on fallback.
affects: all
Errors
Common errors & fixes
TypeError: postgresRedis.query is not a function
Using PostgresRedisAsync where method is queryAsync, not query.
fix
Use postgresRedisAsync.queryAsync instead of .query.
Error: Redis connection error: getaddrinfo ENOTFOUND
Invalid Redis host/port in connection string.
fix
Ensure REDIS_URL or createClient options are correct.
TypeError: Cannot read property 'rows' of undefined
Callback not passed; async usage without awaiting.
fix
Pass callback function to query or use PostgresRedisAsync.
Upgrade
Version history
0.1.0latest on npm
Audit
Dependencies
pgrequiredpeer dependency for Postgres queries
redisrequiredpeer dependency for caching, or ioredis
Agent activity
7 hits · last 30 days
node
6
Resources
postgres-redis — npm install postgres-redis · libregistry