removed configs from all libs and will inject them within the services themselves
This commit is contained in:
parent
fd28162811
commit
6cc5b339bc
32 changed files with 366 additions and 349 deletions
|
|
@ -13,12 +13,10 @@
|
|||
"clean": "rimraf dist"
|
||||
},
|
||||
"dependencies": {
|
||||
"@stock-bot/config": "*",
|
||||
"@stock-bot/logger": "*",
|
||||
"@stock-bot/types": "*",
|
||||
"@types/mongodb": "^4.0.7",
|
||||
"mongodb": "^6.17.0",
|
||||
"yup": "^1.6.1"
|
||||
"mongodb": "^6.17.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.11.0",
|
||||
|
|
|
|||
|
|
@ -1,32 +1,24 @@
|
|||
import { Collection, Db, MongoClient, OptionalUnlessRequiredId } from 'mongodb';
|
||||
import { mongodbConfig } from '@stock-bot/config';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import type { DocumentBase } from './types';
|
||||
import type { DocumentBase, MongoDBClientConfig } from './types';
|
||||
|
||||
/**
|
||||
* Simplified MongoDB Client for Stock Bot Data Service
|
||||
* MongoDB Client for Stock Bot Data Service
|
||||
*
|
||||
* A singleton MongoDB client focused solely on batch upsert operations
|
||||
* MongoDB client focused on batch upsert operations
|
||||
* with minimal configuration and no health monitoring complexity.
|
||||
*/
|
||||
export class MongoDBClient {
|
||||
private static instance: MongoDBClient | null = null;
|
||||
private client: MongoClient | null = null;
|
||||
private db: Db | null = null;
|
||||
private defaultDatabase: string = 'stock'; // Default database name
|
||||
private readonly logger = getLogger('mongodb-client-simple');
|
||||
private readonly config: MongoDBClientConfig;
|
||||
private defaultDatabase: string;
|
||||
private readonly logger = getLogger('mongodb-client');
|
||||
private isConnected = false;
|
||||
|
||||
private constructor() {}
|
||||
|
||||
/**
|
||||
* Get singleton instance
|
||||
*/
|
||||
static getInstance(): MongoDBClient {
|
||||
if (!MongoDBClient.instance) {
|
||||
MongoDBClient.instance = new MongoDBClient();
|
||||
}
|
||||
return MongoDBClient.instance;
|
||||
constructor(config: MongoDBClientConfig) {
|
||||
this.config = config;
|
||||
this.defaultDatabase = config.database || 'stock';
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -42,18 +34,17 @@ export class MongoDBClient {
|
|||
this.logger.info('Connecting to MongoDB...');
|
||||
|
||||
this.client = new MongoClient(uri, {
|
||||
maxPoolSize: 10,
|
||||
minPoolSize: 1,
|
||||
connectTimeoutMS: 10000,
|
||||
socketTimeoutMS: 30000,
|
||||
serverSelectionTimeoutMS: 5000,
|
||||
maxPoolSize: this.config.poolSettings?.maxPoolSize || 10,
|
||||
minPoolSize: this.config.poolSettings?.minPoolSize || 1,
|
||||
connectTimeoutMS: this.config.timeouts?.connectTimeout || 10000,
|
||||
socketTimeoutMS: this.config.timeouts?.socketTimeout || 30000,
|
||||
serverSelectionTimeoutMS: this.config.timeouts?.serverSelectionTimeout || 5000,
|
||||
});
|
||||
|
||||
await this.client.connect();
|
||||
await this.client.db(mongodbConfig.MONGODB_DATABASE).admin().ping();
|
||||
await this.client.db(this.defaultDatabase).admin().ping();
|
||||
|
||||
// Set default database from config
|
||||
this.defaultDatabase = mongodbConfig.MONGODB_DATABASE;
|
||||
this.db = this.client.db(this.defaultDatabase);
|
||||
this.isConnected = true;
|
||||
|
||||
|
|
@ -347,18 +338,11 @@ export class MongoDBClient {
|
|||
}
|
||||
|
||||
private buildConnectionUri(): string {
|
||||
if (mongodbConfig.MONGODB_URI) {
|
||||
return mongodbConfig.MONGODB_URI;
|
||||
if (this.config.uri) {
|
||||
return this.config.uri;
|
||||
}
|
||||
|
||||
const {
|
||||
MONGODB_HOST: host,
|
||||
MONGODB_PORT: port,
|
||||
MONGODB_USERNAME: username,
|
||||
MONGODB_PASSWORD: password,
|
||||
MONGODB_DATABASE: database,
|
||||
MONGODB_AUTH_SOURCE: authSource,
|
||||
} = mongodbConfig;
|
||||
const { host, port, username, password, database, authSource } = this.config;
|
||||
|
||||
// Build URI components
|
||||
const auth = username && password ? `${username}:${password}@` : '';
|
||||
|
|
|
|||
|
|
@ -1,53 +1,20 @@
|
|||
import { MongoDBClient } from './client';
|
||||
import type { MongoDBClientConfig } from './types';
|
||||
|
||||
/**
|
||||
* Get the singleton MongoDB client instance
|
||||
* Factory function to create a MongoDB client instance
|
||||
*/
|
||||
export function getMongoDBClient(): MongoDBClient {
|
||||
return MongoDBClient.getInstance();
|
||||
export function createMongoDBClient(config: MongoDBClientConfig): MongoDBClient {
|
||||
return new MongoDBClient(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to MongoDB using the singleton client
|
||||
* Create and connect a MongoDB client
|
||||
*/
|
||||
export async function connectMongoDB(): Promise<MongoDBClient> {
|
||||
const client = getMongoDBClient();
|
||||
if (!client.connected) {
|
||||
await client.connect();
|
||||
}
|
||||
export async function createAndConnectMongoDBClient(
|
||||
config: MongoDBClientConfig
|
||||
): Promise<MongoDBClient> {
|
||||
const client = createMongoDBClient(config);
|
||||
await client.connect();
|
||||
return client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnect from MongoDB
|
||||
*/
|
||||
export async function disconnectMongoDB(): Promise<void> {
|
||||
const client = getMongoDBClient();
|
||||
if (client.connected) {
|
||||
await client.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default database for all operations
|
||||
*/
|
||||
export function setDefaultDatabase(databaseName: string): void {
|
||||
const client = getMongoDBClient();
|
||||
client.setDefaultDatabase(databaseName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current default database name
|
||||
*/
|
||||
export function getCurrentDatabase(): string {
|
||||
const client = getMongoDBClient();
|
||||
return client.getDefaultDatabase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a database instance by name
|
||||
*/
|
||||
export function getDatabase(databaseName?: string) {
|
||||
const client = getMongoDBClient();
|
||||
return client.getDatabase(databaseName);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
* Simplified MongoDB Client Library for Stock Bot Data Service
|
||||
* MongoDB Client Library for Stock Bot Data Service
|
||||
*
|
||||
* Provides a singleton MongoDB client focused on batch upsert operations
|
||||
* Provides a MongoDB client focused on batch upsert operations
|
||||
* for high-performance data ingestion.
|
||||
*/
|
||||
|
||||
|
|
@ -14,6 +14,8 @@ export type {
|
|||
EarningsTranscript,
|
||||
ExchangeSourceMapping,
|
||||
MasterExchange,
|
||||
MongoDBClientConfig,
|
||||
MongoDBConnectionOptions,
|
||||
NewsArticle,
|
||||
RawDocument,
|
||||
SecFiling,
|
||||
|
|
@ -22,10 +24,6 @@ export type {
|
|||
|
||||
// Factory functions
|
||||
export {
|
||||
connectMongoDB,
|
||||
disconnectMongoDB,
|
||||
getCurrentDatabase,
|
||||
getDatabase,
|
||||
getMongoDBClient,
|
||||
setDefaultDatabase,
|
||||
createMongoDBClient,
|
||||
createAndConnectMongoDBClient,
|
||||
} from './factory';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue