-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
34 lines (29 loc) · 733 Bytes
/
Copy pathdatabase.js
File metadata and controls
34 lines (29 loc) · 733 Bytes
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
import mysql from 'mysql2'
import dotenv from 'dotenv'
dotenv.config()
const pool = mysql.createPool({
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE
}).promise()
export async function getNotes() {
const [rows] = await pool.query("SELECT * FROM notes")
return rows
}
export async function getNote(id) {
const [rows] = await pool.query(`
SELECT *
FROM notes
WHERE id = ?
`, [id])
return rows[0]
}
export async function createNote(title, contents) {
const [result] = await pool.query(`
INSERT INTO notes (title, contents)
VALUES (?, ?)
`, [title, contents])
const id = result.insertId
return getNote(id)
}