diff --git a/backend/.env-example b/backend/.env-example new file mode 100644 index 0000000..0996ae2 --- /dev/null +++ b/backend/.env-example @@ -0,0 +1,7 @@ +PORT=5000 + +DB_HOST= +DB_PORT= +DB_USER= +DB_PASSWORD= +DB_NAME= \ No newline at end of file diff --git a/backend/config/database.js b/backend/config/database.js new file mode 100644 index 0000000..7aa1154 --- /dev/null +++ b/backend/config/database.js @@ -0,0 +1,19 @@ +import mysql from "mysql2/promise"; +import dotenv from "dotenv"; + +dotenv.config(); + +export const pool = mysql.createPool({ + host : process.env.DB_HOST, + port : process.env.DB_PORT, + user: process.env.DB_USER, + database: process.env.DB_NAME, + password: process.env.DB_PASSWORD, + waitForConnections: true, + connectionLimit: 10 +}); + +export async function query(sql, params = []){ + const [rows] = await pool.execute(sql, params); + return rows; +} \ No newline at end of file diff --git a/backend/repositories/usuarioRepository.js b/backend/repositories/usuarioRepository.js new file mode 100644 index 0000000..957df12 --- /dev/null +++ b/backend/repositories/usuarioRepository.js @@ -0,0 +1,18 @@ +import {query} from "../config/database.js"; + +export async function findAll() { + return query(`select usuario.id, usuario.nome, usuario.email, perfil.nome + from usuario inner join perfil on + usuario.perfil_id = perfil.id + where usuario.ativo = true`) +} + +export async function findById(id) { + return query(`select usuario.id, usuario.nome, usuario.email, perfil.nome + from usuario inner join perfil on + usuario.perfil_id = perfil.id + where usuario.ativo = true and usuario.id = ?`, + [id]) +} + +