-template
This commit is contained in:
@@ -1,95 +1,95 @@
|
||||
import { connection } from '../config/db.js'
|
||||
import dotenv from 'dotenv'
|
||||
import { sendError } from '../utils/response.js'
|
||||
dotenv.config()
|
||||
|
||||
// ===================================================
|
||||
// 🧩 Internal DevHint System
|
||||
// ===================================================
|
||||
function devhint(level, location, message, extra = null) {
|
||||
const isEnabled = process.env.DEVHINT === 'true'
|
||||
const currentLevel = parseInt(process.env.DEVHINT_LEVEL || '1', 10)
|
||||
export class GeneralService {
|
||||
devhint(level, location, message, extra = null) {
|
||||
const isEnabled = process.env.DEVHINT === 'true'
|
||||
const currentLevel = parseInt(process.env.DEVHINT_LEVEL || '1', 10)
|
||||
if (!isEnabled || level > currentLevel) return
|
||||
|
||||
if (!isEnabled || level > currentLevel) return
|
||||
const timestamp = new Date().toISOString()
|
||||
const prefix = `🧩 [DEVHINT:${location}]`
|
||||
const formatted = `${prefix} → ${message} (${timestamp})`
|
||||
|
||||
const timestamp = new Date().toISOString()
|
||||
const prefix = `🧩 [DEVHINT:${location}]`
|
||||
const formatted = `${prefix} → ${message} (${timestamp})`
|
||||
if (extra) console.log(formatted, '\n', extra)
|
||||
else console.log(formatted)
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ===================================================
|
||||
// ✅ executeQueryConditions()
|
||||
// ===================================================
|
||||
export async function executeQueryConditions(database, baseQuery, conditions = {}) {
|
||||
devhint(2, 'generalservice.js', 'executeQueryConditions() start')
|
||||
|
||||
let whereClauses = []
|
||||
let params = []
|
||||
let idx = 1
|
||||
|
||||
for (const [key, value] of Object.entries(conditions)) {
|
||||
if (value === undefined || value === null || value === '') continue
|
||||
|
||||
const match = String(value).match(/^(ILIKE|LIKE)\s+(.+)$/i)
|
||||
if (match) {
|
||||
const operator = match[1].toUpperCase()
|
||||
const pattern = match[2].trim()
|
||||
whereClauses.push(`${key} ${operator} $${idx}`)
|
||||
params.push(pattern)
|
||||
// 🔹 highlight jumpout
|
||||
if (message.includes('Jumpout')) {
|
||||
console.log('\x1b[31m%s\x1b[0m', formatted) // แดง = jumpout
|
||||
} else if (message.includes('Error')) {
|
||||
console.log('\x1b[33m%s\x1b[0m', formatted) // เหลือง = error
|
||||
} else {
|
||||
whereClauses.push(`${key} = $${idx}`)
|
||||
params.push(value)
|
||||
console.log(formatted)
|
||||
}
|
||||
idx++
|
||||
|
||||
if (extra) console.log(extra)
|
||||
}
|
||||
|
||||
let finalQuery = baseQuery
|
||||
if (whereClauses.length > 0) {
|
||||
finalQuery += ' AND ' + whereClauses.join(' AND ')
|
||||
// ===================================================
|
||||
// ✅ executeQueryParam() — เจ๊งจริง แล้ว controller catch ได้จริง
|
||||
// ===================================================
|
||||
async executeQueryParam(database, sql, params = []) {
|
||||
const formattedSQL = sql.replace(/\${database}/g, database)
|
||||
try {
|
||||
this.devhint(2, 'executeQueryParam', `📤 Executing Query`, `sql = ${formattedSQL}`)
|
||||
const result = await connection.query(formattedSQL, params)
|
||||
this.devhint(2, 'executeQueryParam', `✅ Query Success (${result.rowCount} rows)`)
|
||||
return result.rows
|
||||
} catch (err) {
|
||||
this.devhint(1, 'executeQueryParam', `❌ SQL Error`, err.message)
|
||||
console.error('🧨 SQL Error:', err.message)
|
||||
throw new Error(`SQL_EXECUTION_FAILED::${err.message}`) // ✅ “เจ๊ง” แล้วโยนขึ้น controller จริง
|
||||
}
|
||||
}
|
||||
|
||||
const formattedSQL = finalQuery.replace(/\${database}/g, database)
|
||||
// ===================================================
|
||||
// ✅ executeQueryConditions() — เหมือนกัน
|
||||
// ===================================================
|
||||
async executeQueryConditions(database, baseQuery, conditions = {}) {
|
||||
this.devhint(2, 'GeneralService', 'executeQueryConditions() start')
|
||||
|
||||
// 🧩 แสดงเฉพาะเมื่อ DEVHINT_LEVEL >= 2
|
||||
devhint(2, 'executeQueryConditions', `📤 Executing Query`, {
|
||||
database,
|
||||
sql: formattedSQL,
|
||||
params
|
||||
})
|
||||
let whereClauses = []
|
||||
let params = []
|
||||
let idx = 1
|
||||
|
||||
const result = await connection.query(formattedSQL, params)
|
||||
for (const [key, value] of Object.entries(conditions)) {
|
||||
if (value === undefined || value === null || value === '') continue
|
||||
const match = String(value).match(/^(ILIKE|LIKE)\s+(.+)$/i)
|
||||
if (match) {
|
||||
const operator = match[1].toUpperCase()
|
||||
const pattern = match[2].trim()
|
||||
whereClauses.push(`${key} ${operator} $${idx}`)
|
||||
params.push(pattern)
|
||||
} else {
|
||||
whereClauses.push(`${key} = $${idx}`)
|
||||
params.push(value)
|
||||
}
|
||||
idx++
|
||||
}
|
||||
|
||||
devhint(2, 'executeQueryConditions', `✅ Query Success (${result.rowCount} rows)`)
|
||||
return result.rows
|
||||
let finalQuery = baseQuery
|
||||
if (whereClauses.length > 0) finalQuery += ' AND ' + whereClauses.join(' AND ')
|
||||
const formattedSQL = finalQuery.replace(/\${database}/g, database)
|
||||
|
||||
try {
|
||||
this.devhint(2, 'executeQueryConditions', `📤 Executing Query`, {
|
||||
database,
|
||||
sql: formattedSQL,
|
||||
params
|
||||
})
|
||||
const result = await connection.query(formattedSQL, params)
|
||||
this.devhint(2, 'executeQueryConditions', `✅ Query Success (${result.rowCount} rows)`)
|
||||
return result.rows
|
||||
} catch (err) {
|
||||
this.devhint(1, 'executeQueryConditions', `❌ SQL Error`, err.message)
|
||||
console.error('🧨 SQL Error:', err.message)
|
||||
throw new Error(`SQL_EXECUTION_FAILED::${err.message}`) // ✅ “เจ๊งจริง” ส่งถึง controller แน่นอน
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ===================================================
|
||||
// ✅ executeQueryParam()
|
||||
// ===================================================
|
||||
export async function executeQueryParam(database, sql, params = []) {
|
||||
const formattedSQL = sql.replace(/\${database}/g, database)
|
||||
|
||||
devhint(2, 'executeQueryParam', `📤 Executing Query`, {
|
||||
database,
|
||||
sql: formattedSQL,
|
||||
params
|
||||
})
|
||||
|
||||
const result = await connection.query(formattedSQL, params)
|
||||
|
||||
devhint(2, 'executeQueryParam', `✅ Query Success (${result.rowCount} rows)`)
|
||||
return result.rows
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ===================================================
|
||||
// Export สำหรับ controller หรืออื่นๆ เรียกใช้ได้ด้วย
|
||||
// ===================================================
|
||||
export { devhint }
|
||||
|
||||
|
||||
|
||||
@@ -175,5 +175,3 @@ export { devhint }
|
||||
// else console.log(formatted)
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user