-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathadmin.ts
314 lines (295 loc) · 11.2 KB
/
admin.ts
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import { AddUserOperation, AddUserOptions } from './operations/add_user';
import { RemoveUserOperation, RemoveUserOptions } from './operations/remove_user';
import {
ValidateCollectionOperation,
ValidateCollectionOptions
} from './operations/validate_collection';
import {
ListDatabasesOperation,
ListDatabasesOptions,
ListDatabasesResult
} from './operations/list_databases';
import { executeOperation } from './operations/execute_operation';
import { RunCommandOperation, RunCommandOptions } from './operations/run_command';
import { Callback, getTopology } from './utils';
import type { Document } from './bson';
import type { CommandOperationOptions } from './operations/command';
import type { Db } from './db';
/** @internal */
export interface AdminPrivate {
db: Db;
}
/**
* 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();
* });
* });
* ```
*/
export class Admin {
/** @internal */
s: AdminPrivate;
/**
* Create a new Admin instance
* @internal
*/
constructor(db: Db) {
this.s = { db };
}
/**
* Execute a command
*
* @param command - The command to execute
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
command(command: Document): Promise<Document>;
command(command: Document, callback: Callback<Document>): void;
command(command: Document, options: RunCommandOptions): Promise<Document>;
command(command: Document, options: RunCommandOptions, callback: Callback<Document>): void;
command(
command: Document,
options?: RunCommandOptions | Callback<Document>,
callback?: Callback<Document>
): Promise<Document> | void {
if (typeof options === 'function') (callback = options), (options = {});
options = Object.assign({ dbName: 'admin' }, options);
return executeOperation(
getTopology(this.s.db),
new RunCommandOperation(this.s.db, command, options),
callback
);
}
/**
* Retrieve the server build information
*
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
buildInfo(): Promise<Document>;
buildInfo(callback: Callback<Document>): void;
buildInfo(options: CommandOperationOptions): Promise<Document>;
buildInfo(options: CommandOperationOptions, callback: Callback<Document>): void;
buildInfo(
options?: CommandOperationOptions | Callback<Document>,
callback?: Callback<Document>
): Promise<Document> | void {
if (typeof options === 'function') (callback = options), (options = {});
options = options ?? {};
return this.command({ buildinfo: 1 }, options, callback as Callback<Document>);
}
/**
* Retrieve the server build information
*
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
serverInfo(): Promise<Document>;
serverInfo(callback: Callback<Document>): void;
serverInfo(options: CommandOperationOptions): Promise<Document>;
serverInfo(options: CommandOperationOptions, callback: Callback<Document>): void;
serverInfo(
options?: CommandOperationOptions | Callback<Document>,
callback?: Callback<Document>
): Promise<Document> | void {
if (typeof options === 'function') (callback = options), (options = {});
options = options ?? {};
return this.command({ buildinfo: 1 }, options, callback as Callback<Document>);
}
/**
* Retrieve this db's server status.
*
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
serverStatus(): Promise<Document>;
serverStatus(callback: Callback<Document>): void;
serverStatus(options: CommandOperationOptions): Promise<Document>;
serverStatus(options: CommandOperationOptions, callback: Callback<Document>): void;
serverStatus(
options?: CommandOperationOptions | Callback<Document>,
callback?: Callback<Document>
): Promise<Document> | void {
if (typeof options === 'function') (callback = options), (options = {});
options = options ?? {};
return this.command({ serverStatus: 1 }, options, callback as Callback<Document>);
}
/**
* Ping the MongoDB server and retrieve results
*
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
ping(): Promise<Document>;
ping(callback: Callback<Document>): void;
ping(options: CommandOperationOptions): Promise<Document>;
ping(options: CommandOperationOptions, callback: Callback<Document>): void;
ping(
options?: CommandOperationOptions | Callback<Document>,
callback?: Callback<Document>
): Promise<Document> | void {
if (typeof options === 'function') (callback = options), (options = {});
options = options ?? {};
return this.command({ ping: 1 }, options, callback as Callback<Document>);
}
/**
* Add a user to the database
*
* @param username - The username for the new user
* @param password - An optional password for the new user
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
addUser(username: string): Promise<Document>;
addUser(username: string, callback: Callback<Document>): void;
addUser(username: string, password: string): Promise<Document>;
addUser(username: string, password: string, callback: Callback<Document>): void;
addUser(username: string, options: AddUserOptions): Promise<Document>;
addUser(username: string, options: AddUserOptions, callback: Callback<Document>): void;
addUser(username: string, password: string, options: AddUserOptions): Promise<Document>;
addUser(
username: string,
password: string,
options: AddUserOptions,
callback: Callback<Document>
): void;
addUser(
username: string,
password?: string | AddUserOptions | Callback<Document>,
options?: AddUserOptions | Callback<Document>,
callback?: Callback<Document>
): Promise<Document> | void {
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 executeOperation(
getTopology(this.s.db),
new AddUserOperation(this.s.db, username, password, options),
callback
);
}
/**
* Remove a user from a database
*
* @param username - The username to remove
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
removeUser(username: string): Promise<boolean>;
removeUser(username: string, callback: Callback<boolean>): void;
removeUser(username: string, options: RemoveUserOptions): Promise<boolean>;
removeUser(username: string, options: RemoveUserOptions, callback: Callback<boolean>): void;
removeUser(
username: string,
options?: RemoveUserOptions | Callback<boolean>,
callback?: Callback<boolean>
): Promise<boolean> | void {
if (typeof options === 'function') (callback = options), (options = {});
options = Object.assign({ dbName: 'admin' }, options);
return executeOperation(
getTopology(this.s.db),
new RemoveUserOperation(this.s.db, username, options),
callback
);
}
/**
* Validate an existing collection
*
* @param collectionName - The name of the collection to validate.
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
validateCollection(collectionName: string): Promise<Document>;
validateCollection(collectionName: string, callback: Callback<Document>): void;
validateCollection(collectionName: string, options: ValidateCollectionOptions): Promise<Document>;
validateCollection(
collectionName: string,
options: ValidateCollectionOptions,
callback: Callback<Document>
): void;
validateCollection(
collectionName: string,
options?: ValidateCollectionOptions | Callback<Document>,
callback?: Callback<Document>
): Promise<Document> | void {
if (typeof options === 'function') (callback = options), (options = {});
options = options ?? {};
return executeOperation(
getTopology(this.s.db),
new ValidateCollectionOperation(this, collectionName, options),
callback
);
}
/**
* List the available databases
*
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
listDatabases(): Promise<ListDatabasesResult>;
listDatabases(callback: Callback<ListDatabasesResult>): void;
listDatabases(options: ListDatabasesOptions): Promise<ListDatabasesResult>;
listDatabases(options: ListDatabasesOptions, callback: Callback<ListDatabasesResult>): void;
listDatabases(
options?: ListDatabasesOptions | Callback<ListDatabasesResult>,
callback?: Callback<ListDatabasesResult>
): Promise<ListDatabasesResult> | void {
if (typeof options === 'function') (callback = options), (options = {});
options = options ?? {};
return executeOperation(
getTopology(this.s.db),
new ListDatabasesOperation(this.s.db, options),
callback
);
}
/**
* Get ReplicaSet status
*
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
replSetGetStatus(): Promise<Document>;
replSetGetStatus(callback: Callback<Document>): void;
replSetGetStatus(options: CommandOperationOptions): Promise<Document>;
replSetGetStatus(options: CommandOperationOptions, callback: Callback<Document>): void;
replSetGetStatus(
options?: CommandOperationOptions | Callback<Document>,
callback?: Callback<Document>
): Promise<Document> | void {
if (typeof options === 'function') (callback = options), (options = {});
options = options ?? {};
return this.command({ replSetGetStatus: 1 }, options, callback as Callback<Document>);
}
}