update mongo for multi db support
This commit is contained in:
parent
4942574b94
commit
cbef304045
7 changed files with 927 additions and 13 deletions
230
examples/mongodb-multi-database-usage.ts
Normal file
230
examples/mongodb-multi-database-usage.ts
Normal file
|
|
@ -0,0 +1,230 @@
|
|||
/**
|
||||
* Practical Usage Examples for Multi-Database MongoDB Client
|
||||
*
|
||||
* This file demonstrates real-world usage patterns for the enhanced MongoDB client
|
||||
* with multiple database support.
|
||||
*/
|
||||
|
||||
import { getCurrentDatabase, MongoDBClient, setDefaultDatabase } from '@stock-bot/mongodb-client';
|
||||
|
||||
// Example 1: Using different databases for different data types
|
||||
export class DataServiceExample {
|
||||
private mongoClient = MongoDBClient.getInstance();
|
||||
|
||||
async initialize() {
|
||||
await this.mongoClient.connect();
|
||||
|
||||
// Set stock as default database for most operations
|
||||
setDefaultDatabase('stock');
|
||||
console.log(`Default database: ${getCurrentDatabase()}`);
|
||||
}
|
||||
|
||||
// Stock market data goes to 'stock' database (default)
|
||||
async saveStockData(symbols: any[], exchanges: any[]) {
|
||||
// These use the default 'stock' database
|
||||
await this.mongoClient.batchUpsert('symbols', symbols, 'symbol');
|
||||
await this.mongoClient.batchUpsert('exchanges', exchanges, 'exchange_id');
|
||||
|
||||
// Or use convenience method (explicitly targets 'stock' database)
|
||||
await this.mongoClient.batchUpsertStock('prices', symbols, 'symbol');
|
||||
}
|
||||
|
||||
// Analytics and metrics go to 'analytics' database
|
||||
async saveAnalyticsData(performanceData: any[], metrics: any[]) {
|
||||
// Override database for specific operations
|
||||
await this.mongoClient.batchUpsert('performance', performanceData, 'date', {
|
||||
database: 'analytics',
|
||||
});
|
||||
|
||||
// Or use convenience method
|
||||
await this.mongoClient.batchUpsertAnalytics('metrics', metrics, 'metric_name');
|
||||
}
|
||||
|
||||
// Trading documents and logs go to 'trading_documents' database
|
||||
async saveTradingData(orders: any[], transactions: any[]) {
|
||||
// Use convenience method for trading data
|
||||
await this.mongoClient.batchUpsertTrading('orders', orders, 'order_id');
|
||||
await this.mongoClient.batchUpsertTrading('transactions', transactions, 'transaction_id');
|
||||
}
|
||||
|
||||
// Example of switching default database dynamically
|
||||
async switchToAnalyticsMode() {
|
||||
console.log(`Current default: ${getCurrentDatabase()}`);
|
||||
|
||||
// Switch to analytics database for a series of operations
|
||||
setDefaultDatabase('analytics');
|
||||
console.log(`New default: ${getCurrentDatabase()}`);
|
||||
|
||||
// Now all operations without explicit database parameter go to 'analytics'
|
||||
await this.mongoClient.batchUpsert('daily_reports', [], 'date');
|
||||
await this.mongoClient.batchUpsert('portfolio_performance', [], 'portfolio_id');
|
||||
|
||||
// Switch back to stock database
|
||||
setDefaultDatabase('stock');
|
||||
}
|
||||
|
||||
// Example of working with multiple databases simultaneously
|
||||
async crossDatabaseAnalysis() {
|
||||
// Get direct access to different databases
|
||||
const stockDb = this.mongoClient.getDatabase('stock');
|
||||
const analyticsDb = this.mongoClient.getDatabase('analytics');
|
||||
const tradingDb = this.mongoClient.getDatabase('trading_documents');
|
||||
|
||||
// Perform operations on multiple databases
|
||||
const stockSymbols = await stockDb.collection('symbols').find({}).toArray();
|
||||
const performance = await analyticsDb.collection('performance').find({}).toArray();
|
||||
const orders = await tradingDb.collection('orders').find({}).toArray();
|
||||
|
||||
console.log('Cross-database analysis:', {
|
||||
symbolsCount: stockSymbols.length,
|
||||
performanceRecords: performance.length,
|
||||
ordersCount: orders.length,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Example 2: Data Migration Between Databases
|
||||
export class DataMigrationExample {
|
||||
private mongoClient = MongoDBClient.getInstance();
|
||||
|
||||
async migrateHistoricalData() {
|
||||
await this.mongoClient.connect();
|
||||
|
||||
// Get collections from different databases
|
||||
const stockCollection = this.mongoClient.getCollection('historical_prices', 'stock');
|
||||
const analyticsCollection = this.mongoClient.getCollection('price_analysis', 'analytics');
|
||||
|
||||
// Read from stock database
|
||||
const historicalPrices = await stockCollection
|
||||
.find({
|
||||
date: { $gte: new Date('2024-01-01') },
|
||||
})
|
||||
.toArray();
|
||||
|
||||
console.log(`Found ${historicalPrices.length} historical price records`);
|
||||
|
||||
// Transform and save to analytics database
|
||||
const analysisData = historicalPrices.map(price => ({
|
||||
symbol: price.symbol,
|
||||
date: price.date,
|
||||
price_change: price.close - price.open,
|
||||
volume_normalized: price.volume / 1000000,
|
||||
created_at: new Date(),
|
||||
updated_at: new Date(),
|
||||
}));
|
||||
|
||||
// Save to analytics database
|
||||
await this.mongoClient.batchUpsert('price_analysis', analysisData, ['symbol', 'date'], {
|
||||
database: 'analytics',
|
||||
});
|
||||
|
||||
console.log(`Migrated ${analysisData.length} records to analytics database`);
|
||||
}
|
||||
}
|
||||
|
||||
// Example 3: Service-Specific Database Usage
|
||||
export class TradingServiceExample {
|
||||
private mongoClient = MongoDBClient.getInstance();
|
||||
|
||||
async initialize() {
|
||||
await this.mongoClient.connect();
|
||||
// Trading service primarily works with trading_documents database
|
||||
setDefaultDatabase('trading_documents');
|
||||
}
|
||||
|
||||
async processTradeOrders(orders: any[]) {
|
||||
// Default database is 'trading_documents', so no need to specify
|
||||
await this.mongoClient.batchUpsert('orders', orders, 'order_id');
|
||||
|
||||
// Log to analytics database for monitoring
|
||||
const orderMetrics = orders.map(order => ({
|
||||
metric_name: `order_${order.type}`,
|
||||
value: order.quantity,
|
||||
timestamp: new Date(),
|
||||
}));
|
||||
|
||||
await this.mongoClient.batchUpsert(
|
||||
'trading_metrics',
|
||||
orderMetrics,
|
||||
['metric_name', 'timestamp'],
|
||||
{ database: 'analytics' }
|
||||
);
|
||||
}
|
||||
|
||||
async getOrderHistory(symbolFilter?: string) {
|
||||
// Get collection from default database (trading_documents)
|
||||
const ordersCollection = this.mongoClient.getCollection('orders');
|
||||
|
||||
const filter = symbolFilter ? { symbol: symbolFilter } : {};
|
||||
return await ordersCollection.find(filter).sort({ created_at: -1 }).limit(100).toArray();
|
||||
}
|
||||
}
|
||||
|
||||
// Example 4: Configuration-Based Database Routing
|
||||
export class ConfigurableDatabaseRouter {
|
||||
private mongoClient = MongoDBClient.getInstance();
|
||||
|
||||
// Configuration mapping data types to databases
|
||||
private databaseConfig = {
|
||||
market_data: 'stock',
|
||||
user_data: 'trading_documents',
|
||||
analytics: 'analytics',
|
||||
logs: 'trading_documents',
|
||||
cache: 'analytics',
|
||||
};
|
||||
|
||||
async saveData(
|
||||
dataType: keyof typeof this.databaseConfig,
|
||||
collection: string,
|
||||
data: any[],
|
||||
uniqueKeys: string[]
|
||||
) {
|
||||
const targetDatabase = this.databaseConfig[dataType];
|
||||
|
||||
console.log(`Saving ${data.length} records to ${targetDatabase}.${collection}`);
|
||||
|
||||
return await this.mongoClient.batchUpsert(collection, data, uniqueKeys, {
|
||||
database: targetDatabase,
|
||||
});
|
||||
}
|
||||
|
||||
async saveMarketData(data: any[]) {
|
||||
return this.saveData('market_data', 'realtime_prices', data, 'symbol');
|
||||
}
|
||||
|
||||
async saveUserActivity(data: any[]) {
|
||||
return this.saveData('user_data', 'user_actions', data, 'user_id');
|
||||
}
|
||||
|
||||
async saveAnalytics(data: any[]) {
|
||||
return this.saveData('analytics', 'performance_metrics', data, 'metric_id');
|
||||
}
|
||||
}
|
||||
|
||||
// Usage example for your data service
|
||||
export async function exampleUsage() {
|
||||
const dataService = new DataServiceExample();
|
||||
|
||||
await dataService.initialize();
|
||||
|
||||
// Save different types of data to appropriate databases
|
||||
await dataService.saveStockData(
|
||||
[{ symbol: 'AAPL', price: 150.25, volume: 1000000 }],
|
||||
[{ exchange_id: 'NYSE', name: 'New York Stock Exchange' }]
|
||||
);
|
||||
|
||||
await dataService.saveAnalyticsData(
|
||||
[{ date: new Date(), portfolio_return: 0.15 }],
|
||||
[{ metric_name: 'sharpe_ratio', value: 1.25 }]
|
||||
);
|
||||
|
||||
await dataService.saveTradingData(
|
||||
[{ order_id: 'ORD001', symbol: 'AAPL', quantity: 100 }],
|
||||
[{ transaction_id: 'TXN001', amount: 15025 }]
|
||||
);
|
||||
|
||||
// Perform cross-database analysis
|
||||
await dataService.crossDatabaseAnalysis();
|
||||
|
||||
console.log('Multi-database operations completed successfully!');
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue