admin.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Admin = void 0;
  4. const bson_1 = require("./bson");
  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.RunAdminCommandOperation(command, {
  61. ...(0, bson_1.resolveBSONOptions)(options),
  62. session: options?.session,
  63. readPreference: options?.readPreference
  64. }));
  65. }
  66. /**
  67. * Retrieve the server build information
  68. *
  69. * @param options - Optional settings for the command
  70. */
  71. async buildInfo(options) {
  72. return this.command({ buildinfo: 1 }, options);
  73. }
  74. /**
  75. * Retrieve the server build information
  76. *
  77. * @param options - Optional settings for the command
  78. */
  79. async serverInfo(options) {
  80. return this.command({ buildinfo: 1 }, options);
  81. }
  82. /**
  83. * Retrieve this db's server status.
  84. *
  85. * @param options - Optional settings for the command
  86. */
  87. async serverStatus(options) {
  88. return this.command({ serverStatus: 1 }, options);
  89. }
  90. /**
  91. * Ping the MongoDB server and retrieve results
  92. *
  93. * @param options - Optional settings for the command
  94. */
  95. async ping(options) {
  96. return this.command({ ping: 1 }, options);
  97. }
  98. /**
  99. * Remove a user from a database
  100. *
  101. * @param username - The username to remove
  102. * @param options - Optional settings for the command
  103. */
  104. async removeUser(username, options) {
  105. return (0, execute_operation_1.executeOperation)(this.s.db.client, new remove_user_1.RemoveUserOperation(this.s.db, username, { dbName: 'admin', ...options }));
  106. }
  107. /**
  108. * Validate an existing collection
  109. *
  110. * @param collectionName - The name of the collection to validate.
  111. * @param options - Optional settings for the command
  112. */
  113. async validateCollection(collectionName, options = {}) {
  114. return (0, execute_operation_1.executeOperation)(this.s.db.client, new validate_collection_1.ValidateCollectionOperation(this, collectionName, options));
  115. }
  116. /**
  117. * List the available databases
  118. *
  119. * @param options - Optional settings for the command
  120. */
  121. async listDatabases(options) {
  122. return (0, execute_operation_1.executeOperation)(this.s.db.client, new list_databases_1.ListDatabasesOperation(this.s.db, options));
  123. }
  124. /**
  125. * Get ReplicaSet status
  126. *
  127. * @param options - Optional settings for the command
  128. */
  129. async replSetGetStatus(options) {
  130. return this.command({ replSetGetStatus: 1 }, options);
  131. }
  132. }
  133. exports.Admin = Admin;
  134. //# sourceMappingURL=admin.js.map