Ver código fonte

数据库新增查询

S0025136190 1 ano atrás
pai
commit
1594b7bdf0
3 arquivos alterados com 89 adições e 1 exclusões
  1. 29 0
      app/api/getData/route.ts
  2. 58 0
      app/api/insertData/route.ts
  3. 2 1
      package.json

+ 29 - 0
app/api/getData/route.ts

@@ -0,0 +1,29 @@
+import { NextResponse } from 'next/server'
+import mysql from 'mysql2/promise'
+ 
+// 创建全局的 MySQL 连接池
+const pool = mysql.createPool({
+  connectionLimit: 10,
+  host: '192.168.3.89', // 服务器地址
+  user: 'root',
+  password: '123456', // 密码
+  database: 'chat',
+})
+ 
+export async function GET(request:any) {
+  try {
+    // 从连接池中获取连接
+    const connection = await pool.getConnection()
+ 
+    // 执行 MySQL 查询
+    const [rows, fields] = await connection.query('SELECT he.id,he.dialog_name,de.id did,de.dialog_id,de.type,de.content FROM dialog he left join dialog_detail de on he.id = de.dialog_id')
+ 
+    // 释放连接回连接池
+    connection.release()
+ 
+    return NextResponse.json({ data: rows }, { status: 200 })
+  } catch (error) {
+    console.error('Error:', error)
+    return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 })
+  }
+}

+ 58 - 0
app/api/insertData/route.ts

@@ -0,0 +1,58 @@
+import { NextResponse } from 'next/server'
+import mysql from 'mysql2/promise'
+ 
+// 创建全局的 MySQL 连接池
+const pool = mysql.createPool({
+  connectionLimit: 10,
+  host: '192.168.3.89', // 服务器地址
+  user: 'root',
+  password: '123456', // 密码
+  database: 'chat',
+})
+
+interface Request{
+    id: string;
+    messages: {
+        id: string;
+        date: string;
+        role: "system" | "user" | "assistant";
+        content: string;
+    }[];
+}
+ 
+export async function InsertData(req:Request,res:any) {
+  try {
+    // 从连接池中获取连接
+    const connection = await pool.getConnection()
+    const id = req.id;
+    const messages = req.messages;
+    
+    const [rows, fields] = await connection.query('SELECT * FROM dialog where id = ?',[id]);
+
+    if(!rows) {
+      // 执行 MySQL 添加头表
+      await connection.execute('INSERT INTO dialog (id, create_time) VALUES (?,?)',[req, new Date])
+    }
+
+    messages.forEach(async function(message) {
+      const detailId = message.id;
+      const role = message.role;
+      const createDate = message.date;
+      const content = message.content;
+      console.log(message);
+      const [detailRows, fields] = await connection.query('SELECT * FROM dialog_detail where id = ?',[detailId]);
+      if(!detailRows) {
+        // 执行 MySQL 添加头表
+        await connection.execute('INSERT INTO dialog_detail (id,dialog_id,type,content,create_by, create_time) VALUES (?, ?, ?)',[detailId,id,role,content,role,createDate])
+      }
+    });
+ 
+    // 释放连接回连接池
+    connection.release()
+ 
+    return res.status(201).json({ message: 'Data inserted successfully' });
+  } catch (error) {
+    console.error('Error:', error)
+    return res.status(500).json({ error: 'Internal Server Error' });
+  }
+}

+ 2 - 1
package.json

@@ -30,6 +30,7 @@
     "html-to-image": "^1.11.11",
     "lodash-es": "^4.17.21",
     "mermaid": "^10.6.1",
+    "mysql2": "^3.11.0",
     "nanoid": "^5.0.3",
     "next": "^14.1.1",
     "node-fetch": "^3.3.1",
@@ -68,4 +69,4 @@
     "lint-staged/yaml": "^2.2.2"
   },
   "packageManager": "yarn@1.22.19"
-}
+}