work on ceo
This commit is contained in:
parent
c8dcd697c9
commit
b25222778e
18 changed files with 391 additions and 110 deletions
|
|
@ -363,6 +363,254 @@ export class MongoDBClient {
|
|||
return { ...docWithTimestamps, _id: result.insertedId } as T;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert multiple documents
|
||||
*/
|
||||
async insertMany<T = any>(
|
||||
collectionName: string,
|
||||
documents: T[],
|
||||
options?: any,
|
||||
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,
|
||||
updated_at: now,
|
||||
}));
|
||||
|
||||
const result = await collection.insertMany(docsWithTimestamps as any, options);
|
||||
return {
|
||||
insertedCount: result.insertedCount,
|
||||
insertedIds: result.insertedIds,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Find multiple documents
|
||||
*/
|
||||
async find<T = any>(
|
||||
collectionName: string,
|
||||
filter: any = {},
|
||||
options?: any,
|
||||
dbName?: string
|
||||
): Promise<T[]> {
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
const cursor = collection.find(filter, options);
|
||||
return await cursor.toArray() as T[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a single document
|
||||
*/
|
||||
async findOne<T = any>(
|
||||
collectionName: string,
|
||||
filter: any,
|
||||
options?: any,
|
||||
dbName?: string
|
||||
): Promise<T | null> {
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
const result = await collection.findOne(filter, options);
|
||||
return result as T | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single document
|
||||
*/
|
||||
async updateOne(
|
||||
collectionName: string,
|
||||
filter: any,
|
||||
update: any,
|
||||
options?: any,
|
||||
dbName?: string
|
||||
): Promise<any> {
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
|
||||
// Add updated_at timestamp
|
||||
if (update.$set) {
|
||||
update.$set.updated_at = new Date();
|
||||
} else if (!update.$setOnInsert && !update.$unset && !update.$inc) {
|
||||
update = { $set: { ...update, updated_at: new Date() } };
|
||||
}
|
||||
|
||||
const result = await collection.updateOne(filter, update, options);
|
||||
return {
|
||||
matchedCount: result.matchedCount,
|
||||
modifiedCount: result.modifiedCount,
|
||||
upsertedCount: result.upsertedCount,
|
||||
upsertedId: result.upsertedId,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Update multiple documents
|
||||
*/
|
||||
async updateMany(
|
||||
collectionName: string,
|
||||
filter: any,
|
||||
update: any,
|
||||
options?: any,
|
||||
dbName?: string
|
||||
): Promise<any> {
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
|
||||
// Add updated_at timestamp
|
||||
if (update.$set) {
|
||||
update.$set.updated_at = new Date();
|
||||
} else if (!update.$setOnInsert && !update.$unset && !update.$inc) {
|
||||
update = { $set: { ...update, updated_at: new Date() } };
|
||||
}
|
||||
|
||||
const result = await collection.updateMany(filter, update, options);
|
||||
return {
|
||||
matchedCount: result.matchedCount,
|
||||
modifiedCount: result.modifiedCount,
|
||||
upsertedCount: result.upsertedCount,
|
||||
upsertedId: result.upsertedId,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a single document
|
||||
*/
|
||||
async deleteOne(
|
||||
collectionName: string,
|
||||
filter: any,
|
||||
options?: any,
|
||||
dbName?: string
|
||||
): Promise<any> {
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
const result = await collection.deleteOne(filter, options);
|
||||
return {
|
||||
deletedCount: result.deletedCount,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete multiple documents
|
||||
*/
|
||||
async deleteMany(
|
||||
collectionName: string,
|
||||
filter: any,
|
||||
options?: any,
|
||||
dbName?: string
|
||||
): Promise<any> {
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
const result = await collection.deleteMany(filter, options);
|
||||
return {
|
||||
deletedCount: result.deletedCount,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Count documents matching a filter
|
||||
*/
|
||||
async countDocuments(
|
||||
collectionName: string,
|
||||
filter: any = {},
|
||||
options?: any,
|
||||
dbName?: string
|
||||
): Promise<number> {
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
return await collection.countDocuments(filter, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform aggregation operations
|
||||
*/
|
||||
async aggregate<T = any>(
|
||||
collectionName: string,
|
||||
pipeline: any[],
|
||||
options?: any,
|
||||
dbName?: string
|
||||
): Promise<T[]> {
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
const cursor = collection.aggregate(pipeline, options);
|
||||
return await cursor.toArray() as T[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an index
|
||||
*/
|
||||
async createIndex(
|
||||
collectionName: string,
|
||||
indexSpec: any,
|
||||
options?: any,
|
||||
dbName?: string
|
||||
): Promise<string> {
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
return await collection.createIndex(indexSpec, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop an index
|
||||
*/
|
||||
async dropIndex(
|
||||
collectionName: string,
|
||||
indexName: string,
|
||||
options?: any,
|
||||
dbName?: string
|
||||
): Promise<void> {
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
await collection.dropIndex(indexName, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* List all indexes on a collection
|
||||
*/
|
||||
async listIndexes(
|
||||
collectionName: string,
|
||||
dbName?: string
|
||||
): Promise<any[]> {
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
const cursor = collection.listIndexes();
|
||||
return await cursor.toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a database instance (interface compatibility)
|
||||
*/
|
||||
getDb(dbName?: string): Db {
|
||||
return this.getDatabase(dbName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new collection
|
||||
*/
|
||||
async createCollection(
|
||||
collectionName: string,
|
||||
options?: any,
|
||||
dbName?: string
|
||||
): Promise<void> {
|
||||
const db = this.getDatabase(dbName);
|
||||
await db.createCollection(collectionName, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop a collection
|
||||
*/
|
||||
async dropCollection(
|
||||
collectionName: string,
|
||||
dbName?: string
|
||||
): Promise<void> {
|
||||
const db = this.getDatabase(dbName);
|
||||
await db.dropCollection(collectionName);
|
||||
}
|
||||
|
||||
/**
|
||||
* List all collections in a database
|
||||
*/
|
||||
async listCollections(
|
||||
filter: any = {},
|
||||
dbName?: string
|
||||
): Promise<any[]> {
|
||||
const db = this.getDatabase(dbName);
|
||||
const collections = await db.listCollections(filter).toArray();
|
||||
return collections;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if client is connected
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue