(
method: 'insertOne' | 'insertMany' | 'updateMany' | 'bulkUpdateMany' | 'deleteMany' | 'countDocuments',
entityName: EntityName<T>,
data?: Partial<T> | Partial<T>[],
where?: FilterQuery<T> | FilterQuery<T>[],
ctx?: Transaction<ClientSession>,
opts?: {
upsert?: boolean;
upsertOptions?: UpsertOptions<T>;
loggerContext?: LoggingOptions;
collation?: CollationOptions;
indexHint?: string | Dictionary;
maxTimeMS?: number;
signal?: AbortSignal;
},
)
| 447 | } |
| 448 | |
| 449 | private async runQuery<T extends object, U extends QueryResult<T> | number = QueryResult<T>>( |
| 450 | method: 'insertOne' | 'insertMany' | 'updateMany' | 'bulkUpdateMany' | 'deleteMany' | 'countDocuments', |
| 451 | entityName: EntityName<T>, |
| 452 | data?: Partial<T> | Partial<T>[], |
| 453 | where?: FilterQuery<T> | FilterQuery<T>[], |
| 454 | ctx?: Transaction<ClientSession>, |
| 455 | opts?: { |
| 456 | upsert?: boolean; |
| 457 | upsertOptions?: UpsertOptions<T>; |
| 458 | loggerContext?: LoggingOptions; |
| 459 | collation?: CollationOptions; |
| 460 | indexHint?: string | Dictionary; |
| 461 | maxTimeMS?: number; |
| 462 | signal?: AbortSignal; |
| 463 | }, |
| 464 | ): Promise<U> { |
| 465 | await this.ensureConnection(); |
| 466 | const { upsert, upsertOptions, loggerContext, collation, indexHint, maxTimeMS, signal } = opts ?? {}; |
| 467 | const collection = this.getCollectionName(entityName); |
| 468 | const logger = this.config.getLogger(); |
| 469 | const options: Dictionary = ctx ? { session: ctx, upsert } : { upsert }; |
| 470 | |
| 471 | if (options.upsert === undefined) { |
| 472 | delete options.upsert; |
| 473 | } |
| 474 | |
| 475 | if (signal) { |
| 476 | options.signal = signal; |
| 477 | } |
| 478 | |
| 479 | const now = Date.now(); |
| 480 | let res: InsertOneResult<T> | InsertManyResult<T> | UpdateResult | DeleteResult | BulkWriteResult | number; |
| 481 | let query: string; |
| 482 | const log = (msg: () => string) => (logger.isEnabled('query') ? msg() : ''); |
| 483 | |
| 484 | switch (method) { |
| 485 | case 'insertOne': |
| 486 | Object.keys(data as Dictionary) |
| 487 | .filter(k => typeof (data as Dictionary)[k] === 'undefined') |
| 488 | .forEach(k => delete (data as Dictionary)[k]); |
| 489 | query = log( |
| 490 | () => `db.getCollection('${collection}').insertOne(${this.logObject(data)}, ${this.logObject(options)});`, |
| 491 | ); |
| 492 | res = await this.rethrow( |
| 493 | this.getCollection(entityName).insertOne(data as OptionalUnlessRequiredId<T>, options), |
| 494 | query, |
| 495 | ); |
| 496 | break; |
| 497 | case 'insertMany': |
| 498 | (data as Dictionary[]).forEach(data => |
| 499 | Object.keys(data) |
| 500 | .filter(k => typeof data[k] === 'undefined') |
| 501 | .forEach(k => delete data[k]), |
| 502 | ); |
| 503 | query = log( |
| 504 | () => `db.getCollection('${collection}').insertMany(${this.logObject(data)}, ${this.logObject(options)});`, |
| 505 | ); |
| 506 | res = await this.rethrow( |
no test coverage detected