admin.js
11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
'use strict';
const applyWriteConcern = require('./utils').applyWriteConcern;
const AddUserOperation = require('./operations/add_user');
const ExecuteDbAdminCommandOperation = require('./operations/execute_db_admin_command');
const RemoveUserOperation = require('./operations/remove_user');
const ValidateCollectionOperation = require('./operations/validate_collection');
const ListDatabasesOperation = require('./operations/list_databases');
const executeOperation = require('./operations/execute_operation');
/**
* @fileOverview The **Admin** class is an internal class that allows convenient access to
* the admin functionality and commands for MongoDB.
*
* **ADMIN Cannot directly be instantiated**
* @example
* const MongoClient = require('mongodb').MongoClient;
* const test = require('assert');
* // Connection url
* const url = 'mongodb://localhost:27017';
* // Database Name
* const dbName = 'test';
*
* // Connect using MongoClient
* MongoClient.connect(url, function(err, client) {
* // Use the admin database for the operation
* const adminDb = client.db(dbName).admin();
*
* // List all the available databases
* adminDb.listDatabases(function(err, dbs) {
* test.equal(null, err);
* test.ok(dbs.databases.length > 0);
* client.close();
* });
* });
*/
/**
* Create a new Admin instance (INTERNAL TYPE, do not instantiate directly)
* @class
* @return {Admin} a collection instance.
*/
function Admin(db, topology, promiseLibrary) {
if (!(this instanceof Admin)) return new Admin(db, topology);
// Internal state
this.s = {
db: db,
topology: topology,
promiseLibrary: promiseLibrary
};
}
/**
* The callback format for results
* @callback Admin~resultCallback
* @param {MongoError} error An error instance representing the error during the execution.
* @param {object} result The result object if the command was executed successfully.
*/
/**
* Execute a command
* @method
* @param {object} command The command hash
* @param {object} [options] Optional settings.
* @param {(ReadPreference|string)} [options.readPreference] The preferred read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
* @param {number} [options.maxTimeMS] Number of milliseconds to wait before aborting the query.
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
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() : {};
const commandOperation = new ExecuteDbAdminCommandOperation(this.s.db, command, options);
return executeOperation(this.s.db.s.topology, commandOperation, callback);
};
/**
* Retrieve the server information for the current
* instance of the db client
*
* @param {Object} [options] optional parameters for this operation
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.buildInfo = function(options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
const cmd = { buildinfo: 1 };
const buildInfoOperation = new ExecuteDbAdminCommandOperation(this.s.db, cmd, options);
return executeOperation(this.s.db.s.topology, buildInfoOperation, callback);
};
/**
* Retrieve the server information for the current
* instance of the db client
*
* @param {Object} [options] optional parameters for this operation
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.serverInfo = function(options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
const cmd = { buildinfo: 1 };
const serverInfoOperation = new ExecuteDbAdminCommandOperation(this.s.db, cmd, options);
return executeOperation(this.s.db.s.topology, serverInfoOperation, callback);
};
/**
* Retrieve this db's server status.
*
* @param {Object} [options] optional parameters for this operation
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.serverStatus = function(options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
const serverStatusOperation = new ExecuteDbAdminCommandOperation(
this.s.db,
{ serverStatus: 1 },
options
);
return executeOperation(this.s.db.s.topology, serverStatusOperation, callback);
};
/**
* Ping the MongoDB server and retrieve results
*
* @param {Object} [options] optional parameters for this operation
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.ping = function(options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
const cmd = { ping: 1 };
const pingOperation = new ExecuteDbAdminCommandOperation(this.s.db, cmd, options);
return executeOperation(this.s.db.s.topology, pingOperation, callback);
};
/**
* Add a user to the database.
* @method
* @param {string} username The username.
* @param {string} password The password.
* @param {object} [options] Optional settings.
* @param {(number|string)} [options.w] **Deprecated** The write concern. Use writeConcern instead.
* @param {number} [options.wtimeout] **Deprecated** The write concern timeout. Use writeConcern instead.
* @param {boolean} [options.j=false] **Deprecated** Specify a journal write concern. Use writeConcern instead.
* @param {boolean} [options.fsync=false] **Deprecated** Specify a file sync write concern. Use writeConcern instead.
* @param {object|WriteConcern} [options.writeConcern] Specify write concern settings.
* @param {object} [options.customData] Custom data associated with the user (only Mongodb 2.6 or higher)
* @param {object[]} [options.roles] Roles associated with the created user (only Mongodb 2.6 or higher)
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
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;
// Special case where there is no password ($external users)
if (typeof username === 'string' && password != null && typeof password === 'object') {
options = password;
password = null;
}
options = args.length ? args.shift() : {};
options = Object.assign({}, options);
// Get the options
options = applyWriteConcern(options, { db: this.s.db });
// Set the db name to admin
options.dbName = 'admin';
const addUserOperation = new AddUserOperation(this.s.db, username, password, options);
return executeOperation(this.s.db.s.topology, addUserOperation, callback);
};
/**
* Remove a user from a database
* @method
* @param {string} username The username.
* @param {object} [options] Optional settings.
* @param {(number|string)} [options.w] **Deprecated** The write concern. Use writeConcern instead.
* @param {number} [options.wtimeout] **Deprecated** The write concern timeout. Use writeConcern instead.
* @param {boolean} [options.j=false] **Deprecated** Specify a journal write concern. Use writeConcern instead.
* @param {boolean} [options.fsync=false] **Deprecated** Specify a file sync write concern. Use writeConcern instead.
* @param {object|WriteConcern} [options.writeConcern] Specify write concern settings.
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {Admin~resultCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
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);
// Get the options
options = applyWriteConcern(options, { db: this.s.db });
// Set the db name
options.dbName = 'admin';
const removeUserOperation = new RemoveUserOperation(this.s.db, username, options);
return executeOperation(this.s.db.s.topology, removeUserOperation, callback);
};
/**
* Validate an existing collection
*
* @param {string} collectionName The name of the collection to validate.
* @param {object} [options] Optional settings.
* @param {boolean} [options.background] Validates a collection in the background, without interrupting read or write traffic (only in MongoDB 4.4+)
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {Admin~resultCallback} [callback] The command result callback.
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.validateCollection = function(collectionName, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
const validateCollectionOperation = new ValidateCollectionOperation(
this,
collectionName,
options
);
return executeOperation(this.s.db.s.topology, validateCollectionOperation, callback);
};
/**
* List the available databases
*
* @param {object} [options] Optional settings.
* @param {boolean} [options.nameOnly=false] Whether the command should return only db names, or names and size info.
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {Admin~resultCallback} [callback] The command result callback.
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.listDatabases = function(options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
return executeOperation(
this.s.db.s.topology,
new ListDatabasesOperation(this.s.db, options),
callback
);
};
/**
* Get ReplicaSet status
*
* @param {Object} [options] optional parameters for this operation
* @param {ClientSession} [options.session] optional session to use for this operation
* @param {Admin~resultCallback} [callback] The command result callback.
* @return {Promise} returns Promise if no callback passed
*/
Admin.prototype.replSetGetStatus = function(options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
const replSetGetStatusOperation = new ExecuteDbAdminCommandOperation(
this.s.db,
{ replSetGetStatus: 1 },
options
);
return executeOperation(this.s.db.s.topology, replSetGetStatusOperation, callback);
};
module.exports = Admin;