@sailboat-computer/data-storage
Version:
Shared data storage library for sailboat computer v3
145 lines (119 loc) • 4.96 kB
text/typescript
/**
* Configuration Service Integration Example
*
* This example demonstrates how to use the StorageConfigManager with the UnifiedStorageManager.
* It shows how to:
* 1. Initialize the configuration manager
* 2. Get the storage configuration
* 3. Create a unified storage manager with the configuration
* 4. Subscribe to configuration changes
* 5. Update the configuration
*/
import {
storageConfigManager,
createUnifiedStorageManager,
eventBus,
UnifiedStorageManager,
StorageConfig
} from '../src';
/**
* Main function
*/
async function main() {
try {
console.log('Initializing configuration manager...');
// Initialize the configuration manager
await storageConfigManager.initialize();
// Set the event bus
storageConfigManager.setEventBus(eventBus);
console.log('Configuration manager initialized');
// Get the storage configuration
const storageConfig = storageConfigManager.getStorageConfig();
console.log('Storage configuration:', JSON.stringify(storageConfig, null, 2));
// Get the unified storage manager options
const options = storageConfigManager.getUnifiedStorageManagerOptions();
console.log('Unified storage manager options:', JSON.stringify(options, null, 2));
// Create a unified storage manager with the configuration
console.log('Creating unified storage manager...');
const storageManager = createUnifiedStorageManager({
...options,
providers: storageConfig.providers
});
// Initialize the storage manager
await storageManager.initialize();
console.log('Unified storage manager initialized');
// Subscribe to configuration change events
const subscriptionId = eventBus.subscribe('system.config_changed.v1', async (event: any) => {
console.log('Configuration changed:', event);
// In a real application, you would update the storage manager with the new configuration
// This might involve closing the current storage manager and creating a new one
console.log('Updating storage manager with new configuration...');
// Example of how you might update the storage manager
await handleConfigChange(event, storageManager);
});
console.log(`Subscribed to configuration changes with ID: ${subscriptionId}`);
// Example: Update the configuration
console.log('Updating configuration...');
// Get the current configuration
const config = storageConfigManager.getStorageConfig();
// Update the Redis host in the configuration
if (config.providers.hot && config.providers.hot.type === 'redis') {
const redisConfig = config.providers.hot.config as any;
redisConfig.host = 'new-redis-host';
// Save the configuration
await storageConfigManager.saveConfig(config, 'Updated Redis host');
console.log('Configuration updated and saved');
// Apply the changes
await storageConfigManager.applyChanges();
console.log('Configuration changes applied');
}
// In a real application, you would keep the storage manager running
// For this example, we'll just wait a bit and then clean up
console.log('Waiting for 2 seconds...');
await new Promise(resolve => setTimeout(resolve, 2000));
// Clean up
console.log('Cleaning up...');
eventBus.unsubscribe(subscriptionId);
await storageManager.close();
storageConfigManager.shutdown();
console.log('Clean up complete');
console.log('Example completed successfully');
} catch (error) {
console.error('Error in example:', error);
}
}
/**
* Handle configuration change event
*
* @param event - Configuration change event
* @param storageManager - Storage manager to update
*/
async function handleConfigChange(event: any, storageManager: UnifiedStorageManager): Promise<void> {
try {
// In a real application, you would:
// 1. Close the current storage manager
// 2. Create a new one with the updated configuration
// 3. Replace the old storage manager with the new one
// For this example, we'll just log the event
console.log('Configuration change event received:');
console.log('- Service:', event.service);
console.log('- Timestamp:', event.timestamp);
console.log('- Config:', JSON.stringify(event.config, null, 2));
// Example of how you might update the storage manager
// await storageManager.close();
//
// const newStorageManager = createUnifiedStorageManager({
// ...event.config,
// providers: event.config.providers
// });
//
// await newStorageManager.initialize();
//
// // Replace the old storage manager with the new one
// storageManager = newStorageManager;
} catch (error) {
console.error('Error handling configuration change:', error);
}
}
// Run the example
main().catch(console.error);