CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
list_collections.ts109 linesDownload Raw Back to operations
1import { type Connection } from '..';2import type { Binary, Document } from '../bson';3import { CursorResponse, ExplainedCursorResponse } from '../cmap/wire_protocol/responses';4import { type CursorTimeoutContext, type CursorTimeoutMode } from '../cursor/abstract_cursor';5import type { Db } from '../db';6import { type Abortable } from '../mongo_types';7import { maxWireVersion } from '../utils';8import { CommandOperation, type CommandOperationOptions } from './command';9import { Aspect, defineAspects } from './operation';10 11/** @public */12export interface ListCollectionsOptions13  extends Omit<CommandOperationOptions, 'writeConcern'>,14    Abortable {15  /** Since 4.0: If true, will only return the collection name in the response, and will omit additional info */16  nameOnly?: boolean;17  /** Since 4.0: If true and nameOnly is true, allows a user without the required privilege (i.e. listCollections action on the database) to run the command when access control is enforced. */18  authorizedCollections?: boolean;19  /** The batchSize for the returned command cursor or if pre 2.8 the systems batch collection */20  batchSize?: number;21  /** @internal */22  timeoutMode?: CursorTimeoutMode;23 24  /** @internal */25  timeoutContext?: CursorTimeoutContext;26}27 28/** @internal */29export class ListCollectionsOperation extends CommandOperation<CursorResponse> {30  override SERVER_COMMAND_RESPONSE_TYPE = CursorResponse;31  /**32   * @remarks WriteConcern can still be present on the options because33   * we inherit options from the client/db/collection.  The34   * key must be present on the options in order to delete it.35   * This allows typescript to delete the key but will36   * not allow a writeConcern to be assigned as a property on options.37   */38  override options: ListCollectionsOptions & { writeConcern?: never };39  db: Db;40  filter: Document;41  nameOnly: boolean;42  authorizedCollections: boolean;43  batchSize?: number;44 45  constructor(db: Db, filter: Document, options?: ListCollectionsOptions) {46    super(db, options);47 48    this.options = { ...options };49    delete this.options.writeConcern;50    this.db = db;51    this.filter = filter;52    this.nameOnly = !!this.options.nameOnly;53    this.authorizedCollections = !!this.options.authorizedCollections;54 55    if (typeof this.options.batchSize === 'number') {56      this.batchSize = this.options.batchSize;57    }58 59    this.SERVER_COMMAND_RESPONSE_TYPE = this.explain ? ExplainedCursorResponse : CursorResponse;60  }61 62  override get commandName() {63    return 'listCollections' as const;64  }65 66  override buildCommandDocument(connection: Connection): Document {67    const command: Document = {68      listCollections: 1,69      filter: this.filter,70      cursor: this.batchSize ? { batchSize: this.batchSize } : {},71      nameOnly: this.nameOnly,72      authorizedCollections: this.authorizedCollections73    };74 75    // we check for undefined specifically here to allow falsy values76    // eslint-disable-next-line no-restricted-syntax77    if (maxWireVersion(connection) >= 9 && this.options.comment !== undefined) {78      command.comment = this.options.comment;79    }80 81    return command;82  }83 84  override handleOk(85    response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>86  ): CursorResponse {87    return response;88  }89}90 91/** @public */92export interface CollectionInfo extends Document {93  name: string;94  type?: string;95  options?: Document;96  info?: {97    readOnly?: false;98    uuid?: Binary;99  };100  idIndex?: Document;101}102 103defineAspects(ListCollectionsOperation, [104  Aspect.READ_OPERATION,105  Aspect.RETRYABLE,106  Aspect.CURSOR_CREATING,107  Aspect.SUPPORTS_RAW_DATA108]);109