generated from azures04/Base-REST-API
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.
49 lines
1.5 KiB
JavaScript
49 lines
1.5 KiB
JavaScript
const { pool } = require("../modules/database")
|
|
const { DefaultError } = require("../errors/errors")
|
|
|
|
async function findByUserId(userId) {
|
|
try {
|
|
const sql = "SELECT* FROM identities WHERE userId = ?"
|
|
const rows = await pool.query(sql, [userId])
|
|
return rows || null
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function findByProviderAndSubject(providerId, providerUserId) {
|
|
try {
|
|
const sql = "SELECT* FROM identities WHERE providerId = ? AND providerUserId = ?"
|
|
const rows = await pool.query(sql, [providerId, providerUserId])
|
|
return rows[0] || null
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function create(userId, providerId, providerUserId) {
|
|
try {
|
|
const sql = "INSERT INTO identities (userId, providerId, providerUserId) VALUES (?, ?, ?)"
|
|
const rows = await pool.query(sql, [userId, providerId, providerUserId])
|
|
return rows[0] || null
|
|
} catch (error) {
|
|
throw new DefaultError(500, "Internal Server Error", error)
|
|
}
|
|
}
|
|
|
|
async function remove(id) {
|
|
try {
|
|
const sql = "DELETE FROM identities 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 = {
|
|
create,
|
|
remove,
|
|
findByUserId,
|
|
findByProviderAndSubject
|
|
} |