generated from azures04/Base-REST-API
Add MariaDB integration and repositories
Introduce DB support and repos: add modules/database.js (connection pool + runDDL) and SQL DDL files (data/ddl/*) for servers, users, providers, identities and credentials. Add repository layers: users, servers, providers, identities, credentials. Update server.js to run DDL at startup. Update .env.example with DB settings and bump dependencies in package.json/package-lock.json (add mariadb, bump dotenv/helmet/path-to-regexp/zod and dev tool upgrades). Error handling and logging included for DDL and repo operations.
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
const { pool } = require("../modules/database")
|
||||
const { DefaultError } = require("../errors/errors")
|
||||
|
||||
async function findById(id) {
|
||||
try {
|
||||
const sql = "SELECT* FROM providers WHERE id = ?"
|
||||
const rows = await pool.query(sql, [id])
|
||||
return rows[0] || null
|
||||
} catch (error) {
|
||||
throw new DefaultError(500, "Internal Server Error", error)
|
||||
}
|
||||
}
|
||||
|
||||
async function findByName(id) {
|
||||
try {
|
||||
const sql = "SELECT* FROM providers WHERE name = ?"
|
||||
const rows = await pool.query(sql, [id])
|
||||
return rows[0] || null
|
||||
} catch (error) {
|
||||
throw new DefaultError(500, "Internal Server Error", error)
|
||||
}
|
||||
}
|
||||
|
||||
async function listEnabled() {
|
||||
try {
|
||||
const sql = "SELECT* FROM providers WHERE isEnabled = ?"
|
||||
const rows = await pool.query(sql, [true])
|
||||
return rows || []
|
||||
} catch (error) {
|
||||
throw new DefaultError(500, "Internal Server Error", error)
|
||||
}
|
||||
}
|
||||
|
||||
async function setEnabled(id, isEnabled) {
|
||||
try {
|
||||
const sql = "UPDATE providers SET isEnabled = ? WHERE id = ?"
|
||||
const rows = await pool.query(sql, [isEnabled, id])
|
||||
return rows.affectedRows > 0
|
||||
} catch (error) {
|
||||
throw new DefaultError(500, "Internal Server Error", error)
|
||||
}
|
||||
}
|
||||
|
||||
async function create(name, issuerLogo, issuerURL) {
|
||||
try {
|
||||
const sql = "INSERT INTO providers (name, issuerLogo, issuerURL) VALUES (?, ?, ?)"
|
||||
const rows = await pool.query(sql, [name, issuerLogo, issuerURL])
|
||||
return rows[0]
|
||||
} catch (error) {
|
||||
throw new DefaultError(500, "Internal Server Error", error)
|
||||
}
|
||||
}
|
||||
|
||||
async function updateProvider(id, name, issuerLogo, issuerURL) {
|
||||
try {
|
||||
const fields = []
|
||||
const params = { id }
|
||||
|
||||
if (name !== undefined) {
|
||||
fields.push('`name` = :name')
|
||||
params.name = name
|
||||
}
|
||||
|
||||
if (issuerLogo !== undefined) {
|
||||
fields.push('`issuerLogo` = :issuerLogo')
|
||||
params.issuerLogo = issuerLogo
|
||||
}
|
||||
|
||||
if (issuerURL !== undefined) {
|
||||
fields.push('`issuerURL` = :issuerURL')
|
||||
params.issuerURL = issuerURL
|
||||
}
|
||||
|
||||
if (fields.length === 0) {
|
||||
return findById(id)
|
||||
}
|
||||
|
||||
const sql = `UPDATE providers SET ${fields.join(', ')} WHERE id = :id`
|
||||
await pool.query(sql, params)
|
||||
|
||||
return findById(id)
|
||||
} catch (error) {
|
||||
throw new DefaultError(500, "Internal Server Error", error)
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(id) {
|
||||
try {
|
||||
const sql = "DELETE FROM providers WHERE id = ?"
|
||||
const rows = await pool.query(sql, [id])
|
||||
return rows.affectedRows > 0
|
||||
} catch (error) {
|
||||
throw new DefaultError(500, "Internal Server Error", error)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
remove,
|
||||
create,
|
||||
findById,
|
||||
findByName,
|
||||
setEnabled,
|
||||
listEnabled,
|
||||
updateProvider,
|
||||
}
|
||||
Reference in New Issue
Block a user