opusdev/vector-similarity-api
1
1import { type Document, resolveBSONOptions } from './bson';2import type { Db } from './db';3import type { CommandOperationOptions } from './operations/command';4import { executeOperation } from './operations/execute_operation';5import {6 ListDatabasesOperation,7 type ListDatabasesOptions,8 type ListDatabasesResult9} from './operations/list_databases';10import { RemoveUserOperation, type RemoveUserOptions } from './operations/remove_user';11import { RunCommandOperation, type RunCommandOptions } from './operations/run_command';12import {13 ValidateCollectionOperation,14 type ValidateCollectionOptions15} from './operations/validate_collection';16import { MongoDBNamespace } from './utils';17 18/** @internal */19export interface AdminPrivate {20 db: Db;21}22 23/**24 * The **Admin** class is an internal class that allows convenient access to25 * the admin functionality and commands for MongoDB.26 *27 * **ADMIN Cannot directly be instantiated**28 * @public29 *30 * @example31 * ```ts32 * import { MongoClient } from 'mongodb';33 *34 * const client = new MongoClient('mongodb://localhost:27017');35 * const admin = client.db().admin();36 * const dbInfo = await admin.listDatabases();37 * for (const db of dbInfo.databases) {38 * console.log(db.name);39 * }40 * ```41 */42export class Admin {43 /** @internal */44 s: AdminPrivate;45 46 /**47 * Create a new Admin instance48 * @internal49 */50 constructor(db: Db) {51 this.s = { db };52 }53 54 /**55 * Execute a command56 *57 * The driver will ensure the following fields are attached to the command sent to the server:58 * - `lsid` - sourced from an implicit session or options.session59 * - `$readPreference` - defaults to primary or can be configured by options.readPreference60 * - `$db` - sourced from the name of this database61 *62 * If the client has a serverApi setting:63 * - `apiVersion`64 * - `apiStrict`65 * - `apiDeprecationErrors`66 *67 * When in a transaction:68 * - `readConcern` - sourced from readConcern set on the TransactionOptions69 * - `writeConcern` - sourced from writeConcern set on the TransactionOptions70 *71 * Attaching any of the above fields to the command will have no effect as the driver will overwrite the value.72 *73 * @param command - The command to execute74 * @param options - Optional settings for the command75 */76 async command(command: Document, options?: RunCommandOptions): Promise<Document> {77 return await executeOperation(78 this.s.db.client,79 new RunCommandOperation(new MongoDBNamespace('admin'), command, {80 ...resolveBSONOptions(options),81 session: options?.session,82 readPreference: options?.readPreference,83 timeoutMS: options?.timeoutMS ?? this.s.db.timeoutMS84 })85 );86 }87 88 /**89 * Retrieve the server build information90 *91 * @param options - Optional settings for the command92 */93 async buildInfo(options?: CommandOperationOptions): Promise<Document> {94 return await this.command({ buildinfo: 1 }, options);95 }96 97 /**98 * Retrieve the server build information99 *100 * @param options - Optional settings for the command101 */102 async serverInfo(options?: CommandOperationOptions): Promise<Document> {103 return await this.command({ buildinfo: 1 }, options);104 }105 106 /**107 * Retrieve this db's server status.108 *109 * @param options - Optional settings for the command110 */111 async serverStatus(options?: CommandOperationOptions): Promise<Document> {112 return await this.command({ serverStatus: 1 }, options);113 }114 115 /**116 * Ping the MongoDB server and retrieve results117 *118 * @param options - Optional settings for the command119 */120 async ping(options?: CommandOperationOptions): Promise<Document> {121 return await this.command({ ping: 1 }, options);122 }123 124 /**125 * Remove a user from a database126 *127 * @param username - The username to remove128 * @param options - Optional settings for the command129 */130 async removeUser(username: string, options?: RemoveUserOptions): Promise<boolean> {131 return await executeOperation(132 this.s.db.client,133 new RemoveUserOperation(this.s.db, username, { dbName: 'admin', ...options })134 );135 }136 137 /**138 * Validate an existing collection139 *140 * @param collectionName - The name of the collection to validate.141 * @param options - Optional settings for the command142 */143 async validateCollection(144 collectionName: string,145 options: ValidateCollectionOptions = {}146 ): Promise<Document> {147 return await executeOperation(148 this.s.db.client,149 new ValidateCollectionOperation(this, collectionName, options)150 );151 }152 153 /**154 * List the available databases155 *156 * @param options - Optional settings for the command157 */158 async listDatabases(options?: ListDatabasesOptions): Promise<ListDatabasesResult> {159 return await executeOperation(160 this.s.db.client,161 new ListDatabasesOperation(this.s.db, { timeoutMS: this.s.db.timeoutMS, ...options })162 );163 }164 165 /**166 * Get ReplicaSet status167 *168 * @param options - Optional settings for the command169 */170 async replSetGetStatus(options?: CommandOperationOptions): Promise<Document> {171 return await this.command({ replSetGetStatus: 1 }, options);172 }173}174 