|
| 1 | +import type { BSONSerializeOptions, Document, Long } from '../bson'; |
| 2 | +import type { Db } from '../db'; |
| 3 | +import { MongoAPIError, MongoUnexpectedServerResponseError } from '../error'; |
| 4 | +import { executeOperation, ExecutionResult } from '../operations/execute_operation'; |
| 5 | +import { GetMoreOperation } from '../operations/get_more'; |
| 6 | +import { RunCommandOperation } from '../operations/run_command'; |
| 7 | +import type { ReadConcernLike } from '../read_concern'; |
| 8 | +import type { ReadPreferenceLike } from '../read_preference'; |
| 9 | +import type { ClientSession } from '../sessions'; |
| 10 | +import { Callback, ns } from '../utils'; |
| 11 | +import { AbstractCursor } from './abstract_cursor'; |
| 12 | + |
| 13 | +/** @public */ |
| 14 | +export type RunCursorCommandOptions = { |
| 15 | + readPreference?: ReadPreferenceLike; |
| 16 | + session?: ClientSession; |
| 17 | +} & BSONSerializeOptions; |
| 18 | + |
| 19 | +/** @internal */ |
| 20 | +type RunCursorCommandResponse = { |
| 21 | + cursor: { id: bigint | Long | number; ns: string; firstBatch: Document[] }; |
| 22 | + ok: 1; |
| 23 | +}; |
| 24 | + |
| 25 | +/** @public */ |
| 26 | +export class RunCommandCursor extends AbstractCursor { |
| 27 | + public readonly command: Readonly<Record<string, any>>; |
| 28 | + public readonly getMoreOptions: { |
| 29 | + comment?: any; |
| 30 | + maxAwaitTimeMS?: number; |
| 31 | + batchSize?: number; |
| 32 | + } = {}; |
| 33 | + |
| 34 | + /** |
| 35 | + * Controls the `getMore.comment` field |
| 36 | + * @param comment - any BSON value |
| 37 | + */ |
| 38 | + public setComment(comment: any): this { |
| 39 | + this.getMoreOptions.comment = comment; |
| 40 | + return this; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Controls the `getMore.maxTimeMS` field. Only valid when cursor is tailable await |
| 45 | + * @param maxTimeMS - the number of milliseconds to wait for new data |
| 46 | + */ |
| 47 | + public setMaxTimeMS(maxTimeMS: number): this { |
| 48 | + this.getMoreOptions.maxAwaitTimeMS = maxTimeMS; |
| 49 | + return this; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Controls the `getMore.batchSize` field |
| 54 | + * @param maxTimeMS - the number documents to return in the `nextBatch` |
| 55 | + */ |
| 56 | + public setBatchSize(batchSize: number): this { |
| 57 | + this.getMoreOptions.batchSize = batchSize; |
| 58 | + return this; |
| 59 | + } |
| 60 | + |
| 61 | + /** Unsupported for RunCommandCursor */ |
| 62 | + public override clone(): never { |
| 63 | + throw new MongoAPIError('Clone not supported, create a new cursor with db.runCursorCommand'); |
| 64 | + } |
| 65 | + |
| 66 | + /** Unsupported for RunCommandCursor: readConcern must be configured directly on command document */ |
| 67 | + public override withReadConcern(_: ReadConcernLike): never { |
| 68 | + throw new MongoAPIError( |
| 69 | + 'RunCommandCursor does not support readConcern it must be attached to the command being run' |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + /** Unsupported for RunCommandCursor: various cursor flags must be configured directly on command document */ |
| 74 | + public override addCursorFlag(_: string, __: boolean): never { |
| 75 | + throw new MongoAPIError( |
| 76 | + 'RunCommandCursor does not support cursor flags, they must be attached to the command being run' |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + /** Unsupported for RunCommandCursor: maxTimeMS must be configured directly on command document */ |
| 81 | + public override maxTimeMS(_: number): never { |
| 82 | + throw new MongoAPIError( |
| 83 | + 'maxTimeMS must be configured on the command document directly, to configure getMore.maxTimeMS use cursor.setMaxTimeMS()' |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + /** Unsupported for RunCommandCursor: batchSize must be configured directly on command document */ |
| 88 | + public override batchSize(_: number): never { |
| 89 | + throw new MongoAPIError( |
| 90 | + 'batchSize must be configured on the command document directly, to configure getMore.batchSize use cursor.setBatchSize()' |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + /** @internal */ |
| 95 | + private db: Db; |
| 96 | + |
| 97 | + /** @internal */ |
| 98 | + constructor(db: Db, command: Document, options: RunCursorCommandOptions = {}) { |
| 99 | + super(db.s.client, ns(db.namespace), options); |
| 100 | + this.db = db; |
| 101 | + this.command = Object.freeze({ ...command }); |
| 102 | + } |
| 103 | + |
| 104 | + /** @internal */ |
| 105 | + protected _initialize(session: ClientSession, callback: Callback<ExecutionResult>) { |
| 106 | + const operation = new RunCommandOperation<RunCursorCommandResponse>(this.db, this.command, { |
| 107 | + ...this.cursorOptions, |
| 108 | + session: session, |
| 109 | + readPreference: this.cursorOptions.readPreference |
| 110 | + }); |
| 111 | + executeOperation(this.client, operation).then( |
| 112 | + response => { |
| 113 | + if (response.cursor == null) { |
| 114 | + callback( |
| 115 | + new MongoUnexpectedServerResponseError('Expected server to respond with cursor') |
| 116 | + ); |
| 117 | + return; |
| 118 | + } |
| 119 | + callback(undefined, { |
| 120 | + server: operation.server, |
| 121 | + session, |
| 122 | + response |
| 123 | + }); |
| 124 | + }, |
| 125 | + err => callback(err) |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + /** @internal */ |
| 130 | + override _getMore(_batchSize: number, callback: Callback<Document>) { |
| 131 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 132 | + const getMoreOperation = new GetMoreOperation(this.namespace, this.id!, this.server!, { |
| 133 | + ...this.cursorOptions, |
| 134 | + session: this.session, |
| 135 | + ...this.getMoreOptions |
| 136 | + }); |
| 137 | + |
| 138 | + executeOperation(this.client, getMoreOperation, callback); |
| 139 | + } |
| 140 | +} |
0 commit comments