改為依 db 參數切換 (use x_db)

This commit is contained in:
2026-04-17 23:38:26 +08:00
parent da72e6f958
commit eaef697ef2
3 changed files with 30 additions and 9 deletions
+14 -3
View File
@@ -51,10 +51,16 @@ app.post(ormRoutes, async (req, res, next) => {
try {
const params = ormService.prepareOrmPayload(req);
// 增加一個 log 觀察是哪個路徑進來的
logger.info(`Incoming Request: ${req.url} -> SP: ${req.params.name}`);
// logger.info(`Calling SP: ${req.params.name} by User: ${req.body.token || 'Guest'}`);
// logger.info(`Incoming Request: ${req.url} -> SP: ${req.params.name}`);
logger.info(`Incoming Request: ${req.url} -> DB: ${req.params.dbname} -> SP: ${req.params.name}`);
const result = await ormService.executeSp(
req.params.name,
params,
req.params.dsNo,
req.params.dbname
);
const result = await ormService.executeSp(req.params.name, params, req.params.dsNo);
res.json(result);
} catch (err) {
logger.error(`API Error on ${req.params.name}: ${err.message}`);
@@ -73,6 +79,11 @@ app.post('/upload/:dbname/:typeid', upload.single('file'), (req, res) => {
res.json({ status: 0, data: { value: fileUrl, url: fileUrl }, msg: 'success' });
});
// 20260409 test IIS 反向代理
app.get('/ping', (req, res) => {
res.send('pong! IIS Reverse Proxy Success !!');
});
// 全域錯誤處理
app.use((err, req, res, next) => {
res.status(err.status || 500).json({ status: err.status || 500, msg: err.message });
+15 -6
View File
@@ -13,8 +13,9 @@ class OrmService {
* @param {string} spName - SP 名稱
* @param {Array} params - 參數陣列
* @param {number|string} dsNo - 回傳格式代號 (2, 3, 8, 20)
* @param {string} dbName - 從路由取得的資料庫名稱 (req.params.dbname)
*/
async executeSp(spName, params, dsNo) {
async executeSp(spName, params, dsNo, dbName) {
if (spName === "orm_api") {
spName = "orm_api_v2"
}
@@ -24,9 +25,15 @@ class OrmService {
// MariaDB SP 呼叫語法: CALL sp_name(?, ?, ...)
const placeholders = safeParams.map(() => '?').join(',');
const sql = (safeParams.length > 0 || spName != "zen_null")
? `CALL ${spName}(${placeholders})`
: `CALL ${spName}()`; // 無參數時補上括號
// --- 動態切換 DB 邏輯 ---
// 使用 `dbName`.`spName` 語法確保在正確的資料庫執行
// 使用反引號 (`) 包裹名稱以防止特殊字元或關鍵字衝突
const fullSpName = dbName ? `\`${dbName}\`.\`${spName}\`` : `\`${spName}\``;
const sql = (safeParams.length > 0 || spName !== "zen_null")
? `CALL ${fullSpName}(${placeholders})`
: `CALL ${fullSpName}()`;
// debug & hard code
/*if (spName === "zen_null") {
@@ -47,7 +54,8 @@ class OrmService {
const startTime = Date.now();
// 將參數轉為字串方便顯示,若是長字串(JSON)則截斷或完整呈現
const paramsDebug = JSON.stringify(params);
logger.info(`[SQL EXECUTE] Query: ${sql} | Params: ${paramsDebug}`);
// logger.info(`[SQL EXECUTE] Query: ${sql} | Params: ${paramsDebug}`);
logger.info(`[SQL EXECUTE] DB: ${dbName} | Query: ${sql} | Params: ${paramsDebug}`);
// --------------------
try {
@@ -58,7 +66,8 @@ class OrmService {
const status = resultSets[0] && resultSets[0][0] ? resultSets[0][0] : { code: 0, msg: 'Success', recno: 0 };
const dataSet = resultSets[1] || [];
logger.info(`[SQL SUCCESS] SP: ${spName} | Time: ${duration}ms | Status: ${status.code}`);
// logger.info(`[SQL SUCCESS] SP: ${spName} | Time: ${duration}ms | Status: ${status.code}`);
logger.info(`[SQL SUCCESS] DB: ${dbName} | SP: ${spName} | Time: ${duration}ms | Status: ${status.code}`);
// 根據 dsNo 格式化回傳內容給前端 amis
const dsNoInt = parseInt(dsNo);
+1
View File
@@ -6,6 +6,7 @@ const crypto = require('crypto'); // 用於生成隨機字串
const storage = multer.diskStorage({
destination: (req, file, cb) => {
const { dbname, typeid } = req.params;
// cannot change db name for difference corp 20260417
const uploadPath = path.join(__dirname, `../../public/upload/${dbname}/${typeid}`);
fs.mkdirSync(uploadPath, { recursive: true });
cb(null, uploadPath);