deepbase-redis
v3.2.0
Published
⚡ DeepBase Redis (vanilla) - driver
Maintainers
Readme
deepbase-redis
Vanilla Redis driver for DeepBase (no modules required).
Installation
npm install deepbase deepbase-redisPrerequisites
Requires standard Redis (no modules needed):
docker run -d -p 6379:6379 --name redis redis:latestNote: This driver works with vanilla Redis. For RedisJSON support, use deepbase-redis-json instead.
Description
Stores data in Redis using standard string operations with JSON serialization. Perfect for:
- ✅ High-performance caching
- ✅ Real-time applications
- ✅ Session storage
- ✅ Works with any Redis installation
- ✅ No modules required
Usage
import DeepBase from 'deepbase';
import RedisDriver from 'deepbase-redis';
const db = new DeepBase(new RedisDriver({
url: 'redis://localhost:6379',
prefix: 'myapp'
}));
await db.connect();
await db.set('users', 'alice', { name: 'Alice', age: 30 });
const alice = await db.get('users', 'alice');Options
new RedisDriver({
url: 'redis://localhost:6379', // Redis connection URL
prefix: 'db', // Key prefix (or use 'name')
nidAlphabet: 'ABC...', // Alphabet for ID generation
nidLength: 10 // Length of generated IDs
})Data Structure
Data is stored as JSON strings in Redis:
// Keys created
myapp:users -> '{"alice": {...}, "bob": {...}}'
myapp:config -> '{"theme": "dark", "lang": "en"}'Differences from deepbase-redis-json
This vanilla driver:
- ✅ Works with any Redis installation
- ✅ No modules required
- ✅ Simpler deployment
- ❌ No atomic JSON path operations
- ❌ Entire values must be read/written
The RedisJSON driver (deepbase-redis-json):
- ✅ Atomic JSON path operations
- ✅ More efficient for large nested objects
- ❌ Requires Redis Stack or RedisJSON module
Features
JSON Serialization
Uses standard JSON serialization:
await db.set('users', 'alice', { name: 'Alice', age: 30 });
// Stored as: '{"alice": {"name": "Alice", "age": 30}}'Nested Operations
Supports nested path operations:
await db.set('users', 'alice', 'address', { city: 'NYC' });
// Reads full object, modifies, and writes backIncrement/Decrement
Basic increment operations:
await db.inc('stats', 'views', 1);
await db.dec('stats', 'views', 1);Key Scanning
Efficiently scans keys with patterns:
const allUsers = await db.get('users');
// Scans all keys matching prefixConnection String Formats
// Local
url: 'redis://localhost:6379'
// With password
url: 'redis://:password@localhost:6379'
// With database number
url: 'redis://localhost:6379/0'
// Redis Cloud
url: 'redis://username:password@host:port'
// TLS/SSL
url: 'rediss://host:port'Performance
Redis is extremely fast:
- Reads: Sub-millisecond response times
- Writes: Thousands of operations per second
- In-memory: Data stored in RAM with optional persistence
- Simple: No complex JSON path operations
Use Cases
Session Storage
const sessions = new DeepBase(new RedisDriver({ prefix: 'session' }));
await sessions.set(sessionId, 'user', userData);Real-time Stats
const stats = new DeepBase(new RedisDriver({ prefix: 'stats' }));
await stats.inc('page', 'views', 1);Cache Layer
const cache = new DeepBase([
new RedisDriver({ prefix: 'cache' }),
new MongoDriver({ url: '...' })
]);Best Practices
- Use as cache layer - Not as primary storage
- Small to medium objects - Full objects are read/written
- Monitor memory usage - Redis is in-memory
- Enable persistence - RDB or AOF for durability
- Use with persistent drivers - MongoDB or JSON backup
Persistence Options
Redis supports:
- RDB: Periodic snapshots
- AOF: Append-only file for durability
Configure in Redis:
docker run -d -p 6379:6379 \
-v redis-data:/data \
redis:latest \
--appendonly yesError Handling
try {
await db.connect();
} catch (error) {
console.error('Redis connection failed:', error);
// Fallback to other drivers
}When to Use
Use deepbase-redis (this driver) when:
- You have standard Redis
- You want simple deployment
- Your objects are small to medium sized
- You don't need atomic JSON operations
Use deepbase-redis-json when:
- You have Redis Stack
- You need atomic JSON path operations
- You work with large nested objects
- You want optimal performance for partial updates
License
MIT - Copyright (c) Martin Clasen
