a249fb038c
- Add Word document parsing and translation export - Add database configuration management - Add AI/LLM translation configuration - Add translation vocabulary management - Add export settings (colors, page orientation, headers) - Separate database and AI configurations - Add debounced search to prevent database overload
68 lines
1.7 KiB
TypeScript
68 lines
1.7 KiB
TypeScript
import mysql from "mysql2/promise";
|
|
|
|
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,
|
|
});
|
|
|
|
export default pool;
|
|
|
|
/**
|
|
* 根据中文菜名查询英文翻译
|
|
* @param chineseName 中文菜名
|
|
* @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]
|
|
);
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 批量查询翻译
|
|
* @param chineseNames 中文菜名数组
|
|
* @returns 翻译结果映射 { chineseName: englishName }
|
|
*/
|
|
export async function getTranslations(
|
|
chineseNames: string[]
|
|
): Promise<Map<string, string>> {
|
|
const result = new Map<string, string>();
|
|
|
|
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
|
|
);
|
|
|
|
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);
|
|
}
|
|
|
|
return result;
|
|
}
|