-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.js
30 lines (24 loc) · 861 Bytes
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { MongoClient } from 'mongodb';
import dotenv from 'dotenv';
dotenv.config();
const uri = process.env.MONGODB_URI;
const client = new MongoClient(uri);
async function connectToDatabase() {
if (!client.topology || !client.topology.isConnected()) {
await client.connect();
}
return client.db('chatbot');
}
async function fetchTextData() {
const db = await connectToDatabase();
const collection = db.collection('initialText');
const textEntry = await collection.findOne();
return textEntry ? textEntry.text : null;
}
async function chatInteraction(userMessage, aiResponse) {
const db = await connectToDatabase();
const collection = db.collection('chatMessages');
const timestamp = new Date();
await collection.insertOne({ userMessage, aiResponse, timestamp });
}
export { connectToDatabase, fetchTextData, chatInteraction };