@@ -15,16 +15,6 @@ export async function ingestData(config, options) {
15
15
16
16
const isDevelopment = process . env . NODE_ENV === 'development' || process . env . NODE_ENV === 'test' ;
17
17
18
- // Validate MongoDB URI
19
- if ( ! config . mongoUrl || typeof config . mongoUrl !== 'string' ) {
20
- throw new Error ( 'Invalid MongoDB URI: URI must be a non-empty string' ) ;
21
- }
22
-
23
- // Ensure URI starts with mongodb:// or mongodb+srv://
24
- if ( ! config . mongoUrl . startsWith ( 'mongodb://' ) && ! config . mongoUrl . startsWith ( 'mongodb+srv://' ) ) {
25
- throw new Error ( 'Invalid MongoDB URI: URI must start with mongodb:// or mongodb+srv://' ) ;
26
- }
27
-
28
18
// Test MongoDB connection before proceeding
29
19
try {
30
20
if ( isDevelopment ) {
@@ -40,62 +30,31 @@ export async function ingestData(config, options) {
40
30
throw new Error ( `MongoDB connection test failed: ${ error . message } ` ) ;
41
31
}
42
32
43
- // Restructure the config to match expected format
33
+ // Create the RAG configuration using the exact structure expected by MongoRAG
44
34
const ragConfig = {
45
- // MongoDB connection details at top level
46
- connectionString : config . mongoUrl , // Try this instead of nested mongodb object
47
- databaseName : config . database , // Use full names at top level
48
- collectionName : config . collection ,
49
-
50
- // Keep the rest of the config
35
+ mongoUrl : config . mongoUrl ,
36
+ database : config . database ,
37
+ collection : config . collection ,
51
38
embedding : {
52
- provider : config . embedding ?. provider || config . provider ,
39
+ provider : config . embedding . provider ,
53
40
apiKey : config . apiKey ,
54
- model : config . embedding ?. model || config . model ,
55
- dimensions : config . embedding ?. dimensions || config . dimensions ,
56
- baseUrl : config . embedding ?. baseUrl || config . baseUrl ,
57
- batchSize : config . embedding ?. batchSize || 100
58
- } ,
59
- search : {
60
- maxResults : config . search ?. maxResults || 5 ,
61
- minScore : config . search ?. minScore || 0.7
41
+ model : config . embedding . model ,
42
+ dimensions : config . embedding . dimensions ,
43
+ baseUrl : config . embedding . baseUrl ,
44
+ batchSize : config . embedding . batchSize
62
45
} ,
63
- indexName : config . indexName ,
64
-
65
- // Add standard MongoDB options
66
- mongodbOptions : {
67
- useNewUrlParser : true ,
68
- useUnifiedTopology : true
69
- }
46
+ indexName : config . indexName
70
47
} ;
71
48
72
- // Remove the mongodb nested object structure
73
- if ( isDevelopment ) {
74
- console . log ( 'Attempting to connect to MongoDB...' ) ;
75
- console . log ( 'MongoDB URI:' , ragConfig . connectionString ) ;
76
- }
77
-
78
- // Set environment variables from config if they're not already set
79
- if ( ! process . env . EMBEDDING_API_KEY && config . apiKey ) {
80
- process . env . EMBEDDING_API_KEY = config . apiKey ;
81
- }
82
- if ( ! process . env . EMBEDDING_PROVIDER && ( config . embedding ?. provider || config . provider ) ) {
83
- process . env . EMBEDDING_PROVIDER = config . embedding ?. provider || config . provider ;
84
- }
85
- if ( ! process . env . EMBEDDING_MODEL && ( config . embedding ?. model || config . model ) ) {
86
- process . env . EMBEDDING_MODEL = config . embedding ?. model || config . model ;
87
- }
88
-
89
49
try {
90
50
if ( isDevelopment ) {
91
- console . log ( 'Creating MongoRAG instance with config ...' ) ;
51
+ console . log ( 'Creating MongoRAG instance...' ) ;
92
52
}
93
53
94
54
const rag = new MongoRAG ( ragConfig ) ;
95
55
96
56
if ( isDevelopment ) {
97
- console . log ( 'Attempting to connect to MongoDB...' ) ;
98
- console . log ( 'MongoDB URI:' , ragConfig . connectionString ) ;
57
+ console . log ( 'Connecting to MongoDB...' ) ;
99
58
}
100
59
101
60
await rag . connect ( ) ;
@@ -128,7 +87,7 @@ export async function ingestData(config, options) {
128
87
const chunkedDocs = [ ] ;
129
88
for ( const doc of documents ) {
130
89
if ( isDevelopment ) {
131
- console . log ( chalk . blue ( `📄 Chunking document: ${ doc . metadata . filename } ` ) ) ;
90
+ console . log ( chalk . blue ( `📄 Chunking document: ${ doc . metadata ? .filename } ` ) ) ;
132
91
}
133
92
const chunks = chunker . chunkDocument ( doc ) ;
134
93
chunkedDocs . push ( ...chunks ) ;
@@ -150,6 +109,7 @@ export async function ingestData(config, options) {
150
109
} ) ;
151
110
152
111
console . log ( chalk . green ( `✅ Successfully ingested ${ result . processed } documents!` ) ) ;
112
+ await rag . close ( ) ;
153
113
return result ;
154
114
} catch ( error ) {
155
115
console . error ( chalk . red ( '❌ Ingestion failed:' ) , error . message ) ;
@@ -169,8 +129,12 @@ async function processDirectory(dirPath, options) {
169
129
const subDocs = await processDirectory ( filePath , options ) ;
170
130
documents . push ( ...subDocs ) ;
171
131
} else if ( stat . isFile ( ) ) {
172
- const docs = await processFile ( filePath , options ) ;
173
- documents . push ( ...docs ) ;
132
+ try {
133
+ const docs = await processFile ( filePath , options ) ;
134
+ documents . push ( ...docs ) ;
135
+ } catch ( error ) {
136
+ console . warn ( chalk . yellow ( `⚠️ Warning: Failed to process ${ filePath } : ${ error . message } ` ) ) ;
137
+ }
174
138
}
175
139
}
176
140
@@ -194,7 +158,7 @@ async function processFile(filePath, options) {
194
158
195
159
if ( isDevelopment ) {
196
160
console . log ( chalk . blue ( `📄 Processed ${ filePath } ` ) ) ;
197
- if ( doc . metadata . processingFailed ) {
161
+ if ( doc . metadata ? .processingFailed ) {
198
162
console . warn ( chalk . yellow ( `⚠️ Warning: ${ doc . metadata . error } ` ) ) ;
199
163
}
200
164
}
0 commit comments