admin.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. 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. * ```js
  20. * const MongoClient = require('mongodb').MongoClient;
  21. * const test = require('assert');
  22. * // Connection url
  23. * const url = 'mongodb://localhost:27017';
  24. * // Database Name
  25. * const dbName = 'test';
  26. *
  27. * // Connect using MongoClient
  28. * MongoClient.connect(url, function(err, client) {
  29. * // Use the admin database for the operation
  30. * const adminDb = client.db(dbName).admin();
  31. *
  32. * // List all the available databases
  33. * adminDb.listDatabases(function(err, dbs) {
  34. * expect(err).to.not.exist;
  35. * test.ok(dbs.databases.length > 0);
  36. * client.close();
  37. * });
  38. * });
  39. * ```
  40. */
  41. class Admin {
  42. /**
  43. * Create a new Admin instance
  44. * @internal
  45. */
  46. constructor(db) {
  47. this.s = { db };
  48. }
  49. command(command, options, callback) {
  50. if (typeof options === 'function')
  51. (callback = options), (options = {});
  52. options = Object.assign({ dbName: 'admin' }, options);
  53. return (0, execute_operation_1.executeOperation)((0, utils_1.getTopology)(this.s.db), new run_command_1.RunCommandOperation(this.s.db, command, options), callback);
  54. }
  55. buildInfo(options, callback) {
  56. if (typeof options === 'function')
  57. (callback = options), (options = {});
  58. options = options !== null && options !== void 0 ? options : {};
  59. return this.command({ buildinfo: 1 }, options, callback);
  60. }
  61. serverInfo(options, callback) {
  62. if (typeof options === 'function')
  63. (callback = options), (options = {});
  64. options = options !== null && options !== void 0 ? options : {};
  65. return this.command({ buildinfo: 1 }, options, callback);
  66. }
  67. serverStatus(options, callback) {
  68. if (typeof options === 'function')
  69. (callback = options), (options = {});
  70. options = options !== null && options !== void 0 ? options : {};
  71. return this.command({ serverStatus: 1 }, options, callback);
  72. }
  73. ping(options, callback) {
  74. if (typeof options === 'function')
  75. (callback = options), (options = {});
  76. options = options !== null && options !== void 0 ? options : {};
  77. return this.command({ ping: 1 }, options, callback);
  78. }
  79. addUser(username, password, options, callback) {
  80. if (typeof password === 'function') {
  81. (callback = password), (password = undefined), (options = {});
  82. }
  83. else if (typeof password !== 'string') {
  84. if (typeof options === 'function') {
  85. (callback = options), (options = password), (password = undefined);
  86. }
  87. else {
  88. (options = password), (callback = undefined), (password = undefined);
  89. }
  90. }
  91. else {
  92. if (typeof options === 'function')
  93. (callback = options), (options = {});
  94. }
  95. options = Object.assign({ dbName: 'admin' }, options);
  96. return (0, execute_operation_1.executeOperation)((0, utils_1.getTopology)(this.s.db), new add_user_1.AddUserOperation(this.s.db, username, password, options), callback);
  97. }
  98. removeUser(username, options, callback) {
  99. if (typeof options === 'function')
  100. (callback = options), (options = {});
  101. options = Object.assign({ dbName: 'admin' }, options);
  102. return (0, execute_operation_1.executeOperation)((0, utils_1.getTopology)(this.s.db), new remove_user_1.RemoveUserOperation(this.s.db, username, options), callback);
  103. }
  104. validateCollection(collectionName, options, callback) {
  105. if (typeof options === 'function')
  106. (callback = options), (options = {});
  107. options = options !== null && options !== void 0 ? options : {};
  108. return (0, execute_operation_1.executeOperation)((0, utils_1.getTopology)(this.s.db), new validate_collection_1.ValidateCollectionOperation(this, collectionName, options), callback);
  109. }
  110. listDatabases(options, callback) {
  111. if (typeof options === 'function')
  112. (callback = options), (options = {});
  113. options = options !== null && options !== void 0 ? options : {};
  114. return (0, execute_operation_1.executeOperation)((0, utils_1.getTopology)(this.s.db), new list_databases_1.ListDatabasesOperation(this.s.db, options), callback);
  115. }
  116. replSetGetStatus(options, callback) {
  117. if (typeof options === 'function')
  118. (callback = options), (options = {});
  119. options = options !== null && options !== void 0 ? options : {};
  120. return this.command({ replSetGetStatus: 1 }, options, callback);
  121. }
  122. }
  123. exports.Admin = Admin;
  124. //# sourceMappingURL=admin.js.map