1- import type { Filter , WithId } from 'mongodb'
1+ import type { Collection , Filter , WithId } from 'mongodb'
22import type {
33 AdvancedConfig ,
44 BuiltInPrompt ,
@@ -17,40 +17,20 @@ import { md5 } from '../utils/security'
1717import { getCacheConfig } from './config'
1818import { ChatInfo , ChatRoom , ChatUsage , Status , UserConfig , UserInfo , UserRole } from './model'
1919
20- const url = process . env . MONGODB_URL
21-
2220let client : MongoClient
2321let dbName : string
2422let isInitialized = false
2523
26- try {
27- client = new MongoClient ( url )
28- const parsedUrl = new URL ( url )
29- dbName = ( parsedUrl . pathname && parsedUrl . pathname !== '/' ) ? parsedUrl . pathname . substring ( 1 ) : 'chatgpt'
30- }
31- catch ( e ) {
32- globalThis . console . error ( 'MongoDB url invalid. please ensure set valid env MONGODB_URL.' , e . message )
33- process . exit ( 1 )
34- }
35-
36- const chatCol = client . db ( dbName ) . collection < ChatInfo > ( 'chat' )
37- const roomCol = client . db ( dbName ) . collection < ChatRoom > ( 'chat_room' )
38- const userCol = client . db ( dbName ) . collection < UserInfo > ( 'user' )
39- const configCol = client . db ( dbName ) . collection < Config > ( 'config' )
40- const usageCol = client . db ( dbName ) . collection < ChatUsage > ( 'chat_usage' )
41- const keyCol = client . db ( dbName ) . collection < KeyConfig > ( 'key_config' )
42- const builtInPromptCol = client . db ( dbName ) . collection < BuiltInPrompt > ( 'built_in_prompt' )
43- const userPromptCol = client . db ( dbName ) . collection < UserPrompt > ( 'user_prompt' )
44- // 新增兑换券的数据库
45- // {
46- // "_id": { "$comment": "Mongodb系统自动" , "$type": "ObjectId" },
47- // "cardno": { "$comment": "卡号(可以用csv导入)", "$type": "String" },
48- // "amount": { "$comment": "卡号对应的额度", "$type": "Int32" },
49- // "redeemed": { "$comment": "标记是否已被兑换,0|1表示false|true,目前类型为Int是为图方便和测试考虑以后识别泄漏啥的(多次被兑换)", "$type": "Int32" },
50- // "redeemed_by": { "$comment": "执行成功兑换的用户", "$type": "String" },
51- // "redeemed_date": { "$comment": "执行成功兑换的日期,考虑通用性选择了String类型,由new Date().toLocaleString()产生", "$type": "String" }
52- // }
53- const redeemCol = client . db ( dbName ) . collection < GiftCard > ( 'giftcards' )
24+ // Collections - initialized in initializeMongoDB()
25+ let chatCol : Collection < ChatInfo >
26+ let roomCol : Collection < ChatRoom >
27+ let userCol : Collection < UserInfo >
28+ let configCol : Collection < Config >
29+ let usageCol : Collection < ChatUsage >
30+ let keyCol : Collection < KeyConfig >
31+ let builtInPromptCol : Collection < BuiltInPrompt >
32+ let userPromptCol : Collection < UserPrompt >
33+ let redeemCol : Collection < GiftCard >
5434
5535/**
5636 * Initialize all database indexes
@@ -187,10 +167,32 @@ export async function initializeMongoDB() {
187167 }
188168
189169 try {
170+ const url = process . env . MONGODB_URL
171+ if ( ! url ) {
172+ throw new Error ( 'MONGODB_URL environment variable is not set' )
173+ }
174+
175+ // Initialize MongoDB client
176+ client = new MongoClient ( url )
177+ const parsedUrl = new URL ( url )
178+ dbName = ( parsedUrl . pathname && parsedUrl . pathname !== '/' ) ? parsedUrl . pathname . substring ( 1 ) : 'chatgpt'
179+
190180 // Connect to MongoDB
191181 await client . connect ( )
192182 globalThis . console . log ( '✓ MongoDB connected successfully' )
193183
184+ // Initialize collections
185+ const db = client . db ( dbName )
186+ chatCol = db . collection < ChatInfo > ( 'chat' )
187+ roomCol = db . collection < ChatRoom > ( 'chat_room' )
188+ userCol = db . collection < UserInfo > ( 'user' )
189+ configCol = db . collection < Config > ( 'config' )
190+ usageCol = db . collection < ChatUsage > ( 'chat_usage' )
191+ keyCol = db . collection < KeyConfig > ( 'key_config' )
192+ builtInPromptCol = db . collection < BuiltInPrompt > ( 'built_in_prompt' )
193+ userPromptCol = db . collection < UserPrompt > ( 'user_prompt' )
194+ redeemCol = db . collection < GiftCard > ( 'giftcards' )
195+
194196 // Initialize indexes
195197 await initializeIndexes ( )
196198
0 commit comments