-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdatabase.js
35 lines (30 loc) · 848 Bytes
/
database.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import mysql from 'mysql2';
import dotenv from 'dotenv';
dotenv.config();
const pool = mysql.createPool({
host: process.env.MYSQL_IP,
port: process.env.MYSQL_PORT,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_ROOT_PASSWORD,
database: process.env.MYSQL_DATABASE,
}).promise();
export async function getAlunos(){
const result = await pool.query(`SELECT * FROM ALUNOS;`);
return result[0];
}
export async function getAluno(id){
const result = await pool.query(`
SELECT *
FROM ALUNOS
WHERE id = ?;
`, [id]);
return result[0];
}
export async function createAluno(nome, idade, cidade){
const result = await pool.query(`
INSERT INTO ALUNOS (nome, idade, cidade)
VALUES (?, ?, ?);
`, [nome, idade, cidade]);
const id = result[0].insertId;
return getAluno(id);
}