-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.js
46 lines (37 loc) · 1.11 KB
/
db.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
36
37
38
39
40
41
42
43
44
45
46
const mysql = require('mysql')
require('dotenv').config();
const getConnection = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME
});
const createTables = () => {
const createCosmeticsTable = `CREATE TABLE IF NOT EXISTS cosmetics (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
type VARCHAR(255) NOT NULL
)`;
getConnection.query(createCosmeticsTable, (err) => {
if (err) {
throw err;
}
console.log('Cosmetics table created or already existed!');
})
addPetCosmetic("Frog");
}
const addPetCosmetic = (name) => {
const insertPetQuery = 'INSERT INTO cosmetics (name, type) VALUES (?, ?)';
const values = [name, "pet"]; // Modify the scale value as needed
getConnection.query(insertPetQuery, values, (err, result) => {
if (err) {
throw err;
}
console.log('Pet added successfully:', result);
});
};
module.exports = {
getConnection,
createTables,
addPetCosmetic
};