CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
validate_collection.ts51 linesDownload Raw Back to operations
1import { type Connection } from '..';2import type { Admin } from '../admin';3import { type Document } from '../bson';4import { MongoDBResponse } from '../cmap/wire_protocol/responses';5import { MongoUnexpectedServerResponseError } from '../error';6import type { ClientSession } from '../sessions';7import { CommandOperation, type CommandOperationOptions } from './command';8 9/** @public */10export interface ValidateCollectionOptions extends Omit<CommandOperationOptions, 'rawData'> {11  /** Validates a collection in the background, without interrupting read or write traffic (only in MongoDB 4.4+) */12  background?: boolean;13}14 15/** @internal */16export class ValidateCollectionOperation extends CommandOperation<Document> {17  override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;18  override options: ValidateCollectionOptions;19  collectionName: string;20 21  constructor(admin: Admin, collectionName: string, options: ValidateCollectionOptions) {22    super(admin.s.db, options);23    this.options = options;24    this.collectionName = collectionName;25  }26 27  override get commandName() {28    return 'validate' as const;29  }30 31  override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document {32    // Decorate command with extra options33    return {34      validate: this.collectionName,35      ...Object.fromEntries(Object.entries(this.options).filter(entry => entry[0] !== 'session'))36    };37  }38 39  override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): Document {40    const result = super.handleOk(response);41    if (result.result != null && typeof result.result !== 'string')42      throw new MongoUnexpectedServerResponseError('Error with validation data');43    if (result.result != null && result.result.match(/exception|corrupt/) != null)44      throw new MongoUnexpectedServerResponseError(`Invalid collection ${this.collectionName}`);45    if (result.valid != null && !result.valid)46      throw new MongoUnexpectedServerResponseError(`Invalid collection ${this.collectionName}`);47 48    return response;49  }50}51