|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- 'use strict';
-
- const executeOperation = require('./utils').executeOperation;
- const applyWriteConcern = require('./utils').applyWriteConcern;
-
- const addUser = require('./operations/db_ops').addUser;
- const executeDbAdminCommand = require('./operations/db_ops').executeDbAdminCommand;
- const removeUser = require('./operations/db_ops').removeUser;
- const replSetGetStatus = require('./operations/admin_ops').replSetGetStatus;
- const serverStatus = require('./operations/admin_ops').serverStatus;
- const validateCollection = require('./operations/admin_ops').validateCollection;
-
-
-
-
- function Admin(db, topology, promiseLibrary) {
- if (!(this instanceof Admin)) return new Admin(db, topology);
-
-
- this.s = {
- db: db,
- topology: topology,
- promiseLibrary: promiseLibrary
- };
- }
-
-
-
-
- Admin.prototype.command = function(command, options, callback) {
- const args = Array.prototype.slice.call(arguments, 1);
- callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
- options = args.length ? args.shift() : {};
-
- return executeOperation(this.s.db.s.topology, executeDbAdminCommand.bind(this.s.db), [
- this.s.db,
- command,
- options,
- callback
- ]);
- };
-
-
- Admin.prototype.buildInfo = function(options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
-
- const cmd = { buildinfo: 1 };
- return executeOperation(this.s.db.s.topology, executeDbAdminCommand.bind(this.s.db), [
- this.s.db,
- cmd,
- options,
- callback
- ]);
- };
-
-
- Admin.prototype.serverInfo = function(options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
-
- const cmd = { buildinfo: 1 };
- return executeOperation(this.s.db.s.topology, executeDbAdminCommand.bind(this.s.db), [
- this.s.db,
- cmd,
- options,
- callback
- ]);
- };
-
-
- Admin.prototype.serverStatus = function(options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
-
- return executeOperation(this.s.db.s.topology, serverStatus, [this, options, callback]);
- };
-
-
- Admin.prototype.ping = function(options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
-
- const cmd = { ping: 1 };
- return executeOperation(this.s.db.s.topology, executeDbAdminCommand.bind(this.s.db), [
- this.s.db,
- cmd,
- options,
- callback
- ]);
- };
-
-
- Admin.prototype.addUser = function(username, password, options, callback) {
- const args = Array.prototype.slice.call(arguments, 2);
- callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
-
- options = args.length ? args.shift() : {};
- options = Object.assign({}, options);
-
- options = applyWriteConcern(options, { db: this.s.db });
-
- options.dbName = 'admin';
-
- return executeOperation(this.s.db.s.topology, addUser, [
- this.s.db,
- username,
- password,
- options,
- callback
- ]);
- };
-
-
- Admin.prototype.removeUser = function(username, options, callback) {
- const args = Array.prototype.slice.call(arguments, 1);
- callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
-
- options = args.length ? args.shift() : {};
- options = Object.assign({}, options);
-
- options = applyWriteConcern(options, { db: this.s.db });
-
- options.dbName = 'admin';
-
- return executeOperation(this.s.db.s.topology, removeUser, [
- this.s.db,
- username,
- options,
- callback
- ]);
- };
-
-
- Admin.prototype.validateCollection = function(collectionName, options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
-
- return executeOperation(this.s.db.s.topology, validateCollection, [
- this,
- collectionName,
- options,
- callback
- ]);
- };
-
-
- Admin.prototype.listDatabases = function(options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
-
- const cmd = { listDatabases: 1 };
- if (options.nameOnly) cmd.nameOnly = Number(cmd.nameOnly);
- return executeOperation(this.s.db.s.topology, executeDbAdminCommand.bind(this.s.db), [
- this.s.db,
- cmd,
- options,
- callback
- ]);
- };
-
-
- Admin.prototype.replSetGetStatus = function(options, callback) {
- if (typeof options === 'function') (callback = options), (options = {});
- options = options || {};
-
- return executeOperation(this.s.db.s.topology, replSetGetStatus, [this, options, callback]);
- };
-
- module.exports = Admin;
|