fixed all libs to be buildiable and dependency hell from removing some

This commit is contained in:
Bojan Kucera 2025-06-04 16:07:08 -04:00
parent 5c64b1ccf8
commit a282dac6cd
40 changed files with 4050 additions and 8219 deletions

View file

@ -1,3 +1,4 @@
import type { Document } from 'mongodb';
import type { MongoDBClient } from './client';
import type { CollectionNames } from './types';
@ -103,11 +104,10 @@ export class MongoDBAggregationBuilder {
this.pipeline.push(stage);
return this;
}
/**
* Execute the aggregation pipeline
*/
async execute<T = any>(): Promise<T[]> {
async execute<T extends Document = Document>(): Promise<T[]> {
if (!this.collection) {
throw new Error('Collection not specified. Use .from() to set the collection.');
}

View file

@ -1,4 +1,4 @@
import { MongoClient, Db, Collection, MongoClientOptions } from 'mongodb';
import { MongoClient, Db, Collection, MongoClientOptions, Document, WithId, OptionalUnlessRequiredId } from 'mongodb';
import { mongodbConfig } from '@stock-bot/config';
import { Logger } from '@stock-bot/logger';
import type {
@ -144,12 +144,10 @@ export class MongoDBClient {
...document,
created_at: document.created_at || now,
updated_at: now
} as T;
// Validate document if schema exists
if (schemaMap[collectionName]) {
} as T; // Validate document if schema exists
if (collectionName in schemaMap) {
try {
schemaMap[collectionName].parse(docWithTimestamps);
(schemaMap as any)[collectionName].parse(docWithTimestamps);
} catch (error) {
if (error instanceof z.ZodError) {
this.logger.error(`Document validation failed for ${collectionName}:`, error.errors);
@ -157,9 +155,7 @@ export class MongoDBClient {
}
throw error;
}
}
const result = await collection.insertOne(docWithTimestamps);
} const result = await collection.insertOne(docWithTimestamps as OptionalUnlessRequiredId<T>);
return { ...docWithTimestamps, _id: result.insertedId } as T;
}
@ -182,7 +178,6 @@ export class MongoDBClient {
const result = await collection.updateOne(filter, { $set: updateWithTimestamp });
return result.modifiedCount > 0;
}
/**
* Find documents with optional validation
*/
@ -192,7 +187,7 @@ export class MongoDBClient {
options: any = {}
): Promise<T[]> {
const collection = this.getCollection<T>(collectionName);
return await collection.find(filter, options).toArray();
return await collection.find(filter, options).toArray() as T[];
}
/**
@ -203,7 +198,7 @@ export class MongoDBClient {
filter: any
): Promise<T | null> {
const collection = this.getCollection<T>(collectionName);
return await collection.findOne(filter);
return await collection.findOne(filter) as T | null;
}
/**
@ -259,11 +254,12 @@ export class MongoDBClient {
{ key: { filing_type: 1 } },
{ key: { cik: 1 } },
{ key: { created_at: -1 } }
]);
// Raw documents indexes
]); // Raw documents indexes
await this.db.collection('raw_documents').createIndex(
{ content_hash: 1 },
{ unique: true }
);
await this.db.collection('raw_documents').createIndexes([
{ key: { content_hash: 1 }, options: { unique: true } },
{ key: { processing_status: 1 } },
{ key: { document_type: 1 } },
{ key: { created_at: -1 } }
@ -366,8 +362,11 @@ export class MongoDBClient {
serverSelectionTimeoutMS: this.config.timeouts?.serverSelectionTimeout,
retryWrites: this.config.options?.retryWrites,
journal: this.config.options?.journal,
readPreference: this.config.options?.readPreference,
writeConcern: { w: this.config.options?.writeConcern },
readPreference: this.config.options?.readPreference, writeConcern: this.config.options?.writeConcern ? {
w: this.config.options.writeConcern === 'majority'
? 'majority' as const
: parseInt(this.config.options.writeConcern, 10) || 1
} : undefined,
tls: this.config.tls?.enabled,
tlsInsecure: this.config.tls?.insecure,
tlsCAFile: this.config.tls?.caFile

View file

@ -176,10 +176,8 @@ export class MongoDBHealthMonitor {
// Average latency (from durability stats if available)
if (dur.timeMS) {
this.metrics.averageLatency = dur.timeMS.dt || 0;
}
} catch (error) {
this.logger.debug('Error parsing server status for metrics:', error);
} } catch (error) {
this.logger.debug('Error parsing server status for metrics:', error as any);
}
}

View file

@ -1,6 +1,7 @@
import { Logger } from '@stock-bot/logger';
import type { MongoDBClient } from './client';
import type { CollectionNames, DocumentBase } from './types';
import type { WithId, OptionalUnlessRequiredId } from 'mongodb';
/**
* MongoDB Transaction Manager
@ -41,10 +42,9 @@ export class MongoDBTransactionManager {
const result = await session.withTransaction(
async () => {
return await operations(session);
},
{
readPreference: options?.readPreference,
readConcern: { level: options?.readConcern || 'majority' },
}, {
readPreference: options?.readPreference as any,
readConcern: { level: options?.readConcern || 'majority' } as any,
writeConcern: options?.writeConcern || { w: 'majority' },
maxCommitTimeMS: options?.maxCommitTimeMS || 10000
}
@ -155,21 +155,17 @@ export class MongoDBTransactionManager {
if (documents.length === 0) {
return 0;
}
// Transform documents if needed
} // Transform documents if needed
const documentsToInsert = transform
? documents.map(transform)
? documents.map((doc: WithId<T>) => transform(doc as T))
: documents;
// Add updated timestamp
const now = new Date();
documentsToInsert.forEach(doc => {
doc.updated_at = now;
});
// Insert into target collection
await targetCollection.insertMany(documentsToInsert, { session });
}); // Insert into target collection
await targetCollection.insertMany(documentsToInsert as OptionalUnlessRequiredId<T>[], { session });
// Remove from source collection
const deleteResult = await sourceCollection.deleteMany(filter, { session });