admin.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import type { Document } from './bson';
  2. import type { Db } from './db';
  3. import { AddUserOperation, type AddUserOptions } from './operations/add_user';
  4. import type { CommandOperationOptions } from './operations/command';
  5. import { executeOperation } from './operations/execute_operation';
  6. import {
  7. ListDatabasesOperation,
  8. type ListDatabasesOptions,
  9. type ListDatabasesResult
  10. } from './operations/list_databases';
  11. import { RemoveUserOperation, type RemoveUserOptions } from './operations/remove_user';
  12. import { RunCommandOperation, type RunCommandOptions } from './operations/run_command';
  13. import {
  14. ValidateCollectionOperation,
  15. type ValidateCollectionOptions
  16. } from './operations/validate_collection';
  17. /** @internal */
  18. export interface AdminPrivate {
  19. db: Db;
  20. }
  21. /**
  22. * The **Admin** class is an internal class that allows convenient access to
  23. * the admin functionality and commands for MongoDB.
  24. *
  25. * **ADMIN Cannot directly be instantiated**
  26. * @public
  27. *
  28. * @example
  29. * ```ts
  30. * import { MongoClient } from 'mongodb';
  31. *
  32. * const client = new MongoClient('mongodb://localhost:27017');
  33. * const admin = client.db().admin();
  34. * const dbInfo = await admin.listDatabases();
  35. * for (const db of dbInfo.databases) {
  36. * console.log(db.name);
  37. * }
  38. * ```
  39. */
  40. export class Admin {
  41. /** @internal */
  42. s: AdminPrivate;
  43. /**
  44. * Create a new Admin instance
  45. * @internal
  46. */
  47. constructor(db: Db) {
  48. this.s = { db };
  49. }
  50. /**
  51. * Execute a command
  52. *
  53. * The driver will ensure the following fields are attached to the command sent to the server:
  54. * - `lsid` - sourced from an implicit session or options.session
  55. * - `$readPreference` - defaults to primary or can be configured by options.readPreference
  56. * - `$db` - sourced from the name of this database
  57. *
  58. * If the client has a serverApi setting:
  59. * - `apiVersion`
  60. * - `apiStrict`
  61. * - `apiDeprecationErrors`
  62. *
  63. * When in a transaction:
  64. * - `readConcern` - sourced from readConcern set on the TransactionOptions
  65. * - `writeConcern` - sourced from writeConcern set on the TransactionOptions
  66. *
  67. * Attaching any of the above fields to the command will have no effect as the driver will overwrite the value.
  68. *
  69. * @param command - The command to execute
  70. * @param options - Optional settings for the command
  71. */
  72. async command(command: Document, options?: RunCommandOptions): Promise<Document> {
  73. return executeOperation(
  74. this.s.db.client,
  75. new RunCommandOperation(this.s.db, command, { dbName: 'admin', ...options })
  76. );
  77. }
  78. /**
  79. * Retrieve the server build information
  80. *
  81. * @param options - Optional settings for the command
  82. */
  83. async buildInfo(options?: CommandOperationOptions): Promise<Document> {
  84. return this.command({ buildinfo: 1 }, options);
  85. }
  86. /**
  87. * Retrieve the server build information
  88. *
  89. * @param options - Optional settings for the command
  90. */
  91. async serverInfo(options?: CommandOperationOptions): Promise<Document> {
  92. return this.command({ buildinfo: 1 }, options);
  93. }
  94. /**
  95. * Retrieve this db's server status.
  96. *
  97. * @param options - Optional settings for the command
  98. */
  99. async serverStatus(options?: CommandOperationOptions): Promise<Document> {
  100. return this.command({ serverStatus: 1 }, options);
  101. }
  102. /**
  103. * Ping the MongoDB server and retrieve results
  104. *
  105. * @param options - Optional settings for the command
  106. */
  107. async ping(options?: CommandOperationOptions): Promise<Document> {
  108. return this.command({ ping: 1 }, options);
  109. }
  110. /**
  111. * Add a user to the database
  112. *
  113. * @param username - The username for the new user
  114. * @param passwordOrOptions - An optional password for the new user, or the options for the command
  115. * @param options - Optional settings for the command
  116. * @deprecated Use the createUser command in `db.command()` instead.
  117. * @see https://www.mongodb.com/docs/manual/reference/command/createUser/
  118. */
  119. async addUser(
  120. username: string,
  121. passwordOrOptions?: string | AddUserOptions,
  122. options?: AddUserOptions
  123. ): Promise<Document> {
  124. options =
  125. options != null && typeof options === 'object'
  126. ? options
  127. : passwordOrOptions != null && typeof passwordOrOptions === 'object'
  128. ? passwordOrOptions
  129. : undefined;
  130. const password = typeof passwordOrOptions === 'string' ? passwordOrOptions : undefined;
  131. return executeOperation(
  132. this.s.db.client,
  133. new AddUserOperation(this.s.db, username, password, { dbName: 'admin', ...options })
  134. );
  135. }
  136. /**
  137. * Remove a user from a database
  138. *
  139. * @param username - The username to remove
  140. * @param options - Optional settings for the command
  141. */
  142. async removeUser(username: string, options?: RemoveUserOptions): Promise<boolean> {
  143. return executeOperation(
  144. this.s.db.client,
  145. new RemoveUserOperation(this.s.db, username, { dbName: 'admin', ...options })
  146. );
  147. }
  148. /**
  149. * Validate an existing collection
  150. *
  151. * @param collectionName - The name of the collection to validate.
  152. * @param options - Optional settings for the command
  153. */
  154. async validateCollection(
  155. collectionName: string,
  156. options: ValidateCollectionOptions = {}
  157. ): Promise<Document> {
  158. return executeOperation(
  159. this.s.db.client,
  160. new ValidateCollectionOperation(this, collectionName, options)
  161. );
  162. }
  163. /**
  164. * List the available databases
  165. *
  166. * @param options - Optional settings for the command
  167. */
  168. async listDatabases(options?: ListDatabasesOptions): Promise<ListDatabasesResult> {
  169. return executeOperation(this.s.db.client, new ListDatabasesOperation(this.s.db, options));
  170. }
  171. /**
  172. * Get ReplicaSet status
  173. *
  174. * @param options - Optional settings for the command
  175. */
  176. async replSetGetStatus(options?: CommandOperationOptions): Promise<Document> {
  177. return this.command({ replSetGetStatus: 1 }, options);
  178. }
  179. }