-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
44 lines (40 loc) · 990 Bytes
/
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
const mysql = require('mysql2')
const connection = mysql.createConnection({
host: 'localhost',
user: 'myuser',
password:'mypass',
database:'mytestdb'
})
function getAllPersons(){
return new Promise(function (resolve, reject){
connection.query(
`select * from persons`,
function(err,rows,cols){
if(err){
reject(err)
} else{
resolve(rows)
}
}
)
})
}
function addNewPersons(name,age,city){
return new Promise(function (resolve, reject){
connection.query(
`insert into persons(name,age,city) values(?,?,?)`,
[name,age,city],
function(err, result){
if(err){
reject(err)
} else{
resolve()
}
}
)
})
}
exports = module.exports = {
getAllPersons,
addNewPersons
}