@@ -52,7 +52,7 @@ class MongoRAG {
5252 maxResults : config . search ?. maxResults || 5
5353 }
5454 } ;
55-
55+
5656
5757 this . client = null ;
5858 this . indexManager = null ;
@@ -66,8 +66,17 @@ class MongoRAG {
6666 async connect ( ) {
6767 if ( ! this . client ) {
6868 try {
69- log ( 'Connecting to MongoDB...' ) ;
69+ log ( 'Initializing MongoDB client ...' ) ;
7070 this . client = new MongoClient ( this . config . mongoUrl ) ;
71+ } catch ( error ) {
72+ console . error ( 'Error initializing MongoDB client:' , error ) ;
73+ throw error ;
74+ }
75+ }
76+
77+ if ( ! this . client . topology || ! this . client . topology . isConnected ( ) ) {
78+ try {
79+ log ( 'Connecting to MongoDB...' ) ;
7180 await this . client . connect ( ) ;
7281 log ( 'Connected to MongoDB' ) ;
7382 } catch ( error ) {
@@ -77,6 +86,7 @@ class MongoRAG {
7786 }
7887 }
7988
89+
8090 /**
8191 * Retrieves a MongoDB collection reference.
8292 * @param {string } [database] - Database name, defaults to the configured database.
@@ -139,8 +149,8 @@ class MongoRAG {
139149 console . log ( '[DEBUG] Using vector search index:' , this . config . indexName ) ;
140150
141151 const indexManager = new IndexManager ( col , {
142- indexName : this . config . indexName ,
143- embeddingFieldPath : this . config . embeddingFieldPath ,
152+ indexName : this . config . indexName ,
153+ embeddingFieldPath : this . config . embeddingFieldPath ,
144154 dimensions : this . config . embedding . dimensions
145155 } ) ;
146156
@@ -207,7 +217,7 @@ class MongoRAG {
207217 const [ embedding ] = await this . embeddingProvider . getEmbeddings ( [ text ] ) ;
208218 return embedding ;
209219 }
210-
220+
211221
212222 /**
213223 * Closes the MongoDB connection.
0 commit comments