1
- import crypto from 'crypto' ;
2
1
import completer from '@mongosh/autocomplete' ;
3
2
import { MongoshInternalError , MongoshWarning } from '@mongosh/errors' ;
4
3
import { changeHistory } from '@mongosh/history' ;
@@ -8,7 +7,6 @@ import type {
8
7
} from '@mongosh/service-provider-core' ;
9
8
import type {
10
9
EvaluationListener ,
11
- Mongo ,
12
10
OnLoadResult ,
13
11
ShellCliOptions ,
14
12
} from '@mongosh/shell-api' ;
@@ -438,35 +436,20 @@ class MongoshNodeRepl implements EvaluationListener {
438
436
this . runtimeState ( ) . context . history = history ;
439
437
}
440
438
441
- // TODO: probably better to have this on instanceState
442
- public _getMongoByConnectionId (
443
- instanceState : ShellInstanceState ,
444
- connectionId : string
445
- ) : Mongo {
446
- for ( const mongo of instanceState . mongos ) {
447
- if ( connectionIdFromURI ( mongo . getURI ( ) ) === connectionId ) {
448
- return mongo ;
449
- }
450
- }
451
- throw new Error ( `mongo with connection id ${ connectionId } not found` ) ;
452
- }
453
-
454
439
public getAutocompletionContext (
455
440
instanceState : ShellInstanceState
456
441
) : AutocompletionContext {
457
442
return {
458
443
currentDatabaseAndConnection : ( ) => {
459
444
return {
460
- connectionId : connectionIdFromURI (
461
- instanceState . currentDb . getMongo ( ) . getURI ( )
462
- ) ,
445
+ connectionId : instanceState . currentDb . getMongo ( ) . getConnectionId ( ) ,
463
446
databaseName : instanceState . currentDb . getName ( ) ,
464
447
} ;
465
448
} ,
466
449
databasesForConnection : async (
467
450
connectionId : string
468
451
) : Promise < string [ ] > => {
469
- const mongo = this . _getMongoByConnectionId ( instanceState , connectionId ) ;
452
+ const mongo = instanceState . getMongoByConnectionId ( connectionId ) ;
470
453
try {
471
454
const dbNames = await mongo . _getDatabaseNamesForCompletion ( ) ;
472
455
return dbNames . filter (
@@ -485,7 +468,7 @@ class MongoshNodeRepl implements EvaluationListener {
485
468
connectionId : string ,
486
469
databaseName : string
487
470
) : Promise < string [ ] > => {
488
- const mongo = this . _getMongoByConnectionId ( instanceState , connectionId ) ;
471
+ const mongo = instanceState . getMongoByConnectionId ( connectionId ) ;
489
472
try {
490
473
const collectionNames = await mongo
491
474
. _getDb ( databaseName )
@@ -507,7 +490,7 @@ class MongoshNodeRepl implements EvaluationListener {
507
490
databaseName : string ,
508
491
collectionName : string
509
492
) : Promise < JSONSchema > => {
510
- const mongo = this . _getMongoByConnectionId ( instanceState , connectionId ) ;
493
+ const mongo = instanceState . getMongoByConnectionId ( connectionId ) ;
511
494
const docs = await mongo
512
495
. _getDb ( databaseName )
513
496
. getCollection ( collectionName )
@@ -1435,12 +1418,4 @@ async function enterAsynchronousExecutionForPrompt(): Promise<void> {
1435
1418
await new Promise ( setImmediate ) ;
1436
1419
}
1437
1420
1438
- function connectionIdFromURI ( uri : string ) : string {
1439
- // turn the uri into something we can safely use as part of a "filename"
1440
- // inside autocomplete
1441
- const hash = crypto . createHash ( 'sha256' ) ;
1442
- hash . update ( uri ) ;
1443
- return hash . digest ( 'hex' ) ;
1444
- }
1445
-
1446
1421
export default MongoshNodeRepl ;
0 commit comments