stock-bot/libs/core/di/src/container
Boki 4cd6038063 refactor: pre-M0 cleanup, core library hardening, and isolated dev/QA Docker stack
- Remove ~44k LOC of dead code, legacy v1 TS simulator/strategy stack, event-bus, analytics FastAPI prototype, legacy calculation archives, and unused deps (data-ingestion provider API handlers untouched)
- Extract @stock-bot/di/http-security and isolate @stock-bot/browser to data-ingestion via ServiceContainerBuilder.withRegistrations()
- Harden @stock-bot/mongodb read-only proxies, flatten @stock-bot/postgres with unified master+research migrations, and fix @stock-bot/config EnvLoader, ServiceApplication worker lifecycle, NamespacedCache.clear(), and BatchProcessor error propagation
- Flatten workspace root (remove apps/stock/package.json) and collapse orchestrator re-export shims
- Pin all Docker image versions, add shm_size/nofile/RAM caps and docker-compose.quant.yml, fix 05-add-visible-column.sql view recreation, and decouple dev/QA Compose project names for side-by-side execution
- Update PLATFORM-PLAN, DATA-CONTRACTS (bars_1m PARTITION BY MONTH, Rust bulk ETL, DuckDB limits), MILESTONES, and servers docs
2026-11-27 00:11:50 -05:00
..
builder.ts refactor: pre-M0 cleanup, core library hardening, and isolated dev/QA Docker stack 2026-11-27 00:11:50 -05:00
README.md refactored di into more composable parts 2025-06-22 21:47:39 -04:00
types.ts refactor: pre-M0 cleanup, core library hardening, and isolated dev/QA Docker stack 2026-11-27 00:11:50 -05:00

DI Container - Modular Structure

Overview

The DI container has been refactored into a modular structure for better organization and maintainability.

Directory Structure

├── container/          # Core container logic
│   ├── builder.ts      # Fluent API for building containers
│   ├── factory.ts      # Factory functions (legacy compatibility)
│   └── types.ts        # Type definitions
├── registrations/      # Service registration modules
│   ├── core.ts         # Core services (config, logger)
│   ├── cache.ts        # Cache services
│   ├── database.ts     # Database clients
│   └── service.ts      # Application services
├── config/             # Configuration management
│   └── schemas/        # Zod schemas for validation
├── factories/          # Service factories
│   └── cache.factory.ts # Cache factory utilities
└── utils/              # Utilities
    └── lifecycle.ts    # Service lifecycle management

Usage Examples

import { ServiceContainerBuilder } from '@stock-bot/di';

// Create container with fluent API
const container = await new ServiceContainerBuilder()
  .withConfig({
    redis: { host: 'localhost', port: 6379 },
    mongodb: { uri: 'mongodb://localhost', database: 'mydb' },
    postgres: { host: 'localhost', database: 'mydb', user: 'user', password: 'pass' }
  })
  .enableService('enableQueue', false) // Disable queue service
  .enableService('enableBrowser', false) // Disable browser service
  .build();

// Services are automatically initialized
const cache = container.cradle.cache;
const mongoClient = container.cradle.mongoClient;

Creating Namespaced Caches

import { CacheFactory } from '@stock-bot/di';

// Create a cache for a specific service
const serviceCache = CacheFactory.createCacheForService(container, 'myservice');

// Create a cache for a handler
const handlerCache = CacheFactory.createCacheForHandler(container, 'myhandler');

// Create a cache with custom prefix
const customCache = CacheFactory.createCacheWithPrefix(container, 'custom');

Manual Service Lifecycle

import { ServiceContainerBuilder, ServiceLifecycleManager } from '@stock-bot/di';

// Create container without auto-initialization
const container = await new ServiceContainerBuilder()
  .withConfig(config)
  .skipInitialization()
  .build();

// Manually initialize services
const lifecycle = new ServiceLifecycleManager();
await lifecycle.initializeServices(container);

// ... use services ...

// Manually shutdown services
await lifecycle.shutdownServices(container);

Legacy API (Backward Compatible)

import { createServiceContainerFromConfig } from '@stock-bot/di';

// Old way still works
const container = createServiceContainerFromConfig(appConfig, {
  enableQueue: true,
  enableCache: true
});

// Manual initialization required with legacy API
await initializeServices(container);

Migration Guide

  1. Replace direct container creation with ServiceContainerBuilder
  2. Use CacheFactory instead of manually creating NamespacedCache
  3. Let the builder handle service initialization automatically
  4. Use typed configuration schemas for better validation