admin.js 5.4 KB

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