-
Notifications
You must be signed in to change notification settings - Fork 0
/
createDatabase.js
181 lines (164 loc) · 6.15 KB
/
createDatabase.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
var fs = require('fs');
var fluent = require("fluent-async");
var _ = require("underscore");
var sql = require("sqlite3");
// this file can be run with an extra argument, if the user only wants to add one of the tables to the database
// the parameters available are: incidents, results, teams or users
// if no extra parameter passed in, all tables will be added to the database
if(process.argv[2] === "incidents") {
incidents(done);
}
else if(process.argv[2] === "results") {
results(done);
}
else if(process.argv[2] === "teams") {
teams(done);
}
else if(process.argv[2] === "users") {
user(done);
}
else {
fluent.create({}).async({results:results}).wait().async({teams:teams}).wait().async({incidents:incidents}).wait().async({user:user}).run(done);
}
// results table population flow
function results(callback) {
var db = new sql.Database("football.db");
console.log("populating the results table");
fluent.create({
table:"results", fileName:"results", db:db
}).async({
checkTableExists:checkTableExists
}, "table", "db").async({
createResultsTable:createResultsTable
}, "checkTableExists", "db").wait().sync({
getJsonData:getJsonData
}, "fileName").sync({
addResults: addResults
}, "getJsonData", "db").wait().sync({
close:close
}, "db").run(callback);
}
// incidents table population flow
function incidents(callback) {
var db = new sql.Database("football.db");
console.log("populating the incidents table");
fluent.create({
table:"incidents", fileName:"results", db:db
}).async({
checkTableExists:checkTableExists
}, "table", "db").async({
createIncidentsTable:createIncidentsTable
}, "checkTableExists", "db").wait().sync({
getJsonData:getJsonData
}, "fileName").sync({
addIncidents: addIncidents
}, "getJsonData", "db").wait().sync({
close:close
}, "db").run(callback);
}
// teams table population flow
function teams(callback) {
var db = new sql.Database("football.db");
console.log("populating the teams table");
fluent.create({
table:"teams", fileName:"tables", db:db
}).async({
checkTableExists:checkTableExists
}, "table", "db").async({
createTeamsTable:createTeamsTable
}, "checkTableExists", "db").wait().sync({
getJsonData:getJsonData
}, "fileName").sync({
addTeams: addTeams
}, "getJsonData", "db").wait().sync({
close:close
}, "db").run(callback);
}
// teams user population flow
function user(callback) {
var db = new sql.Database("football.db");
console.log("creating admin user");
fluent.create({
table:"users", db:db
}).async({
checkTableExists:checkTableExists
}, "table", "db").async({
createUsersTable:createUsersTable
}, "checkTableExists", "db").wait().sync({
addAdmin: addAdmin
}, "db").wait().sync({
close:close
}, "db").run(callback);
}
// check if the inputted table name exists in the database
function checkTableExists(tableName, db, callback) {
db.all("SELECT * FROM sqlite_master WHERE name =? and type='table'", [tableName], callback);
}
// next 4 functions create the relevant tables if they don't exist, or run the callback to continue to the next function, if they do
function createResultsTable(table, db, callback) {
if(table.length){ callback(); }
else {
db.run("create table results (id integer, date text, home_id integer, away_id integer, status text, round integer, scoreHome integer, scoreAway integer)", callback);
}
}
function createIncidentsTable(table, db, callback) {
if(table.length){ callback(); }
else {
db.run("create table incidents (id integer, type text, goaltype text, team_id integer, player_id integer, player text, playershort text, minute integer, match_id integer)", callback);
}
}
function createTeamsTable(table, db, callback) {
if(table.length){ callback(); }
else {
db.run("create table teams (id integer, team text, teamshort text)", callback);
}
}
function createUsersTable(table, db, callback) {
if(table.length){ callback(); }
else {
db.run("create table users (id integer, username text, password text)", callback);
}
}
// get data stored in the json file having the inputted name and being a part of the local /data folder
function getJsonData(fileName) {
var fileJSON = fs.readFileSync('./data/'+fileName+'.json');
return JSON.parse(fileJSON);
}
// next 4 functions add sample data to the relevant tables. running this script twice in a row will add the same data twice,
// so please delete the .db file if you want to revert the database to its initial state
function addResults(rows, db) {
var match, _i, _len;
for (_i = 0, _len = rows.length; _i < _len; _i++) {
match = _.pick(rows[_i], ["id", "date", "home_id", "away_id", "status", "round", "scoreHome", "scoreAway"]);
db.run("insert into results values(?, ?, ?, ?, ?, ?, ?, ?)", _.values(match));
}
}
function addIncidents(rows, db) {
var incident, _i, _j, _len, _len2;
rows = _.filter(rows, function(match){ return (match.incidents && match.incidents.length); });
for (_i = 0, _len = rows.length; _i < _len; _i++) {
for (_j = 0, _len2 = rows[_i].incidents.length; _j < _len2; _j++) {
incident = _.pick(rows[_i].incidents[_j], ["id", "type", "goaltype", "team_id", "player_id", "player", "playershort", "minute"]);
incident.match_id = rows[_i].id;
db.run("insert into incidents values(?, ?, ?, ?, ?, ?, ?, ?, ?)", _.values(incident));
}
}
}
function addTeams(rows, db) {
var team, _i, _len;
for (_i = 0, _len = rows.length; _i < _len; _i++) {
team = _.pick(rows[_i], ["team_id", "team", "teamshort"]);
db.run("insert into teams values(?, ?, ?)", _.values(team));
}
}
function addAdmin(db) {
db.run("insert into users values(1, 'admin', 'admin')");
}
// close database connection
function close(db) {
db.close();
}
// if an error occurred, it shows it in the terminal where the script is being run
function done(callback) {
if(callback) { console.log("Error: ", callback); }
}