-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathadmin.ts
192 lines (179 loc) · 5.75 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
import type { Document } from './bson';
import type { Db } from './db';
import { AddUserOperation, AddUserOptions } from './operations/add_user';
import type { CommandOperationOptions } from './operations/command';
import { executeOperation } from './operations/execute_operation';
import {
ListDatabasesOperation,
ListDatabasesOptions,
ListDatabasesResult
} from './operations/list_databases';
import { RemoveUserOperation, RemoveUserOptions } from './operations/remove_user';
import { RunCommandOperation, RunCommandOptions } from './operations/run_command';
import {
ValidateCollectionOperation,
ValidateCollectionOptions
} from './operations/validate_collection';
/** @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
* ```ts
* import { MongoClient } from 'mongodb';
*
* const client = new MongoClient('mongodb://localhost:27017');
* const admin = client.db().admin();
* const dbInfo = await admin.listDatabases();
* for (const db of dbInfo.databases) {
* console.log(db.name);
* }
* ```
*/
export class Admin {
/** @internal */
s: AdminPrivate;
/**
* Create a new Admin instance
* @internal
*/
constructor(db: Db) {
this.s = { db };
}
/**
* Execute a command
*
* The driver will ensure the following fields are attached to the command sent to the server:
* - `lsid` - sourced from an implicit session or options.session
* - `$readPreference` - defaults to primary or can be configured by options.readPreference
* - `$db` - sourced from the name of this database
*
* If the client has a serverApi setting:
* - `apiVersion`
* - `apiStrict`
* - `apiDeprecationErrors`
*
* When in a transaction:
* - `readConcern` - sourced from readConcern set on the TransactionOptions
* - `writeConcern` - sourced from writeConcern set on the TransactionOptions
*
* Attaching any of the above fields to the command will have no effect as the driver will overwrite the value.
*
* @param command - The command to execute
* @param options - Optional settings for the command
*/
async command(command: Document, options?: RunCommandOptions): Promise<Document> {
return executeOperation(
this.s.db.client,
new RunCommandOperation(this.s.db, command, { dbName: 'admin', ...options })
);
}
/**
* Retrieve the server build information
*
* @param options - Optional settings for the command
*/
async buildInfo(options?: CommandOperationOptions): Promise<Document> {
return this.command({ buildinfo: 1 }, options);
}
/**
* Retrieve the server build information
*
* @param options - Optional settings for the command
*/
async serverInfo(options?: CommandOperationOptions): Promise<Document> {
return this.command({ buildinfo: 1 }, options);
}
/**
* Retrieve this db's server status.
*
* @param options - Optional settings for the command
*/
async serverStatus(options?: CommandOperationOptions): Promise<Document> {
return this.command({ serverStatus: 1 }, options);
}
/**
* Ping the MongoDB server and retrieve results
*
* @param options - Optional settings for the command
*/
async ping(options?: CommandOperationOptions): Promise<Document> {
return this.command({ ping: 1 }, options);
}
/**
* Add a user to the database
*
* @param username - The username for the new user
* @param passwordOrOptions - An optional password for the new user, or the options for the command
* @param options - Optional settings for the command
* @deprecated Use the createUser command in `db.command()` instead.
* @see https://www.mongodb.com/docs/manual/reference/command/createUser/
*/
async addUser(
username: string,
passwordOrOptions?: string | AddUserOptions,
options?: AddUserOptions
): Promise<Document> {
options =
options != null && typeof options === 'object'
? options
: passwordOrOptions != null && typeof passwordOrOptions === 'object'
? passwordOrOptions
: undefined;
const password = typeof passwordOrOptions === 'string' ? passwordOrOptions : undefined;
return executeOperation(
this.s.db.client,
new AddUserOperation(this.s.db, username, password, { dbName: 'admin', ...options })
);
}
/**
* Remove a user from a database
*
* @param username - The username to remove
* @param options - Optional settings for the command
*/
async removeUser(username: string, options?: RemoveUserOptions): Promise<boolean> {
return executeOperation(
this.s.db.client,
new RemoveUserOperation(this.s.db, username, { dbName: 'admin', ...options })
);
}
/**
* Validate an existing collection
*
* @param collectionName - The name of the collection to validate.
* @param options - Optional settings for the command
*/
async validateCollection(
collectionName: string,
options: ValidateCollectionOptions = {}
): Promise<Document> {
return executeOperation(
this.s.db.client,
new ValidateCollectionOperation(this, collectionName, options)
);
}
/**
* List the available databases
*
* @param options - Optional settings for the command
*/
async listDatabases(options?: ListDatabasesOptions): Promise<ListDatabasesResult> {
return executeOperation(this.s.db.client, new ListDatabasesOperation(this.s.db, options));
}
/**
* Get ReplicaSet status
*
* @param options - Optional settings for the command
*/
async replSetGetStatus(options?: CommandOperationOptions): Promise<Document> {
return this.command({ replSetGetStatus: 1 }, options);
}
}