generated from azures04/Base-REST-API
Introduce bootstrap entrypoint and prestartup to ping DB and generate/manage RSA keypair. Add security module for key IO. Implement JWT-based access and refresh tokens (tokensService) with refresh_tokens table and repo. Add auth, provider and user services, and refresh-token management. Update repositories (users, credentials, providers, servers) with SQL fixes and new helper methods. Move DDL execution to bootstrap (remove from server). Update package.json main and add dependencies (bcryptjs, jsonwebtoken). Update .env.example and .gitignore accordingly.
89 lines
2.4 KiB
JavaScript
89 lines
2.4 KiB
JavaScript
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 findByUrl(url) {
|
|
try {
|
|
const sql = "SELECT * FROM servers WHERE url = ?"
|
|
const rows = await pool.query(sql, [url])
|
|
return rows[0] || null
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function create(url, publicKey) {
|
|
try {
|
|
const sql = "INSERT INTO servers (serverUrl, publicKey) VALUES (?, ?)"
|
|
const rows = await pool.query(sql, [url, publicKey])
|
|
return rows[0] || null
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function createAndAssignIdManually(id, url, publicKey) {
|
|
try {
|
|
const sql = "INSERT INTO servers (id, serverUrl, publicKey) VALUES (?, ?, ?)"
|
|
const rows = await pool.query(sql, [id, url, publicKey])
|
|
return rows[0] || null
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function remove(id) {
|
|
try {
|
|
const sql = "DELETE FROM servers 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 updateServer(id, serverUrl, publicKey) {
|
|
try {
|
|
const fields = []
|
|
const params = { id }
|
|
|
|
if (serverUrl !== undefined) {
|
|
fields.push('`serverUrl` = :serverUrl')
|
|
params.serverUrl = serverUrl
|
|
}
|
|
|
|
if (publicKey !== undefined) {
|
|
fields.push('`publicKey` = :publicKey')
|
|
params.publicKey = publicKey
|
|
}
|
|
|
|
if (fields.length === 0) {
|
|
return findById(id)
|
|
}
|
|
|
|
const sql = `UPDATE servers SET ${fields.join(', ')} WHERE id = :id`
|
|
await pool.query(sql, params)
|
|
|
|
return findById(id)
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
create,
|
|
remove,
|
|
findById,
|
|
findByUrl,
|
|
updateServer,
|
|
createAndAssignIdManually
|
|
} |