opusdev/vector-similarity-api
1
1import { type Connection } from '..';2import type { Document } from '../bson';3import { MongoDBResponse } from '../cmap/wire_protocol/responses';4import type { Db } from '../db';5import type { ClientSession } from '../sessions';6import { maxWireVersion, MongoDBNamespace } from '../utils';7import { CommandOperation, type CommandOperationOptions } from './command';8import { Aspect, defineAspects } from './operation';9 10/** @public */11export interface ListDatabasesResult {12 databases: ({ name: string; sizeOnDisk?: number; empty?: boolean } & Document)[];13 totalSize?: number;14 totalSizeMb?: number;15 ok: 1 | 0;16}17 18/** @public */19export interface ListDatabasesOptions extends Omit<CommandOperationOptions, 'rawData'> {20 /** A query predicate that determines which databases are listed */21 filter?: Document;22 /** A flag to indicate whether the command should return just the database names, or return both database names and size information */23 nameOnly?: boolean;24 /** A flag that determines which databases are returned based on the user privileges when access control is enabled */25 authorizedDatabases?: boolean;26}27 28/** @internal */29export class ListDatabasesOperation extends CommandOperation<ListDatabasesResult> {30 override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;31 override options: ListDatabasesOptions;32 33 constructor(db: Db, options?: ListDatabasesOptions) {34 super(db, options);35 this.options = options ?? {};36 this.ns = new MongoDBNamespace('admin', '$cmd');37 }38 39 override get commandName() {40 return 'listDatabases' as const;41 }42 43 override buildCommandDocument(connection: Connection, _session?: ClientSession): Document {44 const cmd: Document = { listDatabases: 1 };45 46 if (typeof this.options.nameOnly === 'boolean') {47 cmd.nameOnly = this.options.nameOnly;48 }49 50 if (this.options.filter) {51 cmd.filter = this.options.filter;52 }53 54 if (typeof this.options.authorizedDatabases === 'boolean') {55 cmd.authorizedDatabases = this.options.authorizedDatabases;56 }57 58 // we check for undefined specifically here to allow falsy values59 // eslint-disable-next-line no-restricted-syntax60 if (maxWireVersion(connection) >= 9 && this.options.comment !== undefined) {61 cmd.comment = this.options.comment;62 }63 64 return cmd;65 }66}67 68defineAspects(ListDatabasesOperation, [Aspect.READ_OPERATION, Aspect.RETRYABLE]);69 