add: role db

This commit is contained in:
2025-10-07 13:45:48 +07:00
parent 315537fc6e
commit 857e9ecf63

59
db/role.db.js Normal file
View File

@@ -0,0 +1,59 @@
const pool = require("../config");
// Get all roles
const getAllRolesDb = async () => {
const queryText = `
SELECT *
FROM m_roles
WHERE deleted_at IS NULL
ORDER BY role_id ASC
`;
const result = await pool.query(queryText);
return result.recordset;
};
// Get role by ID
const getRoleByIdDb = async (roleId) => {
const queryText = `
SELECT *
FROM m_roles
WHERE role_id = $1 AND deleted_at IS NULL
`;
const result = await pool.query(queryText, [roleId]);
return result.recordset[0];
};
// Create role
const createRoleDb = async (data) => {
const { query: queryText, values } = pool.buildDynamicInsert("m_roles", data);
const result = await pool.query(queryText, values);
return result.recordset[0]?.inserted_id || null;
};
// Update role
const updateRoleDb = async (roleId, data) => {
const { query: queryText, values } = pool.buildDynamicUpdate("m_roles", data, { role_id: roleId });
await pool.query(queryText, values);
return true;
};
// Soft delete role
const deleteRoleDb = async (roleId, deletedBy) => {
const queryText = `
UPDATE m_roles
SET deleted_at = GETDATE(),
deleted_by = $1
WHERE role_id = $2
`;
await pool.query(queryText, [deletedBy, roleId]);
return true;
};
module.exports = {
getAllRolesDb,
getRoleByIdDb,
createRoleDb,
updateRoleDb,
deleteRoleDb,
};