@qp-mongosh/shell-api 0.0.0-dev.5 → 0.0.0-dev.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintignore +2 -2
- package/.eslintrc.js +1 -1
- package/AUTHORS +15 -15
- package/LICENSE +200 -200
- package/bin/report-missing-help.ts +24 -24
- package/bin/report-supported-api.ts +14 -14
- package/lib/abstract-cursor.d.ts +33 -33
- package/lib/abstract-cursor.js +191 -191
- package/lib/aggregation-cursor.d.ts +6 -6
- package/lib/aggregation-cursor.js +19 -19
- package/lib/bulk.d.ts +43 -43
- package/lib/bulk.js +223 -223
- package/lib/change-stream-cursor.d.ts +28 -28
- package/lib/change-stream-cursor.js +111 -111
- package/lib/collection.d.ts +95 -95
- package/lib/collection.js +964 -964
- package/lib/cursor.d.ts +32 -32
- package/lib/cursor.js +215 -215
- package/lib/database.d.ts +116 -116
- package/lib/database.js +1223 -1223
- package/lib/dbquery.d.ts +8 -8
- package/lib/dbquery.js +28 -28
- package/lib/decorators.d.ts +73 -73
- package/lib/decorators.js +395 -395
- package/lib/deprecation-warning.d.ts +2 -0
- package/lib/deprecation-warning.js +19 -0
- package/lib/deprecation-warning.js.map +1 -0
- package/lib/enums.d.ts +28 -28
- package/lib/enums.js +33 -33
- package/lib/error-codes.d.ts +12 -12
- package/lib/error-codes.js +19 -19
- package/lib/explainable-cursor.d.ts +11 -11
- package/lib/explainable-cursor.js +31 -31
- package/lib/explainable.d.ts +32 -32
- package/lib/explainable.js +166 -166
- package/lib/field-level-encryption.d.ts +50 -50
- package/lib/field-level-encryption.js +176 -176
- package/lib/help.d.ts +22 -22
- package/lib/help.js +26 -26
- package/lib/helpers.d.ts +71 -71
- package/lib/helpers.js +588 -588
- package/lib/index.d.ts +16 -16
- package/lib/index.js +45 -45
- package/lib/interruptor.d.ts +19 -19
- package/lib/interruptor.js +62 -62
- package/lib/mongo-errors.d.ts +5 -5
- package/lib/mongo-errors.js +37 -37
- package/lib/mongo.d.ts +75 -75
- package/lib/mongo.js +476 -476
- package/lib/no-db.d.ts +5 -5
- package/lib/no-db.js +28 -28
- package/lib/plan-cache.d.ts +16 -16
- package/lib/plan-cache.js +70 -70
- package/lib/replica-set.d.ts +45 -45
- package/lib/replica-set.js +314 -314
- package/lib/result.d.ts +61 -61
- package/lib/result.js +104 -104
- package/lib/session.d.ts +25 -25
- package/lib/session.js +88 -88
- package/lib/shard.d.ts +42 -42
- package/lib/shard.js +414 -414
- package/lib/shell-api.d.ts +52 -52
- package/lib/shell-api.js +298 -298
- package/lib/shell-bson.d.ts +40 -40
- package/lib/shell-bson.js +159 -159
- package/lib/shell-instance-state.d.ts +77 -77
- package/lib/shell-instance-state.js +392 -392
- package/lib/shell-internal-state.d.ts +74 -0
- package/lib/shell-internal-state.js +364 -0
- package/lib/shell-internal-state.js.map +1 -0
- package/package.json +47 -47
- package/tsconfig.lint.json +8 -8
package/lib/mongo.js
CHANGED
|
@@ -1,477 +1,477 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
const errors_1 = require("@qp-mongosh/errors");
|
|
13
|
-
const decorators_1 = require("./decorators");
|
|
14
|
-
const service_provider_core_1 = require("@qp-mongosh/service-provider-core");
|
|
15
|
-
const database_1 = __importDefault(require("./database"));
|
|
16
|
-
const result_1 = require("./result");
|
|
17
|
-
const history_1 = require("@qp-mongosh/history");
|
|
18
|
-
const enums_1 = require("./enums");
|
|
19
|
-
const session_1 = __importDefault(require("./session"));
|
|
20
|
-
const helpers_1 = require("./helpers");
|
|
21
|
-
const change_stream_cursor_1 = __importDefault(require("./change-stream-cursor"));
|
|
22
|
-
const error_codes_1 = require("./error-codes");
|
|
23
|
-
const field_level_encryption_1 = require("./field-level-encryption");
|
|
24
|
-
const error_codes_2 = require("./error-codes");
|
|
25
|
-
let Mongo = class Mongo extends decorators_1.ShellApiClass {
|
|
26
|
-
constructor(instanceState, uri, fleOptions, otherOptions, sp) {
|
|
27
|
-
var _a, _b, _c;
|
|
28
|
-
super();
|
|
29
|
-
this.__serviceProvider = null;
|
|
30
|
-
this._readPreferenceWasExplicitlyRequested = false;
|
|
31
|
-
this._explicitEncryptionOnly = false;
|
|
32
|
-
this._cachedDatabaseNames = [];
|
|
33
|
-
this._instanceState = instanceState;
|
|
34
|
-
this._databases = {};
|
|
35
|
-
if (sp) {
|
|
36
|
-
this.__serviceProvider = sp;
|
|
37
|
-
}
|
|
38
|
-
if (typeof uri === 'string') {
|
|
39
|
-
this._uri = (0, service_provider_core_1.generateUri)({ connectionSpecifier: uri });
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
this._uri = (_b = (_a = sp === null || sp === void 0 ? void 0 : sp.getURI) === null || _a === void 0 ? void 0 : _a.call(sp)) !== null && _b !== void 0 ? _b : (0, service_provider_core_1.generateUri)({ connectionSpecifier: 'mongodb://localhost/' });
|
|
43
|
-
}
|
|
44
|
-
this._readPreferenceWasExplicitlyRequested = /\breadPreference=/.test(this._uri);
|
|
45
|
-
if (fleOptions) {
|
|
46
|
-
if (fleOptions.explicitEncryptionOnly !== undefined) {
|
|
47
|
-
if (fleOptions.schemaMap !== undefined) {
|
|
48
|
-
throw new errors_1.MongoshInvalidInputError('explicitEncryptionOnly and schemaMap are mutually exclusive', errors_1.CommonErrors.InvalidArgument);
|
|
49
|
-
}
|
|
50
|
-
fleOptions = { ...fleOptions };
|
|
51
|
-
this._explicitEncryptionOnly = !!fleOptions.explicitEncryptionOnly;
|
|
52
|
-
delete fleOptions.explicitEncryptionOnly;
|
|
53
|
-
}
|
|
54
|
-
this._fleOptions = (0, helpers_1.processFLEOptions)(fleOptions);
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
const spFleOptions = (_c = sp === null || sp === void 0 ? void 0 : sp.getFleOptions) === null || _c === void 0 ? void 0 : _c.call(sp);
|
|
58
|
-
if (spFleOptions) {
|
|
59
|
-
this._fleOptions = spFleOptions;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
if (otherOptions === null || otherOptions === void 0 ? void 0 : otherOptions.api) {
|
|
63
|
-
if (typeof otherOptions.api === 'string') {
|
|
64
|
-
this._apiOptions = { version: otherOptions.api };
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
this._apiOptions = otherOptions.api;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
get _serviceProvider() {
|
|
72
|
-
if (this.__serviceProvider === null) {
|
|
73
|
-
throw new errors_1.MongoshInternalError('No ServiceProvider available for this mongo', error_codes_2.ShellApiErrors.NotConnected);
|
|
74
|
-
}
|
|
75
|
-
return this.__serviceProvider;
|
|
76
|
-
}
|
|
77
|
-
set _serviceProvider(sp) {
|
|
78
|
-
this.__serviceProvider = sp;
|
|
79
|
-
}
|
|
80
|
-
async _displayBatchSize() {
|
|
81
|
-
var _a;
|
|
82
|
-
return (_a = this._instanceState.displayBatchSizeFromDBQuery) !== null && _a !== void 0 ? _a : await this._instanceState.shellApi.config.get('displayBatchSize');
|
|
83
|
-
}
|
|
84
|
-
[enums_1.asPrintable]() {
|
|
85
|
-
return (0, history_1.redactURICredentials)(this._uri);
|
|
86
|
-
}
|
|
87
|
-
_emitMongoApiCall(methodName, methodArguments = {}) {
|
|
88
|
-
this._instanceState.emitApiCallWithArgs({
|
|
89
|
-
method: methodName,
|
|
90
|
-
class: 'Mongo',
|
|
91
|
-
uri: this._uri,
|
|
92
|
-
arguments: methodArguments
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
async connect(user, pwd) {
|
|
96
|
-
var _a, _b, _c, _d, _e, _f;
|
|
97
|
-
const mongoClientOptions = user || pwd ? {
|
|
98
|
-
auth: { username: user, password: pwd }
|
|
99
|
-
} : {};
|
|
100
|
-
if (this._fleOptions && !this._explicitEncryptionOnly) {
|
|
101
|
-
const extraOptions = {
|
|
102
|
-
...((_a = this._fleOptions.extraOptions) !== null && _a !== void 0 ? _a : {}),
|
|
103
|
-
...((_d = await ((_c = (_b = this._instanceState.evaluationListener) === null || _b === void 0 ? void 0 : _b.startMongocryptd) === null || _c === void 0 ? void 0 : _c.call(_b))) !== null && _d !== void 0 ? _d : {})
|
|
104
|
-
};
|
|
105
|
-
mongoClientOptions.autoEncryption = { ...this._fleOptions, extraOptions };
|
|
106
|
-
}
|
|
107
|
-
if (this._apiOptions) {
|
|
108
|
-
mongoClientOptions.serverApi = this._apiOptions;
|
|
109
|
-
}
|
|
110
|
-
const parentProvider = this._instanceState.initialServiceProvider;
|
|
111
|
-
try {
|
|
112
|
-
this.__serviceProvider = await parentProvider.getNewConnection(this._uri, mongoClientOptions);
|
|
113
|
-
}
|
|
114
|
-
catch (e) {
|
|
115
|
-
if ((e === null || e === void 0 ? void 0 : e.name) === 'MongoServerSelectionError' &&
|
|
116
|
-
((_f = (_e = parentProvider.getRawClient()) === null || _e === void 0 ? void 0 : _e.options) === null || _f === void 0 ? void 0 : _f.tls) &&
|
|
117
|
-
!this._uri.match(/\b(ssl|tls)=/)) {
|
|
118
|
-
e.message += ' (is ?tls=true missing from the connection string?)';
|
|
119
|
-
}
|
|
120
|
-
throw e;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
_getDb(name) {
|
|
124
|
-
(0, helpers_1.assertArgsDefinedType)([name], ['string']);
|
|
125
|
-
if (!(0, helpers_1.isValidDatabaseName)(name)) {
|
|
126
|
-
throw new errors_1.MongoshInvalidInputError(`Invalid database name: ${name}`, errors_1.CommonErrors.InvalidArgument);
|
|
127
|
-
}
|
|
128
|
-
if (!(name in this._databases)) {
|
|
129
|
-
this._databases[name] = new database_1.default(this, name);
|
|
130
|
-
}
|
|
131
|
-
return this._databases[name];
|
|
132
|
-
}
|
|
133
|
-
getDB(db) {
|
|
134
|
-
(0, helpers_1.assertArgsDefinedType)([db], ['string'], 'Mongo.getDB');
|
|
135
|
-
this._instanceState.messageBus.emit('mongosh:getDB', { db });
|
|
136
|
-
return this._getDb(db);
|
|
137
|
-
}
|
|
138
|
-
getCollection(name) {
|
|
139
|
-
var _a, _b;
|
|
140
|
-
(0, helpers_1.assertArgsDefinedType)([name], ['string']);
|
|
141
|
-
const { db, coll } = (_b = (_a = name.match(/^(?<db>[^.]+)\.(?<coll>.+)$/)) === null || _a === void 0 ? void 0 : _a.groups) !== null && _b !== void 0 ? _b : {};
|
|
142
|
-
if (!db || !coll) {
|
|
143
|
-
throw new errors_1.MongoshInvalidInputError('Collection must be of the format <db>.<collection>', errors_1.CommonErrors.InvalidArgument);
|
|
144
|
-
}
|
|
145
|
-
return this._getDb(db).getCollection(coll);
|
|
146
|
-
}
|
|
147
|
-
use(db) {
|
|
148
|
-
var _a;
|
|
149
|
-
(0, helpers_1.assertArgsDefinedType)([db], ['string'], 'Mongo.use');
|
|
150
|
-
this._instanceState.messageBus.emit('mongosh:use', { db });
|
|
151
|
-
let previousDbName;
|
|
152
|
-
let previousDbMongo;
|
|
153
|
-
try {
|
|
154
|
-
const previousDb = this._instanceState.context.db;
|
|
155
|
-
previousDbName = (_a = previousDb === null || previousDb === void 0 ? void 0 : previousDb.getName) === null || _a === void 0 ? void 0 : _a.call(previousDb);
|
|
156
|
-
previousDbMongo = previousDb === null || previousDb === void 0 ? void 0 : previousDb._mongo;
|
|
157
|
-
}
|
|
158
|
-
catch (e) {
|
|
159
|
-
if ((e === null || e === void 0 ? void 0 : e.code) !== error_codes_2.ShellApiErrors.NotConnected) {
|
|
160
|
-
throw e;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
this._instanceState.context.db = this._getDb(db);
|
|
164
|
-
if (db === previousDbName && previousDbMongo === this) {
|
|
165
|
-
return `already on db ${db}`;
|
|
166
|
-
}
|
|
167
|
-
return `switched to db ${db}`;
|
|
168
|
-
}
|
|
169
|
-
async _listDatabases(opts = {}) {
|
|
170
|
-
const result = await this._serviceProvider.listDatabases('admin', {
|
|
171
|
-
...this._getExplicitlyRequestedReadPref(),
|
|
172
|
-
...opts
|
|
173
|
-
});
|
|
174
|
-
if (!('databases' in result)) {
|
|
175
|
-
const err = new errors_1.MongoshRuntimeError('Got invalid result from "listDatabases"', errors_1.CommonErrors.CommandFailed);
|
|
176
|
-
this._instanceState.messageBus.emit('mongosh:error', err, 'shell-api');
|
|
177
|
-
throw err;
|
|
178
|
-
}
|
|
179
|
-
this._cachedDatabaseNames = result.databases.map((db) => db.name);
|
|
180
|
-
return result;
|
|
181
|
-
}
|
|
182
|
-
async _getDatabaseNamesForCompletion() {
|
|
183
|
-
return await Promise.race([
|
|
184
|
-
(async () => {
|
|
185
|
-
return (await this._listDatabases({ readPreference: 'primaryPreferred' })).databases.map(db => db.name);
|
|
186
|
-
})(),
|
|
187
|
-
(async () => {
|
|
188
|
-
await new Promise(resolve => setTimeout(resolve, 200).unref());
|
|
189
|
-
return this._cachedDatabaseNames;
|
|
190
|
-
})()
|
|
191
|
-
]);
|
|
192
|
-
}
|
|
193
|
-
async getDBs(options = {}) {
|
|
194
|
-
this._emitMongoApiCall('getDBs', { options });
|
|
195
|
-
return await this._listDatabases(options);
|
|
196
|
-
}
|
|
197
|
-
async getDBNames(options = {}) {
|
|
198
|
-
this._emitMongoApiCall('getDBNames', { options });
|
|
199
|
-
return (await this._listDatabases(options)).databases.map(db => db.name);
|
|
200
|
-
}
|
|
201
|
-
async show(cmd, arg) {
|
|
202
|
-
this._instanceState.messageBus.emit('mongosh:show', { method: `show ${cmd}` });
|
|
203
|
-
switch (cmd) {
|
|
204
|
-
case 'databases':
|
|
205
|
-
case 'dbs':
|
|
206
|
-
const result = (await this._listDatabases({ readPreference: 'primaryPreferred', promoteLongs: true })).databases;
|
|
207
|
-
return new result_1.CommandResult('ShowDatabasesResult', result);
|
|
208
|
-
case 'collections':
|
|
209
|
-
case 'tables':
|
|
210
|
-
const collectionNames = await this._instanceState.currentDb._getCollectionNamesWithTypes({ readPreference: 'primaryPreferred', promoteLongs: true });
|
|
211
|
-
return new result_1.CommandResult('ShowCollectionsResult', collectionNames);
|
|
212
|
-
case 'profile':
|
|
213
|
-
const sysprof = this._instanceState.currentDb.getCollection('system.profile');
|
|
214
|
-
const profiles = { count: await sysprof.countDocuments({}) };
|
|
215
|
-
if (profiles.count !== 0) {
|
|
216
|
-
profiles.result = await (await sysprof.find({ millis: { $gt: 0 } }))
|
|
217
|
-
.sort({ $natural: -1 })
|
|
218
|
-
.limit(5)
|
|
219
|
-
.toArray();
|
|
220
|
-
}
|
|
221
|
-
return new result_1.CommandResult('ShowProfileResult', profiles);
|
|
222
|
-
case 'users':
|
|
223
|
-
const users = await this._instanceState.currentDb.getUsers();
|
|
224
|
-
return new result_1.CommandResult('ShowResult', users.users);
|
|
225
|
-
case 'roles':
|
|
226
|
-
const roles = await this._instanceState.currentDb.getRoles({ showBuiltinRoles: true });
|
|
227
|
-
return new result_1.CommandResult('ShowResult', roles.roles);
|
|
228
|
-
case 'log':
|
|
229
|
-
const log = await this._instanceState.currentDb.adminCommand({ getLog: arg || 'global' });
|
|
230
|
-
return new result_1.CommandResult('ShowResult', log.log);
|
|
231
|
-
case 'logs':
|
|
232
|
-
const logs = await this._instanceState.currentDb.adminCommand({ getLog: '*' });
|
|
233
|
-
return new result_1.CommandResult('ShowResult', logs.names);
|
|
234
|
-
default:
|
|
235
|
-
const err = new errors_1.MongoshInvalidInputError(`'${cmd}' is not a valid argument for "show".`, errors_1.CommonErrors.InvalidArgument);
|
|
236
|
-
this._instanceState.messageBus.emit('mongosh:error', err, 'shell-api');
|
|
237
|
-
throw err;
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
async close(force) {
|
|
241
|
-
const index = this._instanceState.mongos.indexOf(this);
|
|
242
|
-
if (index === -1) {
|
|
243
|
-
process.emitWarning(new errors_1.MongoshInternalError(`Closing untracked Mongo instance ${this[enums_1.asPrintable]()}`));
|
|
244
|
-
}
|
|
245
|
-
else {
|
|
246
|
-
this._instanceState.mongos.splice(index, 1);
|
|
247
|
-
}
|
|
248
|
-
await this._serviceProvider.close(force);
|
|
249
|
-
}
|
|
250
|
-
async _suspend() {
|
|
251
|
-
return await this._serviceProvider.suspend();
|
|
252
|
-
}
|
|
253
|
-
getReadPrefMode() {
|
|
254
|
-
return this._serviceProvider.getReadPreference().mode;
|
|
255
|
-
}
|
|
256
|
-
getReadPrefTagSet() {
|
|
257
|
-
return this._serviceProvider.getReadPreference().tags;
|
|
258
|
-
}
|
|
259
|
-
getReadPref() {
|
|
260
|
-
return this._serviceProvider.getReadPreference();
|
|
261
|
-
}
|
|
262
|
-
_getExplicitlyRequestedReadPref() {
|
|
263
|
-
return this._readPreferenceWasExplicitlyRequested ?
|
|
264
|
-
{ readPreference: this.getReadPref() } :
|
|
265
|
-
undefined;
|
|
266
|
-
}
|
|
267
|
-
getReadConcern() {
|
|
268
|
-
try {
|
|
269
|
-
const rc = this._serviceProvider.getReadConcern();
|
|
270
|
-
return rc ? rc.level : undefined;
|
|
271
|
-
}
|
|
272
|
-
catch (_a) {
|
|
273
|
-
throw new errors_1.MongoshInternalError('Error retrieving ReadConcern.');
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
getWriteConcern() {
|
|
277
|
-
try {
|
|
278
|
-
return this._serviceProvider.getWriteConcern();
|
|
279
|
-
}
|
|
280
|
-
catch (_a) {
|
|
281
|
-
throw new errors_1.MongoshInternalError('Error retrieving WriteConcern.');
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
async setReadPref(mode, tagSet, hedgeOptions) {
|
|
285
|
-
await this._serviceProvider.resetConnectionOptions({
|
|
286
|
-
readPreference: this._serviceProvider.readPreferenceFromOptions({
|
|
287
|
-
readPreference: mode,
|
|
288
|
-
readPreferenceTags: tagSet,
|
|
289
|
-
hedge: hedgeOptions
|
|
290
|
-
})
|
|
291
|
-
});
|
|
292
|
-
this._readPreferenceWasExplicitlyRequested = true;
|
|
293
|
-
}
|
|
294
|
-
async setReadConcern(level) {
|
|
295
|
-
await this._serviceProvider.resetConnectionOptions({ readConcern: { level: level } });
|
|
296
|
-
}
|
|
297
|
-
async setWriteConcern(concernOrWValue, wtimeoutMSValue, jValue) {
|
|
298
|
-
const options = {};
|
|
299
|
-
let concern;
|
|
300
|
-
if (typeof concernOrWValue === 'object') {
|
|
301
|
-
if (wtimeoutMSValue !== undefined || jValue !== undefined) {
|
|
302
|
-
throw new errors_1.MongoshInvalidInputError('If concern is given as an object no other arguments must be specified', errors_1.CommonErrors.InvalidArgument);
|
|
303
|
-
}
|
|
304
|
-
concern = concernOrWValue;
|
|
305
|
-
}
|
|
306
|
-
else {
|
|
307
|
-
concern = {};
|
|
308
|
-
if (typeof concernOrWValue !== 'string' && typeof concernOrWValue !== 'number') {
|
|
309
|
-
throw new errors_1.MongoshInvalidInputError(`w value must be a number or string, got: ${typeof concernOrWValue}`, errors_1.CommonErrors.InvalidArgument);
|
|
310
|
-
}
|
|
311
|
-
else if (typeof concernOrWValue === 'number' && concernOrWValue < 0) {
|
|
312
|
-
throw new errors_1.MongoshInvalidInputError(`w value must be equal to or greather than 0, got: ${concernOrWValue}`, errors_1.CommonErrors.InvalidArgument);
|
|
313
|
-
}
|
|
314
|
-
concern.w = concernOrWValue;
|
|
315
|
-
if (wtimeoutMSValue !== undefined) {
|
|
316
|
-
if (typeof wtimeoutMSValue !== 'number') {
|
|
317
|
-
throw new errors_1.MongoshInvalidInputError(`wtimeoutMS value must be a number, got: ${typeof wtimeoutMSValue}`, errors_1.CommonErrors.InvalidArgument);
|
|
318
|
-
}
|
|
319
|
-
else if (wtimeoutMSValue < 0) {
|
|
320
|
-
throw new errors_1.MongoshInvalidInputError(`wtimeoutMS must be equal to or greather than 0, got: ${wtimeoutMSValue}`, errors_1.CommonErrors.InvalidArgument);
|
|
321
|
-
}
|
|
322
|
-
concern.wtimeout = wtimeoutMSValue;
|
|
323
|
-
}
|
|
324
|
-
if (jValue !== undefined) {
|
|
325
|
-
if (typeof jValue !== 'boolean') {
|
|
326
|
-
throw new errors_1.MongoshInvalidInputError(`j value must be a boolean, got: ${typeof jValue}`, errors_1.CommonErrors.InvalidArgument);
|
|
327
|
-
}
|
|
328
|
-
concern.j = jValue;
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
if (concern.w !== undefined) {
|
|
332
|
-
options.w = concern.w;
|
|
333
|
-
}
|
|
334
|
-
if (concern.wtimeout !== undefined) {
|
|
335
|
-
options.wtimeoutMS = concern.wtimeout;
|
|
336
|
-
}
|
|
337
|
-
if (concern.j !== undefined) {
|
|
338
|
-
options.journal = concern.j;
|
|
339
|
-
}
|
|
340
|
-
if (concern.fsync !== undefined) {
|
|
341
|
-
options.journal = !!concern.fsync;
|
|
342
|
-
}
|
|
343
|
-
await this._serviceProvider.resetConnectionOptions(options);
|
|
344
|
-
}
|
|
345
|
-
startSession(options = {}) {
|
|
346
|
-
const allTransactionOptions = [
|
|
347
|
-
'readConcern', 'writeConcern', 'readPreference', 'maxCommitTimeMS'
|
|
348
|
-
];
|
|
349
|
-
function assertAllTransactionOptionsUsed(_options) {
|
|
350
|
-
}
|
|
351
|
-
assertAllTransactionOptionsUsed('');
|
|
352
|
-
const defaultTransactionOptions = {};
|
|
353
|
-
for (const key of allTransactionOptions) {
|
|
354
|
-
if (typeof options[key] !== 'undefined') {
|
|
355
|
-
defaultTransactionOptions[key] = options[key];
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
const allSessionOptions = ['causalConsistency', 'snapshot'];
|
|
359
|
-
function assertAllSessionOptionsUsed(_options) { }
|
|
360
|
-
assertAllSessionOptionsUsed('');
|
|
361
|
-
const driverOptions = {};
|
|
362
|
-
if (Object.keys(defaultTransactionOptions).length > 0) {
|
|
363
|
-
driverOptions.defaultTransactionOptions = defaultTransactionOptions;
|
|
364
|
-
}
|
|
365
|
-
for (const key of allSessionOptions) {
|
|
366
|
-
if (typeof options[key] !== 'undefined') {
|
|
367
|
-
driverOptions[key] = options[key];
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
return new session_1.default(this, driverOptions, this._serviceProvider.startSession(driverOptions));
|
|
371
|
-
}
|
|
372
|
-
setCausalConsistency() {
|
|
373
|
-
throw new errors_1.MongoshUnimplementedError('It is not possible to set causal consistency for an entire connection due to the driver, use startSession({causalConsistency: <>}) instead.', errors_1.CommonErrors.NotImplemented, (0, error_codes_1.blockedByDriverMetadata)('Mongo.setCausalConsistency'));
|
|
374
|
-
}
|
|
375
|
-
isCausalConsistency() {
|
|
376
|
-
throw new errors_1.MongoshUnimplementedError('Causal consistency for drivers is set via Mongo.startSession and can be checked via session.getOptions. The default value is true', errors_1.CommonErrors.NotImplemented, (0, error_codes_1.blockedByDriverMetadata)('Mongo.isCausalConsistency'));
|
|
377
|
-
}
|
|
378
|
-
setSlaveOk() {
|
|
379
|
-
throw new errors_1.MongoshDeprecatedError('Setting slaveOk is deprecated, use setReadPref instead.');
|
|
380
|
-
}
|
|
381
|
-
async setSecondaryOk() {
|
|
382
|
-
await this._instanceState.printDeprecationWarning('.setSecondaryOk() is deprecated. Use .setReadPref("primaryPreferred") instead');
|
|
383
|
-
const currentReadPref = this.getReadPrefMode();
|
|
384
|
-
if (currentReadPref === 'primary') {
|
|
385
|
-
await this._instanceState.shellApi.print('Setting read preference from "primary" to "primaryPreferred"');
|
|
386
|
-
await this.setReadPref('primaryPreferred');
|
|
387
|
-
}
|
|
388
|
-
else {
|
|
389
|
-
await this._instanceState.shellApi.print(`Leaving read preference unchanged (is already "${currentReadPref}")`);
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
async watch(pipeline = [], options = {}) {
|
|
393
|
-
if (!Array.isArray(pipeline)) {
|
|
394
|
-
options = pipeline;
|
|
395
|
-
pipeline = [];
|
|
396
|
-
}
|
|
397
|
-
this._emitMongoApiCall('watch', { pipeline, options });
|
|
398
|
-
const cursor = new change_stream_cursor_1.default(this._serviceProvider.watch(pipeline, options), (0, history_1.redactURICredentials)(this._uri), this);
|
|
399
|
-
if (!options.resumeAfter && !options.startAfter && !options.startAtOperationTime) {
|
|
400
|
-
await cursor.tryNext();
|
|
401
|
-
}
|
|
402
|
-
this._instanceState.currentCursor = cursor;
|
|
403
|
-
return cursor;
|
|
404
|
-
}
|
|
405
|
-
getClientEncryption() {
|
|
406
|
-
if (!this._fleOptions) {
|
|
407
|
-
throw new errors_1.MongoshInvalidInputError('Cannot call getClientEncryption() without field-level encryption options', error_codes_2.ShellApiErrors.NotUsingFLE);
|
|
408
|
-
}
|
|
409
|
-
if (!this._clientEncryption) {
|
|
410
|
-
this._clientEncryption = new field_level_encryption_1.ClientEncryption(this);
|
|
411
|
-
}
|
|
412
|
-
return this._clientEncryption;
|
|
413
|
-
}
|
|
414
|
-
getKeyVault() {
|
|
415
|
-
this._keyVault = new field_level_encryption_1.KeyVault(this.getClientEncryption());
|
|
416
|
-
return this._keyVault;
|
|
417
|
-
}
|
|
418
|
-
};
|
|
419
|
-
__decorate([
|
|
420
|
-
(0, decorators_1.returnType)('Database')
|
|
421
|
-
], Mongo.prototype, "getDB", null);
|
|
422
|
-
__decorate([
|
|
423
|
-
(0, decorators_1.returnType)('Collection')
|
|
424
|
-
], Mongo.prototype, "getCollection", null);
|
|
425
|
-
__decorate([
|
|
426
|
-
decorators_1.returnsPromise,
|
|
427
|
-
(0, decorators_1.apiVersions)([1])
|
|
428
|
-
], Mongo.prototype, "getDBs", null);
|
|
429
|
-
__decorate([
|
|
430
|
-
decorators_1.returnsPromise,
|
|
431
|
-
(0, decorators_1.apiVersions)([1])
|
|
432
|
-
], Mongo.prototype, "getDBNames", null);
|
|
433
|
-
__decorate([
|
|
434
|
-
decorators_1.returnsPromise,
|
|
435
|
-
(0, decorators_1.apiVersions)([1])
|
|
436
|
-
], Mongo.prototype, "show", null);
|
|
437
|
-
__decorate([
|
|
438
|
-
decorators_1.returnsPromise
|
|
439
|
-
], Mongo.prototype, "setReadPref", null);
|
|
440
|
-
__decorate([
|
|
441
|
-
decorators_1.returnsPromise
|
|
442
|
-
], Mongo.prototype, "setReadConcern", null);
|
|
443
|
-
__decorate([
|
|
444
|
-
decorators_1.returnsPromise
|
|
445
|
-
], Mongo.prototype, "setWriteConcern", null);
|
|
446
|
-
__decorate([
|
|
447
|
-
(0, decorators_1.topologies)([enums_1.Topologies.ReplSet])
|
|
448
|
-
], Mongo.prototype, "startSession", null);
|
|
449
|
-
__decorate([
|
|
450
|
-
decorators_1.deprecated
|
|
451
|
-
], Mongo.prototype, "setSlaveOk", null);
|
|
452
|
-
__decorate([
|
|
453
|
-
decorators_1.deprecated,
|
|
454
|
-
decorators_1.returnsPromise
|
|
455
|
-
], Mongo.prototype, "setSecondaryOk", null);
|
|
456
|
-
__decorate([
|
|
457
|
-
(0, decorators_1.serverVersions)(['3.1.0', enums_1.ServerVersions.latest]),
|
|
458
|
-
(0, decorators_1.topologies)([enums_1.Topologies.ReplSet, enums_1.Topologies.Sharded]),
|
|
459
|
-
(0, decorators_1.apiVersions)([1]),
|
|
460
|
-
decorators_1.returnsPromise
|
|
461
|
-
], Mongo.prototype, "watch", null);
|
|
462
|
-
__decorate([
|
|
463
|
-
(0, decorators_1.platforms)([service_provider_core_1.ReplPlatform.CLI]),
|
|
464
|
-
(0, decorators_1.serverVersions)(['4.2.0', enums_1.ServerVersions.latest]),
|
|
465
|
-
(0, decorators_1.returnType)('ClientEncryption')
|
|
466
|
-
], Mongo.prototype, "getClientEncryption", null);
|
|
467
|
-
__decorate([
|
|
468
|
-
(0, decorators_1.platforms)([service_provider_core_1.ReplPlatform.CLI]),
|
|
469
|
-
(0, decorators_1.serverVersions)(['4.2.0', enums_1.ServerVersions.latest]),
|
|
470
|
-
(0, decorators_1.returnType)('KeyVault')
|
|
471
|
-
], Mongo.prototype, "getKeyVault", null);
|
|
472
|
-
Mongo = __decorate([
|
|
473
|
-
decorators_1.shellApiClassDefault,
|
|
474
|
-
(0, decorators_1.classPlatforms)([service_provider_core_1.ReplPlatform.CLI])
|
|
475
|
-
], Mongo);
|
|
476
|
-
exports.default = Mongo;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const errors_1 = require("@qp-mongosh/errors");
|
|
13
|
+
const decorators_1 = require("./decorators");
|
|
14
|
+
const service_provider_core_1 = require("@qp-mongosh/service-provider-core");
|
|
15
|
+
const database_1 = __importDefault(require("./database"));
|
|
16
|
+
const result_1 = require("./result");
|
|
17
|
+
const history_1 = require("@qp-mongosh/history");
|
|
18
|
+
const enums_1 = require("./enums");
|
|
19
|
+
const session_1 = __importDefault(require("./session"));
|
|
20
|
+
const helpers_1 = require("./helpers");
|
|
21
|
+
const change_stream_cursor_1 = __importDefault(require("./change-stream-cursor"));
|
|
22
|
+
const error_codes_1 = require("./error-codes");
|
|
23
|
+
const field_level_encryption_1 = require("./field-level-encryption");
|
|
24
|
+
const error_codes_2 = require("./error-codes");
|
|
25
|
+
let Mongo = class Mongo extends decorators_1.ShellApiClass {
|
|
26
|
+
constructor(instanceState, uri, fleOptions, otherOptions, sp) {
|
|
27
|
+
var _a, _b, _c;
|
|
28
|
+
super();
|
|
29
|
+
this.__serviceProvider = null;
|
|
30
|
+
this._readPreferenceWasExplicitlyRequested = false;
|
|
31
|
+
this._explicitEncryptionOnly = false;
|
|
32
|
+
this._cachedDatabaseNames = [];
|
|
33
|
+
this._instanceState = instanceState;
|
|
34
|
+
this._databases = {};
|
|
35
|
+
if (sp) {
|
|
36
|
+
this.__serviceProvider = sp;
|
|
37
|
+
}
|
|
38
|
+
if (typeof uri === 'string') {
|
|
39
|
+
this._uri = (0, service_provider_core_1.generateUri)({ connectionSpecifier: uri });
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
this._uri = (_b = (_a = sp === null || sp === void 0 ? void 0 : sp.getURI) === null || _a === void 0 ? void 0 : _a.call(sp)) !== null && _b !== void 0 ? _b : (0, service_provider_core_1.generateUri)({ connectionSpecifier: 'mongodb://localhost/' });
|
|
43
|
+
}
|
|
44
|
+
this._readPreferenceWasExplicitlyRequested = /\breadPreference=/.test(this._uri);
|
|
45
|
+
if (fleOptions) {
|
|
46
|
+
if (fleOptions.explicitEncryptionOnly !== undefined) {
|
|
47
|
+
if (fleOptions.schemaMap !== undefined) {
|
|
48
|
+
throw new errors_1.MongoshInvalidInputError('explicitEncryptionOnly and schemaMap are mutually exclusive', errors_1.CommonErrors.InvalidArgument);
|
|
49
|
+
}
|
|
50
|
+
fleOptions = { ...fleOptions };
|
|
51
|
+
this._explicitEncryptionOnly = !!fleOptions.explicitEncryptionOnly;
|
|
52
|
+
delete fleOptions.explicitEncryptionOnly;
|
|
53
|
+
}
|
|
54
|
+
this._fleOptions = (0, helpers_1.processFLEOptions)(fleOptions);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
const spFleOptions = (_c = sp === null || sp === void 0 ? void 0 : sp.getFleOptions) === null || _c === void 0 ? void 0 : _c.call(sp);
|
|
58
|
+
if (spFleOptions) {
|
|
59
|
+
this._fleOptions = spFleOptions;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (otherOptions === null || otherOptions === void 0 ? void 0 : otherOptions.api) {
|
|
63
|
+
if (typeof otherOptions.api === 'string') {
|
|
64
|
+
this._apiOptions = { version: otherOptions.api };
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
this._apiOptions = otherOptions.api;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
get _serviceProvider() {
|
|
72
|
+
if (this.__serviceProvider === null) {
|
|
73
|
+
throw new errors_1.MongoshInternalError('No ServiceProvider available for this mongo', error_codes_2.ShellApiErrors.NotConnected);
|
|
74
|
+
}
|
|
75
|
+
return this.__serviceProvider;
|
|
76
|
+
}
|
|
77
|
+
set _serviceProvider(sp) {
|
|
78
|
+
this.__serviceProvider = sp;
|
|
79
|
+
}
|
|
80
|
+
async _displayBatchSize() {
|
|
81
|
+
var _a;
|
|
82
|
+
return (_a = this._instanceState.displayBatchSizeFromDBQuery) !== null && _a !== void 0 ? _a : await this._instanceState.shellApi.config.get('displayBatchSize');
|
|
83
|
+
}
|
|
84
|
+
[enums_1.asPrintable]() {
|
|
85
|
+
return (0, history_1.redactURICredentials)(this._uri);
|
|
86
|
+
}
|
|
87
|
+
_emitMongoApiCall(methodName, methodArguments = {}) {
|
|
88
|
+
this._instanceState.emitApiCallWithArgs({
|
|
89
|
+
method: methodName,
|
|
90
|
+
class: 'Mongo',
|
|
91
|
+
uri: this._uri,
|
|
92
|
+
arguments: methodArguments
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
async connect(user, pwd) {
|
|
96
|
+
var _a, _b, _c, _d, _e, _f;
|
|
97
|
+
const mongoClientOptions = user || pwd ? {
|
|
98
|
+
auth: { username: user, password: pwd }
|
|
99
|
+
} : {};
|
|
100
|
+
if (this._fleOptions && !this._explicitEncryptionOnly) {
|
|
101
|
+
const extraOptions = {
|
|
102
|
+
...((_a = this._fleOptions.extraOptions) !== null && _a !== void 0 ? _a : {}),
|
|
103
|
+
...((_d = await ((_c = (_b = this._instanceState.evaluationListener) === null || _b === void 0 ? void 0 : _b.startMongocryptd) === null || _c === void 0 ? void 0 : _c.call(_b))) !== null && _d !== void 0 ? _d : {})
|
|
104
|
+
};
|
|
105
|
+
mongoClientOptions.autoEncryption = { ...this._fleOptions, extraOptions };
|
|
106
|
+
}
|
|
107
|
+
if (this._apiOptions) {
|
|
108
|
+
mongoClientOptions.serverApi = this._apiOptions;
|
|
109
|
+
}
|
|
110
|
+
const parentProvider = this._instanceState.initialServiceProvider;
|
|
111
|
+
try {
|
|
112
|
+
this.__serviceProvider = await parentProvider.getNewConnection(this._uri, mongoClientOptions);
|
|
113
|
+
}
|
|
114
|
+
catch (e) {
|
|
115
|
+
if ((e === null || e === void 0 ? void 0 : e.name) === 'MongoServerSelectionError' &&
|
|
116
|
+
((_f = (_e = parentProvider.getRawClient()) === null || _e === void 0 ? void 0 : _e.options) === null || _f === void 0 ? void 0 : _f.tls) &&
|
|
117
|
+
!this._uri.match(/\b(ssl|tls)=/)) {
|
|
118
|
+
e.message += ' (is ?tls=true missing from the connection string?)';
|
|
119
|
+
}
|
|
120
|
+
throw e;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
_getDb(name) {
|
|
124
|
+
(0, helpers_1.assertArgsDefinedType)([name], ['string']);
|
|
125
|
+
if (!(0, helpers_1.isValidDatabaseName)(name)) {
|
|
126
|
+
throw new errors_1.MongoshInvalidInputError(`Invalid database name: ${name}`, errors_1.CommonErrors.InvalidArgument);
|
|
127
|
+
}
|
|
128
|
+
if (!(name in this._databases)) {
|
|
129
|
+
this._databases[name] = new database_1.default(this, name);
|
|
130
|
+
}
|
|
131
|
+
return this._databases[name];
|
|
132
|
+
}
|
|
133
|
+
getDB(db) {
|
|
134
|
+
(0, helpers_1.assertArgsDefinedType)([db], ['string'], 'Mongo.getDB');
|
|
135
|
+
this._instanceState.messageBus.emit('mongosh:getDB', { db });
|
|
136
|
+
return this._getDb(db);
|
|
137
|
+
}
|
|
138
|
+
getCollection(name) {
|
|
139
|
+
var _a, _b;
|
|
140
|
+
(0, helpers_1.assertArgsDefinedType)([name], ['string']);
|
|
141
|
+
const { db, coll } = (_b = (_a = name.match(/^(?<db>[^.]+)\.(?<coll>.+)$/)) === null || _a === void 0 ? void 0 : _a.groups) !== null && _b !== void 0 ? _b : {};
|
|
142
|
+
if (!db || !coll) {
|
|
143
|
+
throw new errors_1.MongoshInvalidInputError('Collection must be of the format <db>.<collection>', errors_1.CommonErrors.InvalidArgument);
|
|
144
|
+
}
|
|
145
|
+
return this._getDb(db).getCollection(coll);
|
|
146
|
+
}
|
|
147
|
+
use(db) {
|
|
148
|
+
var _a;
|
|
149
|
+
(0, helpers_1.assertArgsDefinedType)([db], ['string'], 'Mongo.use');
|
|
150
|
+
this._instanceState.messageBus.emit('mongosh:use', { db });
|
|
151
|
+
let previousDbName;
|
|
152
|
+
let previousDbMongo;
|
|
153
|
+
try {
|
|
154
|
+
const previousDb = this._instanceState.context.db;
|
|
155
|
+
previousDbName = (_a = previousDb === null || previousDb === void 0 ? void 0 : previousDb.getName) === null || _a === void 0 ? void 0 : _a.call(previousDb);
|
|
156
|
+
previousDbMongo = previousDb === null || previousDb === void 0 ? void 0 : previousDb._mongo;
|
|
157
|
+
}
|
|
158
|
+
catch (e) {
|
|
159
|
+
if ((e === null || e === void 0 ? void 0 : e.code) !== error_codes_2.ShellApiErrors.NotConnected) {
|
|
160
|
+
throw e;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
this._instanceState.context.db = this._getDb(db);
|
|
164
|
+
if (db === previousDbName && previousDbMongo === this) {
|
|
165
|
+
return `already on db ${db}`;
|
|
166
|
+
}
|
|
167
|
+
return `switched to db ${db}`;
|
|
168
|
+
}
|
|
169
|
+
async _listDatabases(opts = {}) {
|
|
170
|
+
const result = await this._serviceProvider.listDatabases('admin', {
|
|
171
|
+
...this._getExplicitlyRequestedReadPref(),
|
|
172
|
+
...opts
|
|
173
|
+
});
|
|
174
|
+
if (!('databases' in result)) {
|
|
175
|
+
const err = new errors_1.MongoshRuntimeError('Got invalid result from "listDatabases"', errors_1.CommonErrors.CommandFailed);
|
|
176
|
+
this._instanceState.messageBus.emit('mongosh:error', err, 'shell-api');
|
|
177
|
+
throw err;
|
|
178
|
+
}
|
|
179
|
+
this._cachedDatabaseNames = result.databases.map((db) => db.name);
|
|
180
|
+
return result;
|
|
181
|
+
}
|
|
182
|
+
async _getDatabaseNamesForCompletion() {
|
|
183
|
+
return await Promise.race([
|
|
184
|
+
(async () => {
|
|
185
|
+
return (await this._listDatabases({ readPreference: 'primaryPreferred' })).databases.map(db => db.name);
|
|
186
|
+
})(),
|
|
187
|
+
(async () => {
|
|
188
|
+
await new Promise(resolve => setTimeout(resolve, 200).unref());
|
|
189
|
+
return this._cachedDatabaseNames;
|
|
190
|
+
})()
|
|
191
|
+
]);
|
|
192
|
+
}
|
|
193
|
+
async getDBs(options = {}) {
|
|
194
|
+
this._emitMongoApiCall('getDBs', { options });
|
|
195
|
+
return await this._listDatabases(options);
|
|
196
|
+
}
|
|
197
|
+
async getDBNames(options = {}) {
|
|
198
|
+
this._emitMongoApiCall('getDBNames', { options });
|
|
199
|
+
return (await this._listDatabases(options)).databases.map(db => db.name);
|
|
200
|
+
}
|
|
201
|
+
async show(cmd, arg) {
|
|
202
|
+
this._instanceState.messageBus.emit('mongosh:show', { method: `show ${cmd}` });
|
|
203
|
+
switch (cmd) {
|
|
204
|
+
case 'databases':
|
|
205
|
+
case 'dbs':
|
|
206
|
+
const result = (await this._listDatabases({ readPreference: 'primaryPreferred', promoteLongs: true })).databases;
|
|
207
|
+
return new result_1.CommandResult('ShowDatabasesResult', result);
|
|
208
|
+
case 'collections':
|
|
209
|
+
case 'tables':
|
|
210
|
+
const collectionNames = await this._instanceState.currentDb._getCollectionNamesWithTypes({ readPreference: 'primaryPreferred', promoteLongs: true });
|
|
211
|
+
return new result_1.CommandResult('ShowCollectionsResult', collectionNames);
|
|
212
|
+
case 'profile':
|
|
213
|
+
const sysprof = this._instanceState.currentDb.getCollection('system.profile');
|
|
214
|
+
const profiles = { count: await sysprof.countDocuments({}) };
|
|
215
|
+
if (profiles.count !== 0) {
|
|
216
|
+
profiles.result = await (await sysprof.find({ millis: { $gt: 0 } }))
|
|
217
|
+
.sort({ $natural: -1 })
|
|
218
|
+
.limit(5)
|
|
219
|
+
.toArray();
|
|
220
|
+
}
|
|
221
|
+
return new result_1.CommandResult('ShowProfileResult', profiles);
|
|
222
|
+
case 'users':
|
|
223
|
+
const users = await this._instanceState.currentDb.getUsers();
|
|
224
|
+
return new result_1.CommandResult('ShowResult', users.users);
|
|
225
|
+
case 'roles':
|
|
226
|
+
const roles = await this._instanceState.currentDb.getRoles({ showBuiltinRoles: true });
|
|
227
|
+
return new result_1.CommandResult('ShowResult', roles.roles);
|
|
228
|
+
case 'log':
|
|
229
|
+
const log = await this._instanceState.currentDb.adminCommand({ getLog: arg || 'global' });
|
|
230
|
+
return new result_1.CommandResult('ShowResult', log.log);
|
|
231
|
+
case 'logs':
|
|
232
|
+
const logs = await this._instanceState.currentDb.adminCommand({ getLog: '*' });
|
|
233
|
+
return new result_1.CommandResult('ShowResult', logs.names);
|
|
234
|
+
default:
|
|
235
|
+
const err = new errors_1.MongoshInvalidInputError(`'${cmd}' is not a valid argument for "show".`, errors_1.CommonErrors.InvalidArgument);
|
|
236
|
+
this._instanceState.messageBus.emit('mongosh:error', err, 'shell-api');
|
|
237
|
+
throw err;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
async close(force) {
|
|
241
|
+
const index = this._instanceState.mongos.indexOf(this);
|
|
242
|
+
if (index === -1) {
|
|
243
|
+
process.emitWarning(new errors_1.MongoshInternalError(`Closing untracked Mongo instance ${this[enums_1.asPrintable]()}`));
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
this._instanceState.mongos.splice(index, 1);
|
|
247
|
+
}
|
|
248
|
+
await this._serviceProvider.close(force);
|
|
249
|
+
}
|
|
250
|
+
async _suspend() {
|
|
251
|
+
return await this._serviceProvider.suspend();
|
|
252
|
+
}
|
|
253
|
+
getReadPrefMode() {
|
|
254
|
+
return this._serviceProvider.getReadPreference().mode;
|
|
255
|
+
}
|
|
256
|
+
getReadPrefTagSet() {
|
|
257
|
+
return this._serviceProvider.getReadPreference().tags;
|
|
258
|
+
}
|
|
259
|
+
getReadPref() {
|
|
260
|
+
return this._serviceProvider.getReadPreference();
|
|
261
|
+
}
|
|
262
|
+
_getExplicitlyRequestedReadPref() {
|
|
263
|
+
return this._readPreferenceWasExplicitlyRequested ?
|
|
264
|
+
{ readPreference: this.getReadPref() } :
|
|
265
|
+
undefined;
|
|
266
|
+
}
|
|
267
|
+
getReadConcern() {
|
|
268
|
+
try {
|
|
269
|
+
const rc = this._serviceProvider.getReadConcern();
|
|
270
|
+
return rc ? rc.level : undefined;
|
|
271
|
+
}
|
|
272
|
+
catch (_a) {
|
|
273
|
+
throw new errors_1.MongoshInternalError('Error retrieving ReadConcern.');
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
getWriteConcern() {
|
|
277
|
+
try {
|
|
278
|
+
return this._serviceProvider.getWriteConcern();
|
|
279
|
+
}
|
|
280
|
+
catch (_a) {
|
|
281
|
+
throw new errors_1.MongoshInternalError('Error retrieving WriteConcern.');
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
async setReadPref(mode, tagSet, hedgeOptions) {
|
|
285
|
+
await this._serviceProvider.resetConnectionOptions({
|
|
286
|
+
readPreference: this._serviceProvider.readPreferenceFromOptions({
|
|
287
|
+
readPreference: mode,
|
|
288
|
+
readPreferenceTags: tagSet,
|
|
289
|
+
hedge: hedgeOptions
|
|
290
|
+
})
|
|
291
|
+
});
|
|
292
|
+
this._readPreferenceWasExplicitlyRequested = true;
|
|
293
|
+
}
|
|
294
|
+
async setReadConcern(level) {
|
|
295
|
+
await this._serviceProvider.resetConnectionOptions({ readConcern: { level: level } });
|
|
296
|
+
}
|
|
297
|
+
async setWriteConcern(concernOrWValue, wtimeoutMSValue, jValue) {
|
|
298
|
+
const options = {};
|
|
299
|
+
let concern;
|
|
300
|
+
if (typeof concernOrWValue === 'object') {
|
|
301
|
+
if (wtimeoutMSValue !== undefined || jValue !== undefined) {
|
|
302
|
+
throw new errors_1.MongoshInvalidInputError('If concern is given as an object no other arguments must be specified', errors_1.CommonErrors.InvalidArgument);
|
|
303
|
+
}
|
|
304
|
+
concern = concernOrWValue;
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
concern = {};
|
|
308
|
+
if (typeof concernOrWValue !== 'string' && typeof concernOrWValue !== 'number') {
|
|
309
|
+
throw new errors_1.MongoshInvalidInputError(`w value must be a number or string, got: ${typeof concernOrWValue}`, errors_1.CommonErrors.InvalidArgument);
|
|
310
|
+
}
|
|
311
|
+
else if (typeof concernOrWValue === 'number' && concernOrWValue < 0) {
|
|
312
|
+
throw new errors_1.MongoshInvalidInputError(`w value must be equal to or greather than 0, got: ${concernOrWValue}`, errors_1.CommonErrors.InvalidArgument);
|
|
313
|
+
}
|
|
314
|
+
concern.w = concernOrWValue;
|
|
315
|
+
if (wtimeoutMSValue !== undefined) {
|
|
316
|
+
if (typeof wtimeoutMSValue !== 'number') {
|
|
317
|
+
throw new errors_1.MongoshInvalidInputError(`wtimeoutMS value must be a number, got: ${typeof wtimeoutMSValue}`, errors_1.CommonErrors.InvalidArgument);
|
|
318
|
+
}
|
|
319
|
+
else if (wtimeoutMSValue < 0) {
|
|
320
|
+
throw new errors_1.MongoshInvalidInputError(`wtimeoutMS must be equal to or greather than 0, got: ${wtimeoutMSValue}`, errors_1.CommonErrors.InvalidArgument);
|
|
321
|
+
}
|
|
322
|
+
concern.wtimeout = wtimeoutMSValue;
|
|
323
|
+
}
|
|
324
|
+
if (jValue !== undefined) {
|
|
325
|
+
if (typeof jValue !== 'boolean') {
|
|
326
|
+
throw new errors_1.MongoshInvalidInputError(`j value must be a boolean, got: ${typeof jValue}`, errors_1.CommonErrors.InvalidArgument);
|
|
327
|
+
}
|
|
328
|
+
concern.j = jValue;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
if (concern.w !== undefined) {
|
|
332
|
+
options.w = concern.w;
|
|
333
|
+
}
|
|
334
|
+
if (concern.wtimeout !== undefined) {
|
|
335
|
+
options.wtimeoutMS = concern.wtimeout;
|
|
336
|
+
}
|
|
337
|
+
if (concern.j !== undefined) {
|
|
338
|
+
options.journal = concern.j;
|
|
339
|
+
}
|
|
340
|
+
if (concern.fsync !== undefined) {
|
|
341
|
+
options.journal = !!concern.fsync;
|
|
342
|
+
}
|
|
343
|
+
await this._serviceProvider.resetConnectionOptions(options);
|
|
344
|
+
}
|
|
345
|
+
startSession(options = {}) {
|
|
346
|
+
const allTransactionOptions = [
|
|
347
|
+
'readConcern', 'writeConcern', 'readPreference', 'maxCommitTimeMS'
|
|
348
|
+
];
|
|
349
|
+
function assertAllTransactionOptionsUsed(_options) {
|
|
350
|
+
}
|
|
351
|
+
assertAllTransactionOptionsUsed('');
|
|
352
|
+
const defaultTransactionOptions = {};
|
|
353
|
+
for (const key of allTransactionOptions) {
|
|
354
|
+
if (typeof options[key] !== 'undefined') {
|
|
355
|
+
defaultTransactionOptions[key] = options[key];
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
const allSessionOptions = ['causalConsistency', 'snapshot'];
|
|
359
|
+
function assertAllSessionOptionsUsed(_options) { }
|
|
360
|
+
assertAllSessionOptionsUsed('');
|
|
361
|
+
const driverOptions = {};
|
|
362
|
+
if (Object.keys(defaultTransactionOptions).length > 0) {
|
|
363
|
+
driverOptions.defaultTransactionOptions = defaultTransactionOptions;
|
|
364
|
+
}
|
|
365
|
+
for (const key of allSessionOptions) {
|
|
366
|
+
if (typeof options[key] !== 'undefined') {
|
|
367
|
+
driverOptions[key] = options[key];
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
return new session_1.default(this, driverOptions, this._serviceProvider.startSession(driverOptions));
|
|
371
|
+
}
|
|
372
|
+
setCausalConsistency() {
|
|
373
|
+
throw new errors_1.MongoshUnimplementedError('It is not possible to set causal consistency for an entire connection due to the driver, use startSession({causalConsistency: <>}) instead.', errors_1.CommonErrors.NotImplemented, (0, error_codes_1.blockedByDriverMetadata)('Mongo.setCausalConsistency'));
|
|
374
|
+
}
|
|
375
|
+
isCausalConsistency() {
|
|
376
|
+
throw new errors_1.MongoshUnimplementedError('Causal consistency for drivers is set via Mongo.startSession and can be checked via session.getOptions. The default value is true', errors_1.CommonErrors.NotImplemented, (0, error_codes_1.blockedByDriverMetadata)('Mongo.isCausalConsistency'));
|
|
377
|
+
}
|
|
378
|
+
setSlaveOk() {
|
|
379
|
+
throw new errors_1.MongoshDeprecatedError('Setting slaveOk is deprecated, use setReadPref instead.');
|
|
380
|
+
}
|
|
381
|
+
async setSecondaryOk() {
|
|
382
|
+
await this._instanceState.printDeprecationWarning('.setSecondaryOk() is deprecated. Use .setReadPref("primaryPreferred") instead');
|
|
383
|
+
const currentReadPref = this.getReadPrefMode();
|
|
384
|
+
if (currentReadPref === 'primary') {
|
|
385
|
+
await this._instanceState.shellApi.print('Setting read preference from "primary" to "primaryPreferred"');
|
|
386
|
+
await this.setReadPref('primaryPreferred');
|
|
387
|
+
}
|
|
388
|
+
else {
|
|
389
|
+
await this._instanceState.shellApi.print(`Leaving read preference unchanged (is already "${currentReadPref}")`);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
async watch(pipeline = [], options = {}) {
|
|
393
|
+
if (!Array.isArray(pipeline)) {
|
|
394
|
+
options = pipeline;
|
|
395
|
+
pipeline = [];
|
|
396
|
+
}
|
|
397
|
+
this._emitMongoApiCall('watch', { pipeline, options });
|
|
398
|
+
const cursor = new change_stream_cursor_1.default(this._serviceProvider.watch(pipeline, options), (0, history_1.redactURICredentials)(this._uri), this);
|
|
399
|
+
if (!options.resumeAfter && !options.startAfter && !options.startAtOperationTime) {
|
|
400
|
+
await cursor.tryNext();
|
|
401
|
+
}
|
|
402
|
+
this._instanceState.currentCursor = cursor;
|
|
403
|
+
return cursor;
|
|
404
|
+
}
|
|
405
|
+
getClientEncryption() {
|
|
406
|
+
if (!this._fleOptions) {
|
|
407
|
+
throw new errors_1.MongoshInvalidInputError('Cannot call getClientEncryption() without field-level encryption options', error_codes_2.ShellApiErrors.NotUsingFLE);
|
|
408
|
+
}
|
|
409
|
+
if (!this._clientEncryption) {
|
|
410
|
+
this._clientEncryption = new field_level_encryption_1.ClientEncryption(this);
|
|
411
|
+
}
|
|
412
|
+
return this._clientEncryption;
|
|
413
|
+
}
|
|
414
|
+
getKeyVault() {
|
|
415
|
+
this._keyVault = new field_level_encryption_1.KeyVault(this.getClientEncryption());
|
|
416
|
+
return this._keyVault;
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
__decorate([
|
|
420
|
+
(0, decorators_1.returnType)('Database')
|
|
421
|
+
], Mongo.prototype, "getDB", null);
|
|
422
|
+
__decorate([
|
|
423
|
+
(0, decorators_1.returnType)('Collection')
|
|
424
|
+
], Mongo.prototype, "getCollection", null);
|
|
425
|
+
__decorate([
|
|
426
|
+
decorators_1.returnsPromise,
|
|
427
|
+
(0, decorators_1.apiVersions)([1])
|
|
428
|
+
], Mongo.prototype, "getDBs", null);
|
|
429
|
+
__decorate([
|
|
430
|
+
decorators_1.returnsPromise,
|
|
431
|
+
(0, decorators_1.apiVersions)([1])
|
|
432
|
+
], Mongo.prototype, "getDBNames", null);
|
|
433
|
+
__decorate([
|
|
434
|
+
decorators_1.returnsPromise,
|
|
435
|
+
(0, decorators_1.apiVersions)([1])
|
|
436
|
+
], Mongo.prototype, "show", null);
|
|
437
|
+
__decorate([
|
|
438
|
+
decorators_1.returnsPromise
|
|
439
|
+
], Mongo.prototype, "setReadPref", null);
|
|
440
|
+
__decorate([
|
|
441
|
+
decorators_1.returnsPromise
|
|
442
|
+
], Mongo.prototype, "setReadConcern", null);
|
|
443
|
+
__decorate([
|
|
444
|
+
decorators_1.returnsPromise
|
|
445
|
+
], Mongo.prototype, "setWriteConcern", null);
|
|
446
|
+
__decorate([
|
|
447
|
+
(0, decorators_1.topologies)([enums_1.Topologies.ReplSet])
|
|
448
|
+
], Mongo.prototype, "startSession", null);
|
|
449
|
+
__decorate([
|
|
450
|
+
decorators_1.deprecated
|
|
451
|
+
], Mongo.prototype, "setSlaveOk", null);
|
|
452
|
+
__decorate([
|
|
453
|
+
decorators_1.deprecated,
|
|
454
|
+
decorators_1.returnsPromise
|
|
455
|
+
], Mongo.prototype, "setSecondaryOk", null);
|
|
456
|
+
__decorate([
|
|
457
|
+
(0, decorators_1.serverVersions)(['3.1.0', enums_1.ServerVersions.latest]),
|
|
458
|
+
(0, decorators_1.topologies)([enums_1.Topologies.ReplSet, enums_1.Topologies.Sharded]),
|
|
459
|
+
(0, decorators_1.apiVersions)([1]),
|
|
460
|
+
decorators_1.returnsPromise
|
|
461
|
+
], Mongo.prototype, "watch", null);
|
|
462
|
+
__decorate([
|
|
463
|
+
(0, decorators_1.platforms)([service_provider_core_1.ReplPlatform.CLI]),
|
|
464
|
+
(0, decorators_1.serverVersions)(['4.2.0', enums_1.ServerVersions.latest]),
|
|
465
|
+
(0, decorators_1.returnType)('ClientEncryption')
|
|
466
|
+
], Mongo.prototype, "getClientEncryption", null);
|
|
467
|
+
__decorate([
|
|
468
|
+
(0, decorators_1.platforms)([service_provider_core_1.ReplPlatform.CLI]),
|
|
469
|
+
(0, decorators_1.serverVersions)(['4.2.0', enums_1.ServerVersions.latest]),
|
|
470
|
+
(0, decorators_1.returnType)('KeyVault')
|
|
471
|
+
], Mongo.prototype, "getKeyVault", null);
|
|
472
|
+
Mongo = __decorate([
|
|
473
|
+
decorators_1.shellApiClassDefault,
|
|
474
|
+
(0, decorators_1.classPlatforms)([service_provider_core_1.ReplPlatform.CLI])
|
|
475
|
+
], Mongo);
|
|
476
|
+
exports.default = Mongo;
|
|
477
477
|
//# sourceMappingURL=mongo.js.map
|