-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
39 lines (31 loc) · 1.46 KB
/
app.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
var express = require('express');
var app = express();
var mysql = require('mysql');
var mysql_config = require('./mysql_config.js');
var func = require('./main.js');
var mysqlConnection = mysql.createConnection(mysql_config.mySQLConfiguration());
app.get('/', function(req, res){
var sql = 'SELECT * FROM courses LEFT JOIN course_sections ON course_sections.course_id = courses.id LEFT JOIN departments ON departments.id = courses.department_id LEFT JOIN course_terms ON course_sections.courseterm_id = course_terms.id';
//Key relations, Define each table's primary and foreign keys
var nestingOptions = [
{ tableName : 'courses', pkey: 'id', fkeys:[{table:'departments',col:'department_id'}]},
{ tableName : 'course_sections', pkey: 'id', fkeys:[{table:'courses',col:'course_id'},{table:'course_terms',col:'courseterm_id'}]},
{ tableName : 'departments', pkey: 'id'},
{ tableName : 'course_terms', pkey: 'id'}
];
mysqlConnection.query({sql: sql, nestTables: true}, function (err, rows) {
// error handling
if (err){
console.log('Internal error: ', err);
res.send("Mysql query execution error!");
}
else {
var nestedRows = func.convertToNested(rows, nestingOptions);
// res.send(JSON.stringify(nestedRows));
res.send(nestedRows);
}
});
});
app.listen('8081');
console.log('Magic happens on port 8081');
exports = module.exports = app;