1- import crypto from 'crypto' ;
21import completer from '@mongosh/autocomplete' ;
32import { MongoshInternalError , MongoshWarning } from '@mongosh/errors' ;
43import { changeHistory } from '@mongosh/history' ;
@@ -8,7 +7,6 @@ import type {
87} from '@mongosh/service-provider-core' ;
98import type {
109 EvaluationListener ,
11- Mongo ,
1210 OnLoadResult ,
1311 ShellCliOptions ,
1412} from '@mongosh/shell-api' ;
@@ -438,35 +436,20 @@ class MongoshNodeRepl implements EvaluationListener {
438436 this . runtimeState ( ) . context . history = history ;
439437 }
440438
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-
454439 public getAutocompletionContext (
455440 instanceState : ShellInstanceState
456441 ) : AutocompletionContext {
457442 return {
458443 currentDatabaseAndConnection : ( ) => {
459444 return {
460- connectionId : connectionIdFromURI (
461- instanceState . currentDb . getMongo ( ) . getURI ( )
462- ) ,
445+ connectionId : instanceState . currentDb . getMongo ( ) . getConnectionId ( ) ,
463446 databaseName : instanceState . currentDb . getName ( ) ,
464447 } ;
465448 } ,
466449 databasesForConnection : async (
467450 connectionId : string
468451 ) : Promise < string [ ] > => {
469- const mongo = this . _getMongoByConnectionId ( instanceState , connectionId ) ;
452+ const mongo = instanceState . getMongoByConnectionId ( connectionId ) ;
470453 try {
471454 const dbNames = await mongo . _getDatabaseNamesForCompletion ( ) ;
472455 return dbNames . filter (
@@ -485,7 +468,7 @@ class MongoshNodeRepl implements EvaluationListener {
485468 connectionId : string ,
486469 databaseName : string
487470 ) : Promise < string [ ] > => {
488- const mongo = this . _getMongoByConnectionId ( instanceState , connectionId ) ;
471+ const mongo = instanceState . getMongoByConnectionId ( connectionId ) ;
489472 try {
490473 const collectionNames = await mongo
491474 . _getDb ( databaseName )
@@ -507,7 +490,7 @@ class MongoshNodeRepl implements EvaluationListener {
507490 databaseName : string ,
508491 collectionName : string
509492 ) : Promise < JSONSchema > => {
510- const mongo = this . _getMongoByConnectionId ( instanceState , connectionId ) ;
493+ const mongo = instanceState . getMongoByConnectionId ( connectionId ) ;
511494 const docs = await mongo
512495 . _getDb ( databaseName )
513496 . getCollection ( collectionName )
@@ -1435,12 +1418,4 @@ async function enterAsynchronousExecutionForPrompt(): Promise<void> {
14351418 await new Promise ( setImmediate ) ;
14361419}
14371420
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-
14461421export default MongoshNodeRepl ;
0 commit comments