-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
196 lines (168 loc) · 5.14 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const sqlite3 = require('sqlite3');
const Promise = require('bluebird');
let dao = {};
let db;
//create db
dao.createDb = () => {
db = new sqlite3.Database("./database.sqlite3", (err) => {
if (err) {
console.log('Error when creating the database', err)
} else {
console.log('Database created!')
/* Put code to create table(s) here */
dao.createTable(db);
}
});
};
//close db
dao.closeDb = () => {
db.close();
};
//create table
dao.createTable = (db) => {
console.log("create database table");
db.run("CREATE TABLE IF NOT EXISTS actors(id INTEGER PRIMARY KEY, login TEXT, avatar_url TEXT)");
db.run("CREATE TABLE IF NOT EXISTS events(id INTEGER PRIMARY KEY, created_at TIMESTAMP, type TEXT, actor INTEGER, repo INTEGER, FOREIGN KEY (actor) REFERENCES actors(id), FOREIGN KEY (repo) REFERENCES repository(id))");
db.run("CREATE TABLE IF NOT EXISTS repository(id INTEGER PRIMARY KEY, name TEXT, url TEXT)");
};
//run sql query
dao.run = (sql, params = []) => {
return new Promise((resolve, reject) => {
db.run(sql, params, function (err, data) {
if (err) {
console.log('Error running sql ' + sql)
console.log(err)
reject(err)
} else {
resolve(data)
}
})
})
};
//get sqlite data
dao.get = (sql, params = []) => {
return new Promise((resolve, reject) => {
db.get(sql, params, (err, result) => {
if (err) {
console.log('Error running sql: ' + sql)
console.log(err)
reject(err)
} else {
resolve(result)
}
})
})
};
//select all data for sqlite3 query
dao.all = (sql, params = []) => {
return new Promise((resolve, reject) => {
db.all(sql, params, (err, rows) => {
if (err) {
console.log('Error running sql: ' + sql)
console.log(err)
reject(err)
} else {
resolve(rows)
}
})
})
};
/*
* queries
*/
//create event
dao.createEvent = (table, fields, data) => {
//data is an array containing type, repo and actor id;
let sql = `INSERT INTO events (${fields}) VALUES(?, ?, ?, ?, ?)`;
return dao.run(sql, data);
};
//create actor
dao.createActor = (table, fields, data) => {
//data is an array containing type, repo and actor id;
let sql = `INSERT INTO actors (${fields}) VALUES(?, ?, ?)`;
return dao.run(sql, data);
};
//create repo
dao.createRepo= (table, fields, data) => {
//data is an array containing type, repo and actor id;
let sql = `INSERT INTO repository (${fields}) VALUES(?, ?, ?)`;
return dao.run(sql, data);
};
//get all events by ascending order
dao.getEvents = () => {
return dao.all(`
SELECT events.id AS event_id, * FROM events
INNER JOIN actors on actors.id = events.actor
INNER JOIN repository on repository.id = events.repo
ORDER BY id ASC
`);
};
//get all events for actors()
dao.getAllActors = () => {
return dao.all(`
SELECT actors.*, count(events.id) as event_count,
max(created_at) as date from actors
inner join events on actors.id = events.actor
GROUP BY events.actor
order by event_count DESC, date DESC
`);
};
//get events by actor id
dao.getEventByActor = (id) => {
let sql = `
SELECT events.id AS event_id, * FROM events
INNER JOIN actors on actors.id = events.actor
INNER JOIN repository on repository.id = events.repo WHERE actor = ?
ORDER BY id ASC`;
return dao.all(sql, [id]);
};
//return events for maximum streak calculation
dao.getEventStreak = () => {
return dao.all(`
SELECT events.id AS event_id, * FROM events
INNER JOIN actors on actors.id = events.actor
ORDER BY created_at ASC
`)
}
//get events by id
//find event to check if it exists before creating a new record
dao.singleEvent = (id) => {
let sql = 'SELECT * FROM events WHERE id = ?';
return dao.get(sql, [id]);
};
//get all actor;
dao.allActors = () => {
return dao.all('SELECT * FROM actors');
}
//get single actor
dao.singleActor = (id) => {
let sql = 'SELECT * FROM actors WHERE id = ?';
return dao.get(sql, [id]);
};
//get single repository
//get single actor
dao.singleRepo = (id) => {
let sql = 'SELECT * FROM repository WHERE id = ?';
return dao.get(sql, [id]);
};
//update actor url
dao.updateActorUrl = (data) => {
let sql = 'UPDATE actors SET avatar_url = ? WHERE id = ?';
return dao.run(sql, [data.avatar_url, data.id]);
};
//delete event records
dao.eraseEvent = () => {
let sql = 'DELETE FROM events';
return dao.run(sql);
};
//delete repo records
dao.eraseRepository = () => {
let sql = 'DELETE FROM repository';
return dao.run(sql);
};
//delete actor records
dao.eraseActors = () => {
let sql = 'DELETE FROM actors';
return dao.run(sql);
}
module.exports = dao;