@@ -66,12 +66,14 @@ export class Database {
66
66
} ) ;
67
67
}
68
68
69
- this . client . connect ( ) . catch ( ( err ) => {
69
+ try {
70
+ await this . client . connect ( ) ;
71
+ this . client . db ( config . MONGODB_DB_NAME + ( import . meta. env . MODE === "test" ? "-test" : "" ) ) ;
72
+ this . client . on ( "open" , ( ) => this . initDatabase ( ) ) ;
73
+ } catch ( err ) {
70
74
logger . error ( err , "Connection error" ) ;
71
75
process . exit ( 1 ) ;
72
- } ) ;
73
- this . client . db ( config . MONGODB_DB_NAME + ( import . meta. env . MODE === "test" ? "-test" : "" ) ) ;
74
- this . client . on ( "open" , ( ) => this . initDatabase ( ) ) ;
76
+ }
75
77
76
78
// Disconnect DB on exit
77
79
onExit ( async ( ) => {
@@ -198,22 +200,6 @@ export class Database {
198
200
conversations
199
201
. createIndex ( { "messages.createdAt" : 1 } , { sparse : true } )
200
202
. catch ( ( e ) => logger . error ( e ) ) ;
201
-
202
- // Text index for searching conversation titles and message content
203
- conversations
204
- . createIndex (
205
- {
206
- userId : 1 ,
207
- sessionId : 1 ,
208
- title : "text" ,
209
- "messages.content" : "text" ,
210
- } ,
211
- {
212
- default_language : "en" ,
213
- }
214
- )
215
- . catch ( ( e ) => logger . error ( e ) ) ;
216
-
217
203
// Unique index for stats
218
204
conversationStats
219
205
. createIndex (
@@ -311,9 +297,19 @@ export let collections: ReturnType<typeof Database.prototype.getCollections>;
311
297
312
298
export const ready = ( async ( ) => {
313
299
if ( ! building ) {
314
- await Database . getInstance ( ) ;
315
- collections = await Database . getInstance ( ) . then ( ( db ) => db . getCollections ( ) ) ;
300
+ const db = await Database . getInstance ( ) ;
301
+ collections = db . getCollections ( ) ;
316
302
} else {
317
303
collections = { } as unknown as ReturnType < typeof Database . prototype . getCollections > ;
318
304
}
319
305
} ) ( ) ;
306
+
307
+ export async function getCollectionsEarly ( ) : Promise <
308
+ ReturnType < typeof Database . prototype . getCollections >
309
+ > {
310
+ await ready ;
311
+ if ( ! collections ) {
312
+ throw new Error ( "Database not initialized" ) ;
313
+ }
314
+ return collections ;
315
+ }
0 commit comments