-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
40 lines (36 loc) · 981 Bytes
/
init.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
const mysql = require('mysql2');
const fs = require('fs')
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
});
const sqlStatements = fs.readFileSync("init.sql", 'utf-8').split(";")
let promises = []
const intitDB = () => {
sqlStatements.forEach(statement => {
if(statement.trim()){
let query = new Promise((resolve, reject) => {
connection.query(statement, (err, results) => {
if(err){
reject(err)
} else {
console.log("Query ran successfully")
resolve()
}
})
});
promises.push(query)
}
})
}
intitDB();
Promise.all(promises).then(() => {
connection.end((err) => {
if(err){
throw err
} else {
console.log("DB closed successfully")
}
})
})