@@ -10,37 +10,60 @@ import type { Session } from "$lib/types/Session";
10
10
import type { Assistant } from "$lib/types/Assistant" ;
11
11
import type { Report } from "$lib/types/Report" ;
12
12
import type { ConversationStats } from "$lib/types/ConversationStats" ;
13
+ import type { MigrationResult } from "$lib/types/MigrationResult" ;
14
+ import type { Semaphore } from "$lib/types/Semaphore" ;
13
15
14
16
if ( ! MONGODB_URL ) {
15
17
throw new Error (
16
18
"Please specify the MONGODB_URL environment variable inside .env.local. Set it to mongodb://localhost:27017 if you are running MongoDB locally, or to a MongoDB Atlas free instance for example."
17
19
) ;
18
20
}
21
+ export const CONVERSATION_STATS_COLLECTION = "conversations.stats" ;
19
22
20
23
const client = new MongoClient ( MONGODB_URL , {
21
24
directConnection : MONGODB_DIRECT_CONNECTION === "true" ,
22
25
} ) ;
23
26
24
27
export const connectPromise = client . connect ( ) . catch ( console . error ) ;
25
28
26
- const db = client . db ( MONGODB_DB_NAME + ( import . meta. env . MODE === "test" ? "-test" : "" ) ) ;
29
+ export function getCollections ( mongoClient : MongoClient ) {
30
+ const db = mongoClient . db ( MONGODB_DB_NAME + ( import . meta. env . MODE === "test" ? "-test" : "" ) ) ;
27
31
28
- export const CONVERSATION_STATS_COLLECTION = "conversations.stats" ;
32
+ const conversations = db . collection < Conversation > ( "conversations" ) ;
33
+ const conversationStats = db . collection < ConversationStats > ( CONVERSATION_STATS_COLLECTION ) ;
34
+ const assistants = db . collection < Assistant > ( "assistants" ) ;
35
+ const reports = db . collection < Report > ( "reports" ) ;
36
+ const sharedConversations = db . collection < SharedConversation > ( "sharedConversations" ) ;
37
+ const abortedGenerations = db . collection < AbortedGeneration > ( "abortedGenerations" ) ;
38
+ const settings = db . collection < Settings > ( "settings" ) ;
39
+ const users = db . collection < User > ( "users" ) ;
40
+ const sessions = db . collection < Session > ( "sessions" ) ;
41
+ const messageEvents = db . collection < MessageEvent > ( "messageEvents" ) ;
42
+ const bucket = new GridFSBucket ( db , { bucketName : "files" } ) ;
43
+ const migrationResults = db . collection < MigrationResult > ( "migrationResults" ) ;
44
+ const semaphores = db . collection < Semaphore > ( "semaphores" ) ;
45
+
46
+ return {
47
+ conversations,
48
+ conversationStats,
49
+ assistants,
50
+ reports,
51
+ sharedConversations,
52
+ abortedGenerations,
53
+ settings,
54
+ users,
55
+ sessions,
56
+ messageEvents,
57
+ bucket,
58
+ migrationResults,
59
+ semaphores,
60
+ } ;
61
+ }
62
+ const db = client . db ( MONGODB_DB_NAME + ( import . meta. env . MODE === "test" ? "-test" : "" ) ) ;
29
63
30
- const conversations = db . collection < Conversation > ( "conversations" ) ;
31
- const conversationStats = db . collection < ConversationStats > ( CONVERSATION_STATS_COLLECTION ) ;
32
- const assistants = db . collection < Assistant > ( "assistants" ) ;
33
- const reports = db . collection < Report > ( "reports" ) ;
34
- const sharedConversations = db . collection < SharedConversation > ( "sharedConversations" ) ;
35
- const abortedGenerations = db . collection < AbortedGeneration > ( "abortedGenerations" ) ;
36
- const settings = db . collection < Settings > ( "settings" ) ;
37
- const users = db . collection < User > ( "users" ) ;
38
- const sessions = db . collection < Session > ( "sessions" ) ;
39
- const messageEvents = db . collection < MessageEvent > ( "messageEvents" ) ;
40
- const bucket = new GridFSBucket ( db , { bucketName : "files" } ) ;
64
+ const collections = getCollections ( client ) ;
41
65
42
- export { client , db } ;
43
- export const collections = {
66
+ const {
44
67
conversations,
45
68
conversationStats,
46
69
assistants,
@@ -51,8 +74,10 @@ export const collections = {
51
74
users,
52
75
sessions,
53
76
messageEvents,
54
- bucket,
55
- } ;
77
+ semaphores,
78
+ } = collections ;
79
+
80
+ export { client , db , collections } ;
56
81
57
82
client . on ( "open" , ( ) => {
58
83
conversations
@@ -120,4 +145,8 @@ client.on("open", () => {
120
145
assistants . createIndex ( { searchTokens : 1 } ) . catch ( console . error ) ;
121
146
reports . createIndex ( { assistantId : 1 } ) . catch ( console . error ) ;
122
147
reports . createIndex ( { createdBy : 1 , assistantId : 1 } ) . catch ( console . error ) ;
148
+
149
+ // Unique index for semaphore and migration results
150
+ semaphores . createIndex ( { key : 1 } , { unique : true } ) . catch ( console . error ) ;
151
+ semaphores . createIndex ( { createdAt : 1 } , { expireAfterSeconds : 60 } ) . catch ( console . error ) ;
123
152
} ) ;
0 commit comments