opusdev/vector-similarity-api
1
1import { BSONType, type Document } from '../bson';2import { type Connection } from '../cmap/connection';3import { MongoDBResponse } from '../cmap/wire_protocol/responses';4import type { Db } from '../db';5import { MongoUnexpectedServerResponseError } from '../error';6import { CommandOperation, type CommandOperationOptions } from './command';7 8/** @public */9export type ProfilingLevelOptions = Omit<CommandOperationOptions, 'rawData'>;10 11class ProfilingLevelResponse extends MongoDBResponse {12 get was() {13 return this.get('was', BSONType.int, true);14 }15}16 17/** @internal */18export class ProfilingLevelOperation extends CommandOperation<string> {19 override SERVER_COMMAND_RESPONSE_TYPE = ProfilingLevelResponse;20 override options: ProfilingLevelOptions;21 22 constructor(db: Db, options: ProfilingLevelOptions) {23 super(db, options);24 this.options = options;25 }26 27 override get commandName() {28 return 'profile' as const;29 }30 31 override buildCommandDocument(_connection: Connection): Document {32 return { profile: -1 };33 }34 35 override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): string {36 if (response.ok === 1) {37 const was = response.was;38 if (was === 0) return 'off';39 if (was === 1) return 'slow_only';40 if (was === 2) return 'all';41 throw new MongoUnexpectedServerResponseError(`Illegal profiling level value ${was}`);42 } else {43 throw new MongoUnexpectedServerResponseError('Error with profile command');44 }45 }46}47 