commit 2 refactor

This commit is contained in:
Boki 2026-10-20 14:35:45 -04:00
parent 588f666943
commit 32e43c9243
310 changed files with 33318 additions and 31189 deletions

View file

@ -1,6 +1,20 @@
import type { Logger } from '@stock-bot/logger';
import type { Document, OptionalUnlessRequiredId } from 'mongodb';
import type {
AggregateOptions,
BulkWriteOptions,
CountDocumentsOptions,
CreateCollectionOptions,
CreateIndexesOptions,
DeleteOptions,
Document,
DropIndexesOptions,
Filter,
FindOptions,
IndexSpecification,
OptionalUnlessRequiredId,
UpdateOptions,
} from 'mongodb';
import { Collection, Db, MongoClient } from 'mongodb';
import type { Logger } from '@stock-bot/logger';
import type {
ConnectionEvents,
DocumentBase,
@ -312,15 +326,18 @@ export class MongoDBClient {
}
}
this.logger.debug(`Batch upsert completed [${collectionName}-${documents.length}][${operationId}]`, {
database: dbName,
collection: collectionName,
totalRecords: documents.length,
inserted: totalInserted,
updated: totalUpdated,
errors: errors.length,
uniqueKeys: keyFields,
});
this.logger.debug(
`Batch upsert completed [${collectionName}-${documents.length}][${operationId}]`,
{
database: dbName,
collection: collectionName,
totalRecords: documents.length,
inserted: totalInserted,
updated: totalUpdated,
errors: errors.length,
uniqueKeys: keyFields,
}
);
return { insertedCount: totalInserted, updatedCount: totalUpdated, errors };
}
@ -366,22 +383,25 @@ export class MongoDBClient {
/**
* Insert multiple documents
*/
async insertMany<T = any>(
async insertMany<T extends Document = Document>(
collectionName: string,
documents: T[],
options?: any,
options?: BulkWriteOptions,
dbName?: string
): Promise<any> {
) {
const collection = this.getCollection(collectionName, dbName);
const now = new Date();
const docsWithTimestamps = documents.map(doc => ({
...doc,
created_at: (doc as any).created_at || now,
created_at: doc.created_at || now,
updated_at: now,
}));
const result = await collection.insertMany(docsWithTimestamps as any, options);
const result = await collection.insertMany(
docsWithTimestamps as OptionalUnlessRequiredId<Document>[],
options
);
return {
insertedCount: result.insertedCount,
insertedIds: result.insertedIds,
@ -391,13 +411,13 @@ export class MongoDBClient {
/**
* Find multiple documents
*/
async find<T = any>(
async find<T extends Document = Document>(
collectionName: string,
filter: any = {},
options?: any,
filter: Filter<Document> = {},
options?: FindOptions,
dbName?: string
): Promise<T[]> {
const collection = this.getCollection(collectionName, dbName);
const collection = this.getCollection<T>(collectionName, dbName);
const cursor = collection.find(filter, options);
return (await cursor.toArray()) as T[];
}
@ -405,13 +425,13 @@ export class MongoDBClient {
/**
* Find a single document
*/
async findOne<T = any>(
async findOne<T extends Document = Document>(
collectionName: string,
filter: any,
options?: any,
filter: Filter<Document>,
options?: FindOptions,
dbName?: string
): Promise<T | null> {
const collection = this.getCollection(collectionName, dbName);
const collection = this.getCollection<T>(collectionName, dbName);
const result = await collection.findOne(filter, options);
return result as T | null;
}
@ -421,11 +441,11 @@ export class MongoDBClient {
*/
async updateOne(
collectionName: string,
filter: any,
update: any,
options?: any,
filter: Filter<Document>,
update: Document,
options?: UpdateOptions,
dbName?: string
): Promise<any> {
) {
const collection = this.getCollection(collectionName, dbName);
// Add updated_at timestamp
@ -449,11 +469,11 @@ export class MongoDBClient {
*/
async updateMany(
collectionName: string,
filter: any,
update: any,
options?: any,
filter: Filter<Document>,
update: Document,
options?: UpdateOptions,
dbName?: string
): Promise<any> {
) {
const collection = this.getCollection(collectionName, dbName);
// Add updated_at timestamp
@ -477,10 +497,10 @@ export class MongoDBClient {
*/
async deleteOne(
collectionName: string,
filter: any,
options?: any,
filter: Filter<Document>,
options?: DeleteOptions,
dbName?: string
): Promise<any> {
) {
const collection = this.getCollection(collectionName, dbName);
const result = await collection.deleteOne(filter, options);
return {
@ -493,10 +513,10 @@ export class MongoDBClient {
*/
async deleteMany(
collectionName: string,
filter: any,
options?: any,
filter: Filter<Document>,
options?: DeleteOptions,
dbName?: string
): Promise<any> {
) {
const collection = this.getCollection(collectionName, dbName);
const result = await collection.deleteMany(filter, options);
return {
@ -509,8 +529,8 @@ export class MongoDBClient {
*/
async countDocuments(
collectionName: string,
filter: any = {},
options?: any,
filter: Filter<Document> = {},
options?: CountDocumentsOptions,
dbName?: string
): Promise<number> {
const collection = this.getCollection(collectionName, dbName);
@ -520,10 +540,10 @@ export class MongoDBClient {
/**
* Perform aggregation operations
*/
async aggregate<T = any>(
async aggregate<T extends Document = Document>(
collectionName: string,
pipeline: any[],
options?: any,
pipeline: Document[],
options?: AggregateOptions,
dbName?: string
): Promise<T[]> {
const collection = this.getCollection(collectionName, dbName);
@ -536,8 +556,8 @@ export class MongoDBClient {
*/
async createIndex(
collectionName: string,
indexSpec: any,
options?: any,
indexSpec: IndexSpecification,
options?: CreateIndexesOptions,
dbName?: string
): Promise<string> {
const collection = this.getCollection(collectionName, dbName);
@ -550,7 +570,7 @@ export class MongoDBClient {
async dropIndex(
collectionName: string,
indexName: string,
options?: any,
options?: DropIndexesOptions,
dbName?: string
): Promise<void> {
const collection = this.getCollection(collectionName, dbName);
@ -560,7 +580,7 @@ export class MongoDBClient {
/**
* List all indexes on a collection
*/
async listIndexes(collectionName: string, dbName?: string): Promise<any[]> {
async listIndexes(collectionName: string, dbName?: string): Promise<Document[]> {
const collection = this.getCollection(collectionName, dbName);
const cursor = collection.listIndexes();
return await cursor.toArray();
@ -576,7 +596,11 @@ export class MongoDBClient {
/**
* Create a new collection
*/
async createCollection(collectionName: string, options?: any, dbName?: string): Promise<void> {
async createCollection(
collectionName: string,
options?: CreateCollectionOptions,
dbName?: string
): Promise<void> {
const db = this.getDatabase(dbName);
await db.createCollection(collectionName, options);
}
@ -592,7 +616,7 @@ export class MongoDBClient {
/**
* List all collections in a database
*/
async listCollections(filter: any = {}, dbName?: string): Promise<any[]> {
async listCollections(filter: Filter<Document> = {}, dbName?: string): Promise<Document[]> {
const db = this.getDatabase(dbName);
const collections = await db.listCollections(filter).toArray();
return collections;