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