Skip to content

Commit f3f9fe7

Browse files
authored
Merge pull request #134 from jamebal/jmal
使用MongoDB URL里的数据库名称
2 parents 3fb11c6 + 9737fd1 commit f3f9fe7

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

service/src/storage/mongo.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import type { ChatOptions, Config, UsageResponse } from './model'
66
dotenv.config()
77

88
const url = process.env.MONGODB_URL
9+
const parsedUrl = new URL(url)
10+
const dbName = (parsedUrl.pathname && parsedUrl.pathname !== '/') ? parsedUrl.pathname.substring(1) : 'chatgpt'
911
const client = new MongoClient(url)
10-
const chatCol = client.db('chatgpt').collection('chat')
11-
const roomCol = client.db('chatgpt').collection('chat_room')
12-
const userCol = client.db('chatgpt').collection('user')
13-
const configCol = client.db('chatgpt').collection('config')
14-
const usageCol = client.db('chatgpt').collection('chat_usage')
12+
const chatCol = client.db(dbName).collection('chat')
13+
const roomCol = client.db(dbName).collection('chat_room')
14+
const userCol = client.db(dbName).collection('user')
15+
const configCol = client.db(dbName).collection('config')
16+
const usageCol = client.db(dbName).collection('chat_usage')
1517

1618
/**
1719
* 插入聊天信息

0 commit comments

Comments
 (0)