Skip to main content

Storage Adapters

kSync provides multiple storage adapters to persist events to different backends. All storage adapters implement the StorageAdapter interface.

Storage Interface

InMemoryStorage

A simple in-memory storage adapter for development and testing.

Constructor

Options:
  • maxEvents? (number): Maximum number of events to keep in memory (default: unlimited)
  • ttl? (number): Time-to-live for events in milliseconds (default: unlimited)

Methods

save(event)

Saves an event to memory.

load()

Loads all events from memory.

clear()

Clears all events from memory.

getMetadata()

Gets storage metadata.

Example

FileStorage

Persists events to a local file system.

Constructor

Parameters:
  • filePath (string): Path to the storage file
  • options (optional):
    • compression? (boolean): Enable gzip compression (default: false)
    • backup? (boolean): Create backup files (default: true)
    • syncWrites? (boolean): Synchronous writes for durability (default: false)

Methods

Same as StorageAdapter interface.

Example

IndexedDBStorage

Browser-based storage using IndexedDB.

Constructor

Parameters:
  • dbName (string): Name of the IndexedDB database
  • options (optional):
    • version? (number): Database version (default: 1)
    • storeName? (string): Object store name (default: ‘events’)

Methods

Same as StorageAdapter interface.

Example

SQLiteStorage

Server-side SQLite storage for Node.js applications.

Constructor

Parameters:
  • dbPath (string): Path to SQLite database file
  • options (optional):
    • tableName? (string): Table name for events (default: ‘events’)
    • wal? (boolean): Enable WAL mode (default: true)
    • cache? (number): Cache size in pages (default: 2000)

Methods

Same as StorageAdapter interface, plus:

vacuum()

Optimizes the database by reclaiming space.

analyze()

Updates database statistics for query optimization.

Example

RedisStorage

Redis-based storage for distributed applications.

Constructor

Options:
  • host (string): Redis host
  • port (number): Redis port
  • password? (string): Redis password
  • db? (number): Redis database number
  • keyPrefix? (string): Prefix for Redis keys (default: ‘ksync:’)
  • compression? (boolean): Enable compression (default: false)

Methods

Same as StorageAdapter interface, plus:

getStats()

Gets Redis storage statistics.

Example

CloudStorage

Cloud storage adapter for AWS S3, Google Cloud Storage, etc.

Constructor

Parameters:
  • provider (‘s3’ | ‘gcs’ | ‘azure’): Cloud provider
  • options: Provider-specific configuration

S3 Options

Example

Custom Storage Adapter

You can create custom storage adapters by implementing the StorageAdapter interface:

Storage Migration

kSync provides utilities for migrating between storage adapters:

Storage Encryption

For sensitive data, you can add encryption:

Performance Considerations

Batch Operations

For high-throughput applications, use batch operations:

Compression

Enable compression for large events:

Connection Pooling

For database storage, use connection pooling:

Storage Monitoring

Monitor storage performance and health: