-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabaseRequests.js
52 lines (44 loc) · 1.14 KB
/
databaseRequests.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
'use strict';
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'test2',
password : 'test2',
database : 'calorie_counter'
});
connection.connect();
function getMeals(callback) {
var sql = 'SELECT meal_id, name, calories, date FROM `meals`';
connection.query(sql, function(err, res) {
if (err) throw err;
callback(res);
});
}
function getMealById(id, callback) {
var sql = 'SELECT meal_id, name, calories, date FROM `meals` WHERE meal_id=?';
connection.query(sql, id, function(err, res) {
if (err) throw err;
callback(res);
});
}
function addMeal(attributes, callback) {
var sql = 'INSERT INTO `meals` SET ?';
connection.query(sql, attributes, function(err, res) {
if (err) throw err;
var newId = res.insertId;
getMealById(newId, callback);
});
}
function removeMeal(id, callback) {
var sql = 'DELETE FROM `meals` WHERE `meal_id`= ?';
connection.query(sql, id, function(err, res) {
if (err) throw err;
callback(res);
});
}
module.exports = {
getMeals: getMeals,
getMealById: getMealById,
addMeal: addMeal,
removeMeal: removeMeal
};