-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
150 lines (93 loc) · 3.05 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const sql =require("mysql");
const MD5 = require("crypto-js/md5");
require("dotenv").config();
const host = process.env.HOST;
const usr = process.env.USR;
const password = process.env.PASSWORD;
const database = process.env.DATABASE;
const connection= sql.createConnection({
host:host,
user:usr,
password:password,
database:database
});
connection.connect();
const connetti= async()=>
{
connection.query("select * from utenti",(err,res)=>{
});
return connection;
};
const query =(q,callback) =>{
connection.query(q,callback);
}
const query2= (q) =>{
return new Promise((resolve, reject) => {
connection.query(q,(error, results) => {
if (error) {
return reject(error);
}
resolve(results);
});
});
};
inizializza();
async function inizializza()
{
creaTableUser();
creaTableGettoni();
creaTableAcquista();
}
async function creaTableAcquista()
{
let q = `CREATE TABLE IF NOT EXISTS acquisti (idUtente int(11) NOT NULL,idGettone int(11) NOT NULL)`;
connection.query(q,(err,res)=>{
if( err) throw err;
})
}
async function creaTableUser() {
let q = `CREATE TABLE IF NOT EXISTS utenti(id int(11) PRIMARY KEY AUTO_INCREMENT,email varchar(20) NOT NULL,nickname varchar(20) NOT NULL,
pass varchar(32) NOT NULL,
punti int(11) NOT NULL,
monete int(11) NOT NULL,
activeSkin varchar(50) NOT NULL
)`;
connection.query(q,(err,res)=>{
if(err) throw err;
connection.query("CREATE TRIGGER IF NOT EXISTS `gettoneRed` AFTER INSERT ON `utenti` FOR EACH ROW INSERT INTO acquisti (idUtente, idGettone) VALUES (NEW.id, 5)",(err,res)=>{
if(err) throw err;
});
});
}
async function creaTableGettoni()
{
let q = `CREATE TABLE IF NOT EXISTS gettoni (
id int(11) NOT NULL,
path varchar(32) NOT NULL,
costo int(11) NOT NULL
)`;
connection.query(q,(err,res)=>{
if(err) throw err;
});
let qInsert = `INSERT IGNORE INTO gettoni (id, path, costo) VALUES
(1, 'images/mario.png', 150),
(2, 'images/charizard.png', 70),
(3, 'images/dragon.png', 25),
(4, 'images/pikachu.png', 50),
(5, 'images/red.png', 0),
(6, 'images/sonic.png', 50),
(7, 'images/star.png', 10),
(8, 'images/wario.png', 35),
(9, 'images/yellow.png', 0),
(10, 'images/sponge.png', 40),
(11, 'images/spongeMeme.png', 15),
(12, 'images/deadpool.png', 300),
(13, 'images/spiderman.png', 120);
`;
connection.query(qInsert,(err,res)=>{
if(err) throw err;
});
}
module.exports = {
query,connetti
};