admin.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. const utils_1 = require("./utils");
  11. /**
  12. * The **Admin** class is an internal class that allows convenient access to
  13. * the admin functionality and commands for MongoDB.
  14. *
  15. * **ADMIN Cannot directly be instantiated**
  16. * @public
  17. *
  18. * @example
  19. * ```ts
  20. * import { MongoClient } from 'mongodb';
  21. *
  22. * const client = new MongoClient('mongodb://localhost:27017');
  23. * const admin = client.db().admin();
  24. * const dbInfo = await admin.listDatabases();
  25. * for (const db of dbInfo.databases) {
  26. * console.log(db.name);
  27. * }
  28. * ```
  29. */
  30. class Admin {
  31. /**
  32. * Create a new Admin instance
  33. * @internal
  34. */
  35. constructor(db) {
  36. this.s = { db };
  37. }
  38. /**
  39. * Execute a command
  40. *
  41. * The driver will ensure the following fields are attached to the command sent to the server:
  42. * - `lsid` - sourced from an implicit session or options.session
  43. * - `$readPreference` - defaults to primary or can be configured by options.readPreference
  44. * - `$db` - sourced from the name of this database
  45. *
  46. * If the client has a serverApi setting:
  47. * - `apiVersion`
  48. * - `apiStrict`
  49. * - `apiDeprecationErrors`
  50. *
  51. * When in a transaction:
  52. * - `readConcern` - sourced from readConcern set on the TransactionOptions
  53. * - `writeConcern` - sourced from writeConcern set on the TransactionOptions
  54. *
  55. * Attaching any of the above fields to the command will have no effect as the driver will overwrite the value.
  56. *
  57. * @param command - The command to execute
  58. * @param options - Optional settings for the command
  59. */
  60. async command(command, options) {
  61. return await (0, execute_operation_1.executeOperation)(this.s.db.client, new run_command_1.RunCommandOperation(new utils_1.MongoDBNamespace('admin'), command, {
  62. ...(0, bson_1.resolveBSONOptions)(options),
  63. session: options?.session,
  64. readPreference: options?.readPreference,
  65. timeoutMS: options?.timeoutMS ?? this.s.db.timeoutMS
  66. }));
  67. }
  68. /**
  69. * Retrieve the server build information
  70. *
  71. * @param options - Optional settings for the command
  72. */
  73. async buildInfo(options) {
  74. return await this.command({ buildinfo: 1 }, options);
  75. }
  76. /**
  77. * Retrieve the server build information
  78. *
  79. * @param options - Optional settings for the command
  80. */
  81. async serverInfo(options) {
  82. return await this.command({ buildinfo: 1 }, options);
  83. }
  84. /**
  85. * Retrieve this db's server status.
  86. *
  87. * @param options - Optional settings for the command
  88. */
  89. async serverStatus(options) {
  90. return await this.command({ serverStatus: 1 }, options);
  91. }
  92. /**
  93. * Ping the MongoDB server and retrieve results
  94. *
  95. * @param options - Optional settings for the command
  96. */
  97. async ping(options) {
  98. return await this.command({ ping: 1 }, options);
  99. }
  100. /**
  101. * Remove a user from a database
  102. *
  103. * @param username - The username to remove
  104. * @param options - Optional settings for the command
  105. */
  106. async removeUser(username, options) {
  107. return await (0, execute_operation_1.executeOperation)(this.s.db.client, new remove_user_1.RemoveUserOperation(this.s.db, username, { dbName: 'admin', ...options }));
  108. }
  109. /**
  110. * Validate an existing collection
  111. *
  112. * @param collectionName - The name of the collection to validate.
  113. * @param options - Optional settings for the command
  114. */
  115. async validateCollection(collectionName, options = {}) {
  116. return await (0, execute_operation_1.executeOperation)(this.s.db.client, new validate_collection_1.ValidateCollectionOperation(this, collectionName, options));
  117. }
  118. /**
  119. * List the available databases
  120. *
  121. * @param options - Optional settings for the command
  122. */
  123. async listDatabases(options) {
  124. return await (0, execute_operation_1.executeOperation)(this.s.db.client, new list_databases_1.ListDatabasesOperation(this.s.db, { timeoutMS: this.s.db.timeoutMS, ...options }));
  125. }
  126. /**
  127. * Get ReplicaSet status
  128. *
  129. * @param options - Optional settings for the command
  130. */
  131. async replSetGetStatus(options) {
  132. return await this.command({ replSetGetStatus: 1 }, options);
  133. }
  134. }
  135. exports.Admin = Admin;
  136. //# sourceMappingURL=admin.js.map