import type { Document } from './bson'; import type { Db } from './db'; import { AddUserOperation, AddUserOptions } from './operations/add_user'; import type { CommandOperationOptions } from './operations/command'; import { executeOperation } from './operations/execute_operation'; import { ListDatabasesOperation, ListDatabasesOptions, ListDatabasesResult } from './operations/list_databases'; import { RemoveUserOperation, RemoveUserOptions } from './operations/remove_user'; import { RunCommandOperation, RunCommandOptions } from './operations/run_command'; import { ValidateCollectionOperation, ValidateCollectionOptions } from './operations/validate_collection'; import type { Callback } from './utils'; /** @internal */ export interface AdminPrivate { db: Db; } /** * The **Admin** class is an internal class that allows convenient access to * the admin functionality and commands for MongoDB. * * **ADMIN Cannot directly be instantiated** * @public * * @example * ```ts * import { MongoClient } from 'mongodb'; * * const client = new MongoClient('mongodb://localhost:27017'); * const admin = client.db().admin(); * const dbInfo = await admin.listDatabases(); * for (const db of dbInfo.databases) { * console.log(db.name); * } * ``` */ export class Admin { /** @internal */ s: AdminPrivate; /** * Create a new Admin instance * @internal */ constructor(db: Db) { this.s = { db }; } /** * Execute a command * * @param command - The command to execute * @param options - Optional settings for the command * @param callback - An optional callback, a Promise will be returned if none is provided */ command(command: Document): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ command(command: Document, callback: Callback): void; command(command: Document, options: RunCommandOptions): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ command(command: Document, options: RunCommandOptions, callback: Callback): void; command( command: Document, options?: RunCommandOptions | Callback, callback?: Callback ): Promise | void { if (typeof options === 'function') (callback = options), (options = {}); options = Object.assign({ dbName: 'admin' }, options); return executeOperation( this.s.db.s.client, new RunCommandOperation(this.s.db, command, options), callback ); } /** * Retrieve the server build information * * @param options - Optional settings for the command * @param callback - An optional callback, a Promise will be returned if none is provided */ buildInfo(): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ buildInfo(callback: Callback): void; buildInfo(options: CommandOperationOptions): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ buildInfo(options: CommandOperationOptions, callback: Callback): void; buildInfo( options?: CommandOperationOptions | Callback, callback?: Callback ): Promise | void { if (typeof options === 'function') (callback = options), (options = {}); options = options ?? {}; return this.command({ buildinfo: 1 }, options, callback as Callback); } /** * Retrieve the server build information * * @param options - Optional settings for the command * @param callback - An optional callback, a Promise will be returned if none is provided */ serverInfo(): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ serverInfo(callback: Callback): void; serverInfo(options: CommandOperationOptions): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ serverInfo(options: CommandOperationOptions, callback: Callback): void; serverInfo( options?: CommandOperationOptions | Callback, callback?: Callback ): Promise | void { if (typeof options === 'function') (callback = options), (options = {}); options = options ?? {}; return this.command({ buildinfo: 1 }, options, callback as Callback); } /** * Retrieve this db's server status. * * @param options - Optional settings for the command * @param callback - An optional callback, a Promise will be returned if none is provided */ serverStatus(): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ serverStatus(callback: Callback): void; serverStatus(options: CommandOperationOptions): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ serverStatus(options: CommandOperationOptions, callback: Callback): void; serverStatus( options?: CommandOperationOptions | Callback, callback?: Callback ): Promise | void { if (typeof options === 'function') (callback = options), (options = {}); options = options ?? {}; return this.command({ serverStatus: 1 }, options, callback as Callback); } /** * Ping the MongoDB server and retrieve results * * @param options - Optional settings for the command * @param callback - An optional callback, a Promise will be returned if none is provided */ ping(): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ ping(callback: Callback): void; ping(options: CommandOperationOptions): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ ping(options: CommandOperationOptions, callback: Callback): void; ping( options?: CommandOperationOptions | Callback, callback?: Callback ): Promise | void { if (typeof options === 'function') (callback = options), (options = {}); options = options ?? {}; return this.command({ ping: 1 }, options, callback as Callback); } /** * Add a user to the database * * @param username - The username for the new user * @param password - An optional password for the new user * @param options - Optional settings for the command * @param callback - An optional callback, a Promise will be returned if none is provided */ addUser(username: string): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ addUser(username: string, callback: Callback): void; addUser(username: string, password: string): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ addUser(username: string, password: string, callback: Callback): void; addUser(username: string, options: AddUserOptions): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ addUser(username: string, options: AddUserOptions, callback: Callback): void; addUser(username: string, password: string, options: AddUserOptions): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ addUser( username: string, password: string, options: AddUserOptions, callback: Callback ): void; addUser( username: string, password?: string | AddUserOptions | Callback, options?: AddUserOptions | Callback, callback?: Callback ): Promise | void { if (typeof password === 'function') { (callback = password), (password = undefined), (options = {}); } else if (typeof password !== 'string') { if (typeof options === 'function') { (callback = options), (options = password), (password = undefined); } else { (options = password), (callback = undefined), (password = undefined); } } else { if (typeof options === 'function') (callback = options), (options = {}); } options = Object.assign({ dbName: 'admin' }, options); return executeOperation( this.s.db.s.client, new AddUserOperation(this.s.db, username, password, options), callback ); } /** * Remove a user from a database * * @param username - The username to remove * @param options - Optional settings for the command * @param callback - An optional callback, a Promise will be returned if none is provided */ removeUser(username: string): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ removeUser(username: string, callback: Callback): void; removeUser(username: string, options: RemoveUserOptions): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ removeUser(username: string, options: RemoveUserOptions, callback: Callback): void; removeUser( username: string, options?: RemoveUserOptions | Callback, callback?: Callback ): Promise | void { if (typeof options === 'function') (callback = options), (options = {}); options = Object.assign({ dbName: 'admin' }, options); return executeOperation( this.s.db.s.client, new RemoveUserOperation(this.s.db, username, options), callback ); } /** * Validate an existing collection * * @param collectionName - The name of the collection to validate. * @param options - Optional settings for the command * @param callback - An optional callback, a Promise will be returned if none is provided */ validateCollection(collectionName: string): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ validateCollection(collectionName: string, callback: Callback): void; validateCollection(collectionName: string, options: ValidateCollectionOptions): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ validateCollection( collectionName: string, options: ValidateCollectionOptions, callback: Callback ): void; validateCollection( collectionName: string, options?: ValidateCollectionOptions | Callback, callback?: Callback ): Promise | void { if (typeof options === 'function') (callback = options), (options = {}); options = options ?? {}; return executeOperation( this.s.db.s.client, new ValidateCollectionOperation(this, collectionName, options), callback ); } /** * List the available databases * * @param options - Optional settings for the command * @param callback - An optional callback, a Promise will be returned if none is provided */ listDatabases(): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ listDatabases(callback: Callback): void; listDatabases(options: ListDatabasesOptions): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ listDatabases(options: ListDatabasesOptions, callback: Callback): void; listDatabases( options?: ListDatabasesOptions | Callback, callback?: Callback ): Promise | void { if (typeof options === 'function') (callback = options), (options = {}); options = options ?? {}; return executeOperation( this.s.db.s.client, new ListDatabasesOperation(this.s.db, options), callback ); } /** * Get ReplicaSet status * * @param options - Optional settings for the command * @param callback - An optional callback, a Promise will be returned if none is provided */ replSetGetStatus(): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ replSetGetStatus(callback: Callback): void; replSetGetStatus(options: CommandOperationOptions): Promise; /** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */ replSetGetStatus(options: CommandOperationOptions, callback: Callback): void; replSetGetStatus( options?: CommandOperationOptions | Callback, callback?: Callback ): Promise | void { if (typeof options === 'function') (callback = options), (options = {}); options = options ?? {}; return this.command({ replSetGetStatus: 1 }, options, callback as Callback); } }