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,90 @@
|
||||
const { pool } = require("../modules/database")
|
||||
const { DefaultError } = require("../errors/errors")
|
||||
|
||||
async function findById(id) {
|
||||
try {
|
||||
const sql = "SELECT* FROM servers 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 create(serverId, remoteId = null, displayName, avatarUrl) {
|
||||
try {
|
||||
const sql = "INSERT INTO users (serverId, remoteId, displayName, avatarUrl) VALUES (?, ?, ?, ?)"
|
||||
const rows = await pool.query(sql, [serverId, remoteId, displayName, avatarUrl])
|
||||
return rows[0] || null
|
||||
} catch (error) {
|
||||
throw new DefaultError(500, "Internal Server Error", error)
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(id) {
|
||||
try {
|
||||
const sql = "DELETE FROM users WHERE id = ?"
|
||||
const rows = await pool.query(sql, [id])
|
||||
return rows.affectedRows > 0
|
||||
} catch (error) {
|
||||
throw new DefaultError(500, "Internal Server Error", error)
|
||||
}
|
||||
}
|
||||
|
||||
async function isLocal(id) {
|
||||
const sql = "SELECT (remoteId IS NULL) AS isLocal FROM users WHERE id = ?"
|
||||
const rows = await db.query(sql, [id])
|
||||
|
||||
if (rows.length === 0) {
|
||||
throw new DefaultError(404, `User ${id} not found`)
|
||||
}
|
||||
|
||||
return Boolean(rows[0].isLocal)
|
||||
}
|
||||
|
||||
async function updateProfile(id, displayName, avatarUrl) {
|
||||
try {
|
||||
const fields = []
|
||||
const params = { id }
|
||||
|
||||
if (displayName !== undefined) {
|
||||
fields.push('`displayName` = :displayName')
|
||||
params.displayName = displayName
|
||||
}
|
||||
|
||||
if (avatarUrl !== undefined) {
|
||||
fields.push('`avatarUrl` = :avatarUrl')
|
||||
params.avatarUrl = avatarUrl
|
||||
}
|
||||
|
||||
if (fields.length === 0) {
|
||||
return findById(id)
|
||||
}
|
||||
|
||||
const sql = `UPDATE users 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 findByServerAndRemoteId(serverId, remoteId) {
|
||||
try {
|
||||
const sql = "SELECT * FROM users WHERE serverId = ? AND remoteId = ?"
|
||||
const rows = await pool.query(sql, [serverId, remoteId])
|
||||
return rows[0] || null
|
||||
} catch (error) {
|
||||
throw new DefaultError(500, "Internal Server Error", error)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
create,
|
||||
remove,
|
||||
isLocal,
|
||||
findById,
|
||||
updateProfile,
|
||||
findByServerAndRemoteId
|
||||
}
|
||||
Reference in New Issue
Block a user