opusdev/vector-similarity-api
1
1import { type Connection } from '..';2import type { Document } from '../bson';3import { MongoDBResponse } from '../cmap/wire_protocol/responses';4import type { Collection } from '../collection';5import type { ClientSession } from '../sessions';6import { CommandOperation, type CommandOperationOptions } from './command';7import { Aspect, defineAspects } from './operation';8 9/** @public */10export interface EstimatedDocumentCountOptions extends CommandOperationOptions {11 /**12 * The maximum amount of time to allow the operation to run.13 *14 * This option is sent only if the caller explicitly provides a value. The default is to not send a value.15 */16 maxTimeMS?: number;17}18 19/** @internal */20export class EstimatedDocumentCountOperation extends CommandOperation<number> {21 override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;22 override options: EstimatedDocumentCountOptions;23 collectionName: string;24 25 constructor(collection: Collection, options: EstimatedDocumentCountOptions = {}) {26 super(collection, options);27 this.options = options;28 this.collectionName = collection.collectionName;29 }30 31 override get commandName() {32 return 'count' as const;33 }34 35 override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document {36 const cmd: Document = { count: this.collectionName };37 38 if (typeof this.options.maxTimeMS === 'number') {39 cmd.maxTimeMS = this.options.maxTimeMS;40 }41 42 // we check for undefined specifically here to allow falsy values43 // eslint-disable-next-line no-restricted-syntax44 if (this.options.comment !== undefined) {45 cmd.comment = this.options.comment;46 }47 48 return cmd;49 }50 51 override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): number {52 return response.getNumber('n') ?? 0;53 }54}55 56defineAspects(EstimatedDocumentCountOperation, [57 Aspect.READ_OPERATION,58 Aspect.RETRYABLE,59 Aspect.CURSOR_CREATING,60 Aspect.SUPPORTS_RAW_DATA61]);62 