-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.js
More file actions
43 lines (35 loc) · 1 KB
/
Copy pathdatabase.js
File metadata and controls
43 lines (35 loc) · 1 KB
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
36
37
38
39
40
41
42
43
import mysql from 'mysql2'
import dotenv from 'dotenv'
import { create } from 'domain'
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
}
const notes = await getNotes()
export async function getNote(id){
const [result] = await pool.query(
`SELECT *
FROM notes
WHERE id = ${id}`)
return result
}
export async function createNote(title, contents){
const [insert] = await pool.query(`
INSERT into notes (title, contents)
VALUES ('${title}', '${contents}')
`)
return getNote(insert.insertId)
}
export async function resetNotes(){
const reset = await pool.query(`
TRUNCATE TABLE notes;
`)
return { success: true, message: "Notes table has been reset"}
}