feat: improve database module and apply untranslated color to UI
This commit is contained in:
+53
-38
@@ -1,16 +1,29 @@
|
||||
import mysql from "mysql2/promise";
|
||||
import {getConfig} from "@/lib/config";
|
||||
|
||||
// 获取配置文件中的数据链接
|
||||
const dataBaseConfig = getConfig().db
|
||||
|
||||
const pool = mysql.createPool({
|
||||
host: process.env.DB_HOST || "127.0.0.1",
|
||||
port: parseInt(process.env.DB_PORT || "3306"),
|
||||
user: process.env.DB_USER || "root",
|
||||
password: process.env.DB_PASSWORD || "",
|
||||
database: process.env.DB_NAME || "recipe_tools",
|
||||
waitForConnections: true,
|
||||
connectionLimit: 10,
|
||||
queueLimit: 0,
|
||||
host: dataBaseConfig.host || "127.0.0.1",
|
||||
port: dataBaseConfig.port || 3306,
|
||||
user: dataBaseConfig.user || "root",
|
||||
password: dataBaseConfig.password || "123456",
|
||||
database: dataBaseConfig.database || "recipe_tools",
|
||||
waitForConnections: true,
|
||||
connectionLimit: 10,
|
||||
queueLimit: 0,
|
||||
});
|
||||
|
||||
console.log({
|
||||
host: dataBaseConfig.host || "127.0.0.1",
|
||||
port: dataBaseConfig.port || 3306,
|
||||
user: dataBaseConfig.user || "root",
|
||||
password: dataBaseConfig.password || "123456",
|
||||
database: dataBaseConfig.database || "recipe_tools",
|
||||
waitForConnections: true,
|
||||
connectionLimit: 10,
|
||||
queueLimit: 0,
|
||||
})
|
||||
export default pool;
|
||||
|
||||
/**
|
||||
@@ -19,20 +32,20 @@ export default pool;
|
||||
* @returns 翻译结果或 null
|
||||
*/
|
||||
export async function getTranslation(chineseName: string): Promise<string | null> {
|
||||
try {
|
||||
const [rows] = await pool.execute<mysql.RowDataPacket[]>(
|
||||
"SELECT english FROM words WHERE word = ? LIMIT 1",
|
||||
[chineseName]
|
||||
);
|
||||
try {
|
||||
const [rows] = await pool.execute<mysql.RowDataPacket[]>(
|
||||
"SELECT english FROM words WHERE word = ? LIMIT 1",
|
||||
[chineseName]
|
||||
);
|
||||
|
||||
if (rows.length > 0 && rows[0].english) {
|
||||
return rows[0].english as string;
|
||||
if (rows.length > 0 && rows[0].english) {
|
||||
return rows[0].english as string;
|
||||
}
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.error("Database query error:", error);
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.error("Database query error:", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -41,27 +54,29 @@ export async function getTranslation(chineseName: string): Promise<string | null
|
||||
* @returns 翻译结果映射 { chineseName: englishName }
|
||||
*/
|
||||
export async function getTranslations(
|
||||
chineseNames: string[]
|
||||
chineseNames: string[]
|
||||
): Promise<Map<string, string>> {
|
||||
const result = new Map<string, string>();
|
||||
const result = new Map<string, string>();
|
||||
|
||||
if (chineseNames.length === 0) return result;
|
||||
if (chineseNames.length === 0) return result;
|
||||
|
||||
try {
|
||||
const placeholders = chineseNames.map(() => "?").join(",");
|
||||
const [rows] = await pool.execute<mysql.RowDataPacket[]>(
|
||||
`SELECT word, english FROM words WHERE word IN (${placeholders})`,
|
||||
chineseNames
|
||||
);
|
||||
try {
|
||||
const placeholders = chineseNames.map(() => "?").join(",");
|
||||
const [rows] = await pool.execute<mysql.RowDataPacket[]>(
|
||||
`SELECT word, english
|
||||
FROM words
|
||||
WHERE word IN (${placeholders})`,
|
||||
chineseNames
|
||||
);
|
||||
|
||||
for (const row of rows) {
|
||||
if (row.word && row.english) {
|
||||
result.set(row.word as string, row.english as string);
|
||||
}
|
||||
for (const row of rows) {
|
||||
if (row.word && row.english) {
|
||||
result.set(row.word as string, row.english as string);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Database batch query error:", error);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Database batch query error:", error);
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user