|
| 1 | +import type { Document } from '../bson'; |
| 2 | +import { type ClientBulkWriteCursorResponse } from '../cmap/wire_protocol/responses'; |
| 3 | +import type { MongoClient } from '../mongo_client'; |
| 4 | +import { ClientBulkWriteOperation } from '../operations/client_bulk_write/client_bulk_write'; |
| 5 | +import { type ClientBulkWriteOptions } from '../operations/client_bulk_write/common'; |
| 6 | +import { executeOperation } from '../operations/execute_operation'; |
| 7 | +import type { ClientSession } from '../sessions'; |
| 8 | +import { mergeOptions, MongoDBNamespace } from '../utils'; |
| 9 | +import { |
| 10 | + AbstractCursor, |
| 11 | + type AbstractCursorOptions, |
| 12 | + type InitialCursorResponse |
| 13 | +} from './abstract_cursor'; |
| 14 | + |
| 15 | +/** @public */ |
| 16 | +export interface ClientBulkWriteCursorOptions |
| 17 | + extends AbstractCursorOptions, |
| 18 | + ClientBulkWriteOptions {} |
| 19 | + |
| 20 | +/** |
| 21 | + * This is the cursor that handles client bulk write operations. Note this is never |
| 22 | + * exposed directly to the user and is always immediately exhausted. |
| 23 | + * @internal |
| 24 | + */ |
| 25 | +export class ClientBulkWriteCursor extends AbstractCursor { |
| 26 | + public readonly command: Document; |
| 27 | + /** @internal */ |
| 28 | + private cursorResponse?: ClientBulkWriteCursorResponse; |
| 29 | + /** @internal */ |
| 30 | + private clientBulkWriteOptions: ClientBulkWriteOptions; |
| 31 | + |
| 32 | + /** @internal */ |
| 33 | + constructor(client: MongoClient, command: Document, options: ClientBulkWriteOptions = {}) { |
| 34 | + super(client, new MongoDBNamespace('admin', '$cmd'), options); |
| 35 | + |
| 36 | + this.command = command; |
| 37 | + this.clientBulkWriteOptions = options; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * We need a way to get the top level cursor response fields for |
| 42 | + * generating the bulk write result, so we expose this here. |
| 43 | + */ |
| 44 | + get response(): ClientBulkWriteCursorResponse { |
| 45 | + if (this.cursorResponse) return this.cursorResponse; |
| 46 | + throw new Error('no cursor response'); |
| 47 | + } |
| 48 | + |
| 49 | + clone(): ClientBulkWriteCursor { |
| 50 | + const clonedOptions = mergeOptions({}, this.clientBulkWriteOptions); |
| 51 | + delete clonedOptions.session; |
| 52 | + return new ClientBulkWriteCursor(this.client, this.command, { |
| 53 | + ...clonedOptions |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + /** @internal */ |
| 58 | + async _initialize(session: ClientSession): Promise<InitialCursorResponse> { |
| 59 | + const clientBulkWriteOperation = new ClientBulkWriteOperation(this.command, { |
| 60 | + ...this.clientBulkWriteOptions, |
| 61 | + ...this.cursorOptions, |
| 62 | + session |
| 63 | + }); |
| 64 | + |
| 65 | + const response = await executeOperation(this.client, clientBulkWriteOperation); |
| 66 | + this.cursorResponse = response; |
| 67 | + |
| 68 | + return { server: clientBulkWriteOperation.server, session, response }; |
| 69 | + } |
| 70 | +} |
0 commit comments