admin.js
5.06 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Admin = void 0;
const add_user_1 = require("./operations/add_user");
const execute_operation_1 = require("./operations/execute_operation");
const list_databases_1 = require("./operations/list_databases");
const remove_user_1 = require("./operations/remove_user");
const run_command_1 = require("./operations/run_command");
const validate_collection_1 = require("./operations/validate_collection");
/**
* The **Admin** class is an internal class that allows convenient access to
* the admin functionality and commands for MongoDB.
*
* **ADMIN Cannot directly be instantiated**
* @public
*
* @example
* ```js
* 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) {
* expect(err).to.not.exist;
* test.ok(dbs.databases.length > 0);
* client.close();
* });
* });
* ```
*/
class Admin {
/**
* Create a new Admin instance
* @internal
*/
constructor(db) {
this.s = { db };
}
command(command, options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
options = Object.assign({ dbName: 'admin' }, options);
return (0, execute_operation_1.executeOperation)(this.s.db, new run_command_1.RunCommandOperation(this.s.db, command, options), callback);
}
buildInfo(options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
options = options !== null && options !== void 0 ? options : {};
return this.command({ buildinfo: 1 }, options, callback);
}
serverInfo(options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
options = options !== null && options !== void 0 ? options : {};
return this.command({ buildinfo: 1 }, options, callback);
}
serverStatus(options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
options = options !== null && options !== void 0 ? options : {};
return this.command({ serverStatus: 1 }, options, callback);
}
ping(options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
options = options !== null && options !== void 0 ? options : {};
return this.command({ ping: 1 }, options, callback);
}
addUser(username, password, options, callback) {
if (typeof password === 'function') {
(callback = password), (password = undefined), (options = {});
}
else if (typeof password !== 'string') {
if (typeof options === 'function') {
(callback = options), (options = password), (password = undefined);
}
else {
(options = password), (callback = undefined), (password = undefined);
}
}
else {
if (typeof options === 'function')
(callback = options), (options = {});
}
options = Object.assign({ dbName: 'admin' }, options);
return (0, execute_operation_1.executeOperation)(this.s.db, new add_user_1.AddUserOperation(this.s.db, username, password, options), callback);
}
removeUser(username, options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
options = Object.assign({ dbName: 'admin' }, options);
return (0, execute_operation_1.executeOperation)(this.s.db, new remove_user_1.RemoveUserOperation(this.s.db, username, options), callback);
}
validateCollection(collectionName, options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
options = options !== null && options !== void 0 ? options : {};
return (0, execute_operation_1.executeOperation)(this.s.db, new validate_collection_1.ValidateCollectionOperation(this, collectionName, options), callback);
}
listDatabases(options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
options = options !== null && options !== void 0 ? options : {};
return (0, execute_operation_1.executeOperation)(this.s.db, new list_databases_1.ListDatabasesOperation(this.s.db, options), callback);
}
replSetGetStatus(options, callback) {
if (typeof options === 'function')
(callback = options), (options = {});
options = options !== null && options !== void 0 ? options : {};
return this.command({ replSetGetStatus: 1 }, options, callback);
}
}
exports.Admin = Admin;
//# sourceMappingURL=admin.js.map