cleanup
This commit is contained in:
parent
c353ddbe01
commit
ce9e2df67c
310 changed files with 2550 additions and 36861 deletions
|
|
@ -80,7 +80,7 @@ export class MongoDBClient {
|
|||
});
|
||||
|
||||
await this.client.connect();
|
||||
await this.client.db(this.defaultDatabase).admin().ping();
|
||||
await this.client.db(this.defaultDatabase).command({ ping: 1 });
|
||||
|
||||
// Set default database from config
|
||||
this.db = this.client.db(this.defaultDatabase);
|
||||
|
|
@ -201,6 +201,41 @@ export class MongoDBClient {
|
|||
return this.client.db(dbName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether this client is locked to read-only mode
|
||||
*/
|
||||
isReadOnly(): boolean {
|
||||
return this.config.readOnly === true;
|
||||
}
|
||||
|
||||
private assertWritable(operation: string): void {
|
||||
if (this.config.readOnly) {
|
||||
throw new Error(
|
||||
`MongoDB client is configured in read-only mode; write operation "${operation}" is forbidden`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private assertReadOnlyPipeline(pipeline: Document[]): void {
|
||||
if (!this.config.readOnly) {
|
||||
return;
|
||||
}
|
||||
const checkStage = (value: unknown): void => {
|
||||
if (!value || typeof value !== 'object') return;
|
||||
for (const [key, child] of Object.entries(value)) {
|
||||
if (key === '$out' || key === '$merge') {
|
||||
throw new Error(
|
||||
`MongoDB client is configured in read-only mode; aggregation stage "${key}" is forbidden`
|
||||
);
|
||||
}
|
||||
checkStage(child);
|
||||
}
|
||||
};
|
||||
for (const stage of pipeline) {
|
||||
checkStage(stage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch upsert documents for high-performance operations
|
||||
* Supports single or multiple unique keys for matching
|
||||
|
|
@ -216,6 +251,7 @@ export class MongoDBClient {
|
|||
database?: string; // Optional database override
|
||||
} = {}
|
||||
): Promise<{ insertedCount: number; updatedCount: number; errors: unknown[] }> {
|
||||
this.assertWritable(`batchUpsert(${collectionName})`);
|
||||
if (!this.client) {
|
||||
throw new Error('MongoDB client not connected');
|
||||
}
|
||||
|
|
@ -367,6 +403,7 @@ export class MongoDBClient {
|
|||
Partial<Pick<T, 'created_at' | 'updated_at'>>,
|
||||
database?: string
|
||||
): Promise<T> {
|
||||
this.assertWritable(`insertOne(${collectionName})`);
|
||||
const collection = this.getCollection<T>(collectionName, database);
|
||||
|
||||
const now = new Date();
|
||||
|
|
@ -389,6 +426,7 @@ export class MongoDBClient {
|
|||
options?: BulkWriteOptions,
|
||||
dbName?: string
|
||||
) {
|
||||
this.assertWritable(`insertMany(${collectionName})`);
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
const now = new Date();
|
||||
|
||||
|
|
@ -446,6 +484,7 @@ export class MongoDBClient {
|
|||
options?: UpdateOptions,
|
||||
dbName?: string
|
||||
) {
|
||||
this.assertWritable(`updateOne(${collectionName})`);
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
|
||||
// Add updated_at timestamp
|
||||
|
|
@ -474,6 +513,7 @@ export class MongoDBClient {
|
|||
options?: UpdateOptions,
|
||||
dbName?: string
|
||||
) {
|
||||
this.assertWritable(`updateMany(${collectionName})`);
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
|
||||
// Add updated_at timestamp
|
||||
|
|
@ -501,6 +541,7 @@ export class MongoDBClient {
|
|||
options?: DeleteOptions,
|
||||
dbName?: string
|
||||
) {
|
||||
this.assertWritable(`deleteOne(${collectionName})`);
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
const result = await collection.deleteOne(filter, options);
|
||||
return {
|
||||
|
|
@ -517,6 +558,7 @@ export class MongoDBClient {
|
|||
options?: DeleteOptions,
|
||||
dbName?: string
|
||||
) {
|
||||
this.assertWritable(`deleteMany(${collectionName})`);
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
const result = await collection.deleteMany(filter, options);
|
||||
return {
|
||||
|
|
@ -546,6 +588,7 @@ export class MongoDBClient {
|
|||
options?: AggregateOptions,
|
||||
dbName?: string
|
||||
): Promise<T[]> {
|
||||
this.assertReadOnlyPipeline(pipeline);
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
const cursor = collection.aggregate(pipeline, options);
|
||||
return (await cursor.toArray()) as T[];
|
||||
|
|
@ -560,6 +603,7 @@ export class MongoDBClient {
|
|||
options?: CreateIndexesOptions,
|
||||
dbName?: string
|
||||
): Promise<string> {
|
||||
this.assertWritable(`createIndex(${collectionName})`);
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
return await collection.createIndex(indexSpec, options);
|
||||
}
|
||||
|
|
@ -573,6 +617,7 @@ export class MongoDBClient {
|
|||
options?: DropIndexesOptions,
|
||||
dbName?: string
|
||||
): Promise<void> {
|
||||
this.assertWritable(`dropIndex(${collectionName})`);
|
||||
const collection = this.getCollection(collectionName, dbName);
|
||||
await collection.dropIndex(indexName, options);
|
||||
}
|
||||
|
|
@ -601,6 +646,7 @@ export class MongoDBClient {
|
|||
options?: CreateCollectionOptions,
|
||||
dbName?: string
|
||||
): Promise<void> {
|
||||
this.assertWritable(`createCollection(${collectionName})`);
|
||||
const db = this.getDatabase(dbName);
|
||||
await db.createCollection(collectionName, options);
|
||||
}
|
||||
|
|
@ -609,6 +655,7 @@ export class MongoDBClient {
|
|||
* Drop a collection
|
||||
*/
|
||||
async dropCollection(collectionName: string, dbName?: string): Promise<void> {
|
||||
this.assertWritable(`dropCollection(${collectionName})`);
|
||||
const db = this.getDatabase(dbName);
|
||||
await db.dropCollection(collectionName);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ export interface MongoDBClientConfig {
|
|||
password?: string;
|
||||
authSource?: string;
|
||||
uri?: string;
|
||||
readOnly?: boolean;
|
||||
poolSettings?: {
|
||||
maxPoolSize: number;
|
||||
minPoolSize: number;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue