admin.js 5.1 KB

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