integrated data-ingestion

This commit is contained in:
Boki 2025-06-21 19:42:20 -04:00
parent 9673ae70ef
commit 3227388d25
15 changed files with 226 additions and 133 deletions

View file

@ -3,9 +3,10 @@ import { getLogger } from '@stock-bot/logger';
import {
ConnectionFactory,
ServiceContainer,
PoolSizeCalculator
PoolSizeCalculator,
createServiceContainer
} from '@stock-bot/di';
import type { ConnectionFactoryConfig, DynamicPoolConfig } from '@stock-bot/mongodb';
import type { DynamicPoolConfig } from '@stock-bot/mongodb';
const logger = getLogger('database-setup');
@ -40,23 +41,25 @@ export function createConnectionFactory(): ConnectionFactory {
/**
* Sets up the service container with all dependencies
*/
export async function setupServiceContainer(): Promise<ServiceContainer> {
export async function setupServiceContainer(): Promise<{ container: ServiceContainer, factory: ConnectionFactory }> {
logger.info('Setting up service container for data-ingestion');
const connectionFactory = createConnectionFactory();
const dbConfig = getDatabaseConfig();
const queueConfig = getQueueConfig();
// Create base container
const container = new ServiceContainer('data-ingestion');
// Create enhanced service container with connection factory
const container = createServiceContainer('data-ingestion', connectionFactory, {
database: dbConfig
});
// Register MongoDB with dynamic pool sizing
// Override the default database connections with specific configurations
// MongoDB with dynamic pool sizing for batch operations
container.register({
name: 'mongodb',
factory: async () => {
const poolSize = PoolSizeCalculator.calculate('data-ingestion', 'batch-import');
const pool = await connectionFactory.createMongoDB({
name: 'default',
name: 'data-ingestion',
config: {
uri: dbConfig.mongodb.uri,
database: dbConfig.mongodb.database,
@ -77,18 +80,15 @@ export async function setupServiceContainer(): Promise<ServiceContainer> {
return pool.client;
},
singleton: true,
dispose: async (client) => {
await client.disconnect();
}
});
// Register PostgreSQL
// PostgreSQL with optimized settings for data ingestion
container.register({
name: 'postgres',
factory: async () => {
const poolSize = PoolSizeCalculator.calculate('data-ingestion');
const pool = await connectionFactory.createPostgreSQL({
name: 'default',
name: 'data-ingestion',
config: {
host: dbConfig.postgresql.host,
port: dbConfig.postgresql.port,
@ -107,17 +107,14 @@ export async function setupServiceContainer(): Promise<ServiceContainer> {
return pool.client;
},
singleton: true,
dispose: async (client) => {
await client.disconnect();
}
});
// Register Cache
// Cache with data-ingestion specific configuration
container.register({
name: 'cache',
factory: async () => {
const pool = await connectionFactory.createCache({
name: 'default',
name: 'data-ingestion',
config: {
host: dbConfig.dragonfly.host,
port: dbConfig.dragonfly.port,
@ -129,12 +126,12 @@ export async function setupServiceContainer(): Promise<ServiceContainer> {
singleton: true,
});
// Register QueueManager
// Queue with data-ingestion specific configuration
container.register({
name: 'queue',
factory: async () => {
const pool = await connectionFactory.createQueue({
name: 'default',
name: 'data-ingestion',
config: {
host: dbConfig.dragonfly.host,
port: dbConfig.dragonfly.port,
@ -144,19 +141,6 @@ export async function setupServiceContainer(): Promise<ServiceContainer> {
return pool.client;
},
singleton: true,
dispose: async (queueManager) => {
await queueManager.shutdown();
}
});
// Register the connection factory itself for pool management
container.register({
name: 'connectionFactory',
factory: () => connectionFactory,
singleton: true,
dispose: async (factory) => {
await factory.disposeAll();
}
});
logger.info('Service container setup complete');
@ -166,7 +150,7 @@ export async function setupServiceContainer(): Promise<ServiceContainer> {
await enableDynamicPoolSizing(container);
}
return container;
return { container, factory: connectionFactory };
}
/**