-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase-initializer.js
139 lines (115 loc) · 4.18 KB
/
database-initializer.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
var mysql = require('mysql');
var parse = require('csv-parse');
var fs = require('fs');
var cardJSON = 'json_data/card-data.json';
var isCreated=0;
var index = 0;
//holds json_data
var cardOutput = [];
var cardID = [];
var cardName = [];
var cardClass = [];
var deckCode = [];
// creates the stream to read in CSV's from file if we ever use CSV's
/*fs.createReadStream(cardCSV).pipe(parse())
.on('data',function(csvrow) {
cardOutput.push(csvrow);
}); */
//Create connection to the database
var con;
if(isCreated===1)
{
con = mysql.createConnection({
host: "localhost",
user: "root",
password: "Badgers1!",
database: "hsdb"
});
}
else
{
con = mysql.createConnection({
host: "localhost",
user: "root",
password: "Badgers1!"
});
isCreated= 1;
}
function createDB() {
con.query("CREATE DATABASE hsdb", function (err, result) {
console.log("Database created"); //DEBUG
});
con.query('USE hsdb', function (err, result) {
console.log("hsdb ready for use"); //DEBUG
});
}
var cmds = [
"DROP TABLE card",
"CREATE TABLE IF NOT EXISTS card (name VARCHAR(255), class VARCHAR(255), id VARCHAR(255), PRIMARY KEY (id)) ENGINE=InnoDB",
"CREATE TABLE IF NOT EXISTS has (cardid VARCHAR(255), deckcode VARCHAR(255)) ENGINE=InnoDB",
"CREATE TABLE IF NOT EXISTS user (userid INT NOT NULL AUTO_INCREMENT, email VARCHAR(255) NOT NULL UNIQUE, PRIMARY KEY (userid)) ENGINE=InnoDB",
"CREATE TABLE IF NOT EXISTS ownedBy " +
"(deckname VARCHAR(255) NOT NULL, " +
"userid INT NOT NULL, " +
"deckcode VARCHAR(255) NOT NULL, " +
"PRIMARY KEY (userid, deckcode), " +
"FOREIGN KEY (userid) REFERENCES user (userid) ON DELETE CASCADE) ENGINE = InnoDB",
"CREATE TABLE IF NOT EXISTS tournament (tournamentid INT NOT NULL AUTO_INCREMENT, name VARCHAR(255)," +
"numDecks INT unsigned, PRIMARY KEY (tournamentid)) ENGINE=InnoDB",
"CREATE TABLE IF NOT EXISTS playsInTournament (tournamentid INT NOT NULL, userid INT NOT NULL," +
"PRIMARY KEY (tournamentid, userid)," +
"FOREIGN KEY (tournamentid) REFERENCES tournament (tournamentid) ON DELETE CASCADE," +
"FOREIGN KEY (userid) REFERENCES user (userid) ON DELETE CASCADE) ENGINE=InnoDB",
"CREATE TABLE IF NOT EXISTS matches " +
"(matchid INT NOT NULL AUTO_INCREMENT, homeTeamId INT, awayTeamId INT, winningTeamId INT, isValid INT, PRIMARY KEY (matchid)) ENGINE=InnoDB"
]
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
try {
createDB();
console.log("hsdb sucessfully created!"); //DEBUG
}
catch (ER_DB_CREATE_EXISTS) {
con.query("DROP DATABASE IF EXISTS hsdb");
createDB();
}
// iterate over queries and execute them
for (var i = 0; i < cmds.length; i++ ) {
con.query(cmds[i], function (err, result) {
if (err) throw err;
});
}
console.log("Tables initialized");
populate(index);
//bulkloading JSON files into DB
function populate(index) {
if (index === 0) {
fs.readFile(cardJSON, 'utf8', function (err, data) {
if (err) throw err;
//JSON parse command
data = JSON.parse(data);
for (i = 0; i < data.length; i++) {
//pull info from JSON file
cardID[i] = (data[i].dbfId);
cardName[i] = (data[i].name);
cardClass[i] = (data[i].cardClass);
//put data into one array to easily import into DB
cardOutput.push([cardName[i], cardClass[i], cardID[i]]);
}
//inserts card data into DB
con.query("INSERT INTO card (name, class, id) VALUES ?", [cardOutput], function (err) {
if (err) throw err;
});
console.log("Card table populated");
if (index === 0)
populate(index+1);
});
}
if (index === 1) {
console.log("hsdb initializer finished");
con.end();
return;
}
}
});