fixed up di
This commit is contained in:
parent
d63025de90
commit
8550b1de57
5 changed files with 19 additions and 56 deletions
|
|
@ -7,7 +7,7 @@ import { Browser } from '@stock-bot/browser';
|
|||
import { createCache, type CacheProvider } from '@stock-bot/cache';
|
||||
import type { IServiceContainer } from '@stock-bot/handlers';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import { createMongoDBClient } from '@stock-bot/mongodb';
|
||||
import { MongoDBClient } from '@stock-bot/mongodb';
|
||||
import { createPostgreSQLClient } from '@stock-bot/postgres';
|
||||
import { ProxyManager } from '@stock-bot/proxy';
|
||||
import { createQuestDBClient } from '@stock-bot/questdb';
|
||||
|
|
@ -90,22 +90,9 @@ export function createServiceContainer(config: AppConfig): AwilixContainer {
|
|||
);
|
||||
// Note: initialization happens in initializeServices function
|
||||
return manager;
|
||||
}).singleton(),
|
||||
|
||||
|
||||
// MongoDB client with injected logger
|
||||
}).singleton(), // MongoDB client with injected dependencies
|
||||
mongoClient: asFunction(({ mongoConfig, logger }) => {
|
||||
// Parse MongoDB URI to extract host and port
|
||||
const url = new URL(mongoConfig.uri);
|
||||
return createMongoDBClient(
|
||||
{
|
||||
uri: mongoConfig.uri,
|
||||
host: url.hostname,
|
||||
port: parseInt(url.port || '27017'),
|
||||
database: mongoConfig.database,
|
||||
},
|
||||
logger
|
||||
);
|
||||
return new MongoDBClient(mongoConfig, logger);
|
||||
}).singleton(),
|
||||
|
||||
postgresClient: asFunction(({ postgresConfig, logger }) => {
|
||||
|
|
|
|||
|
|
@ -32,11 +32,9 @@ export class ConnectionFactory implements IConnectionFactory {
|
|||
this.logger.info('Creating MongoDB connection pool', {
|
||||
name: poolConfig.name,
|
||||
poolSize: poolConfig.poolSize,
|
||||
});
|
||||
|
||||
try {
|
||||
}); try {
|
||||
// Dynamic import to avoid circular dependency
|
||||
const { createMongoDBClient } = await import('@stock-bot/mongodb');
|
||||
const { MongoDBClient } = await import('@stock-bot/mongodb');
|
||||
|
||||
const events = {
|
||||
onConnect: () => {
|
||||
|
|
@ -50,7 +48,7 @@ export class ConnectionFactory implements IConnectionFactory {
|
|||
},
|
||||
};
|
||||
|
||||
const client = createMongoDBClient(poolConfig.config as any, events);
|
||||
const client = new MongoDBClient(poolConfig.config as any, this.logger, events);
|
||||
|
||||
await client.connect();
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { Collection, Db, MongoClient, OptionalUnlessRequiredId } from 'mongodb';
|
||||
import type { Logger } from '@stock-bot/core/logger';
|
||||
import type { DocumentBase, MongoDBClientConfig, PoolMetrics, ConnectionEvents, DynamicPoolConfig } from './types';
|
||||
|
||||
/**
|
||||
|
|
@ -12,17 +13,17 @@ export class MongoDBClient {
|
|||
private db: Db | null = null;
|
||||
private readonly config: MongoDBClientConfig;
|
||||
private defaultDatabase: string;
|
||||
private readonly logger: any;
|
||||
private readonly logger: Logger;
|
||||
private isConnected = false;
|
||||
private readonly metrics: PoolMetrics;
|
||||
private readonly events?: ConnectionEvents;
|
||||
private dynamicPoolConfig?: DynamicPoolConfig;
|
||||
private poolMonitorInterval?: Timer;
|
||||
|
||||
constructor(config: MongoDBClientConfig, logger?: any, events?: ConnectionEvents) {
|
||||
this.config = config;
|
||||
this.defaultDatabase = config.database || 'stock';
|
||||
this.logger = logger || console;
|
||||
constructor(mongoConfig: MongoDBClientConfig, logger: Logger, events?: ConnectionEvents) {
|
||||
this.config = mongoConfig;
|
||||
this.defaultDatabase = mongoConfig.database || 'stock';
|
||||
this.logger = logger;
|
||||
this.events = events;
|
||||
this.metrics = {
|
||||
totalConnections: 0,
|
||||
|
|
@ -311,7 +312,7 @@ export class MongoDBClient {
|
|||
* Get a collection (interface compatibility method)
|
||||
* This method provides compatibility with the IMongoDBClient interface
|
||||
*/
|
||||
collection(name: string, database?: string): Collection<any> {
|
||||
collection(name: string, database?: string): Collection<DocumentBase> {
|
||||
return this.getCollection(name, database);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,23 +1,5 @@
|
|||
import { MongoDBClient } from './client';
|
||||
import type { ConnectionEvents, MongoDBClientConfig } from './types';
|
||||
// This factory is no longer needed when using Awilix DI
|
||||
// The MongoDBClient is now registered directly in the DI container
|
||||
// See: libs/core/di/src/awilix-container.ts
|
||||
|
||||
/**
|
||||
* Factory function to create a MongoDB client instance
|
||||
*/
|
||||
export function createMongoDBClient(config: MongoDBClientConfig, logger?: any, events?: ConnectionEvents): MongoDBClient {
|
||||
return new MongoDBClient(config, logger, events);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create and connect a MongoDB client
|
||||
*/
|
||||
export async function createAndConnectMongoDBClient(
|
||||
config: MongoDBClientConfig,
|
||||
logger?: any,
|
||||
events?: ConnectionEvents
|
||||
): Promise<MongoDBClient> {
|
||||
const client = createMongoDBClient(config, logger, events);
|
||||
this.logger = logger || console; // Fallback to console if no logger provided
|
||||
await client.connect();
|
||||
return client;
|
||||
}
|
||||
export { MongoDBClient } from './client';
|
||||
|
|
@ -25,10 +25,5 @@ export type {
|
|||
DynamicPoolConfig,
|
||||
} from './types';
|
||||
|
||||
// Factory functions
|
||||
export {
|
||||
createMongoDBClient,
|
||||
createAndConnectMongoDBClient,
|
||||
} from './factory';
|
||||
|
||||
// Singleton pattern removed - use factory functions instead
|
||||
// Note: Factory functions removed - use Awilix DI container instead
|
||||
// See: libs/core/di/src/awilix-container.ts
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue