example for template master device

This commit is contained in:
2025-10-10 14:53:43 +07:00
parent 6eed13bc4f
commit ab3b38eb49
9 changed files with 266 additions and 226 deletions

View File

@@ -46,6 +46,11 @@ async function query(text, params = []) {
return request.query(sqlText);
}
function isValidDate(dateStr) {
const d = new Date(dateStr);
return !isNaN(d.getTime()); // true kalau valid
}
/**
* Build filter query
*/
@@ -71,10 +76,24 @@ function buildFilterQuery(filterQuery = [], fixedParams = []) {
queryParams.push(f.param ? 1 : 0);
whereConditions.push(`${f.column} = $${queryParams.length}`);
break;
case 'between':
if (Array.isArray(f.param) && f.param.length === 2) {
const from = f.param[0];
const to = f.param[1];
if (isValidDate(from) && isValidDate(to)) {
queryParams.push(from);
queryParams.push(to);
whereConditions.push(
`${f.column} BETWEEN $${queryParams.length - 1} AND $${queryParams.length}`
);
}
}
break;
}
});
return { whereConditions, queryParams };
return { whereConditions, whereParamAnd: queryParams };
}
/**
@@ -96,13 +115,17 @@ function buildStringOrIlike(columnParam, criteria, fixedParams = []) {
? `AND (${orStringConditions.join(" OR ")})`
: "";
return { whereOrConditions: whereClause, whereParam: queryParams };
return { whereOrConditions: whereClause, whereParamOr: queryParams };
}
/**
* Build dynamic UPDATE
*/
function buildDynamicUpdate(table, data, where) {
data.updated_by = data.userId
delete data.userId;
const setParts = [];
const values = [];
let index = 1;
@@ -118,8 +141,8 @@ function buildDynamicUpdate(table, data, where) {
throw new Error("Tidak ada kolom untuk diupdate");
}
// updated_at otomatis pakai GETDATE()
setParts.push(`updated_at = GETDATE()`);
// updated_at otomatis pakai CURRENT_TIMESTAMP
setParts.push(`updated_at = CURRENT_TIMESTAMP`);
const whereParts = [];
for (const [key, value] of Object.entries(where)) {
@@ -140,6 +163,11 @@ function buildDynamicUpdate(table, data, where) {
* Build dynamic INSERT
*/
function buildDynamicInsert(table, data) {
data.created_by = data.userId
data.updated_by = data.userId
delete data.userId;
const columns = [];
const placeholders = [];
const values = [];
@@ -159,7 +187,7 @@ function buildDynamicInsert(table, data) {
// created_at & updated_at otomatis
columns.push("created_at", "updated_at");
placeholders.push("GETDATE()", "GETDATE()");
placeholders.push("CURRENT_TIMESTAMP", "CURRENT_TIMESTAMP");
const query = `
INSERT INTO ${table} (${columns.join(", ")})