Registry / database / safe-redis-schema

safe-redis-schema

JSON →
library1.1.0jsnpmunverified

Type-safe and validated data schemas for Redis with TypeScript. Version 1.1.0 provides runtime and compile-time validation for Redis data structures like hashes, counters, and objects. Ensures key uniqueness across schema definitions to prevent accidental reuse. Different from generic Redis ORMs by enforcing schema validation at both read and write, with built-in TypeScript type inference. Low release cadence; stable for production use cases requiring strict data integrity.

npm install safe-redis-schema
INSTALL
IMPORT
SIG · SAFE-REDIS-SCHEMA
S
safe-redis-schema
databasejavascriptv1.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.

RedisStore
import { RedisStore } from 'safe-redis-schema';
const RedisStore = require('safe-redis-schema').RedisStore;
ESM-only; CommonJS require works but types may not infer correctly.
defineObj
import { defineObj } from 'safe-redis-schema';
import defineObj from 'safe-redis-schema';
Named export, not default.
defineHashOf
import { defineHashOf } from 'safe-redis-schema';
import { defineHashOf } from 'safe-redis-schema/defineHashOf';
Exported from package root, no subpath.

Defines and uses a Redis schema with validated hashes and counters, demonstrating key reuse prevention and type safety.

import { RedisStore, defineHashOfCounters, defineHashOf } from 'safe-redis-schema'; import * as Safe from 'safe-portals'; const secureRandomSecret = process.env.REDIS_URL ?? ''; const store = new RedisStore(secureRandomSecret); const ns = store.namespacedBy('myApp:'); const pageHits = defineHashOfCounters(ns, 'pageHits'); const users = defineHashOf(ns, 'users', Safe.obj({ name: Safe.str, age: Safe.int })); async function main() { await pageHits.incrby('/home', 1); const count = await pageHits.get('/home'); console.log('Page hits:', count); await users.hset('user:1', { name: 'Alice', age: 30 }); const user = await users.hget('user:1'); console.log('User:', user); } main().catch(console.error);
Debug
Known issues
breakingKey reuse across schema types throws error
fix
Ensure each Redis key is used only once across schema definitions (e.g., do not define both defineObj and defineHashOfCounters for same key).
affects: >=1.0.0
gotchaRedis URL must be provided via environment variable or constructor
fix
Set REDIS_URL environment variable or pass URL to RedisStore constructor.
affects: >=1.0.0
gotchaTypeScript strict mode required for full type safety
fix
Enable strict mode in tsconfig.json to benefit from type inference.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Key 'pageHits' already used for a different schema type
Attempted to define a schema with a key that is already used by another schema definition.
fix
Use unique keys for each schema definition or namespace them appropriately.
TypeError: store.namespacedBy is not a function
Called namespacedBy on something that is not a RedisStore instance, possibly due to incorrect import.
fix
Ensure RedisStore is correctly imported: import { RedisStore } from 'safe-redis-schema';
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies
safe-portalsrequiredRequired for runtime type validation (Safe.str, Safe.int, Safe.obj).
Agent activity
12 hits · last 30 days
node
10
Meta
1
Resources
safe-redis-schema — npm install safe-redis-schema · libregistry