forked from rajaraodv/mysqlProjectsList
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
75 lines (60 loc) · 2.56 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
var connection;
exports.setupDBAndTable = function (conn) {
//save connection
connection = conn;
//NOTE: This particular (https://github.com/felixge/node-mysql) Mysql module,
// queues ALL 'connection.query' calls & runs queries in SEQUENCE so we don't have to nest them!
//i.e. In the below code,
//1. create projectsDB DB & *its* callback is called FIRST,
//2. then 'use projectsDB' and *its callback* is called
//3. then show tables query and *its callback* is called
//4. then 'create' table is called
//If not on Cloud Foundry, create DB 'projectsDB' and then switch to it 'projectsDB'
//Note: Cloud Foundry does these steps automatically.
if (!process.env.VCAP_SERVICES) {
connection.query('CREATE DATABASE IF NOT EXISTS projectsDB;', function (err) {
if (err) return console.log(err);
});
//Switch to 'projectsDB' database
connection.query('USE projectsDB;', function (err) {
if (err) return console.log(err);
});
}
//setup 'projects' table w/ schema
connection.query('SHOW TABLES LIKE "projects";', function (err, rows) {
if (err) return console.log(err);
//create table if it's not already setup
if (rows.length == 0) {
var sql = "" +
"CREATE TABLE projects(" +
" id INT UNSIGNED NOT NULL auto_increment," +
" name VARCHAR(50) NOT NULL default ''," +
" site VARCHAR(50) NOT NULL default ''," +
" description VARCHAR(200) NOT NULL default ''," +
" PRIMARY KEY (id)" +
");";
connection.query(sql, function (err) {
if (err) return console.log(err);
});
}
});
};
exports.addProject = function (task, callback) {
connection.query("INSERT INTO projects (name, site, description) VALUES (?, ?, ?)", [task.name, task.site, task.description], callback);
};
exports.updateProject = function (id, task, callback) {
var sql = "UPDATE projects SET name='" + task.name
+ "', site='" + task.site
+ "', description='" + task.description
+ "' WHERE id=" + id;
connection.query(sql, callback);
};
exports.getProjects = function (callback) {
connection.query("SELECT * FROM projects", callback);
};
exports.getProject = function (id, callback) {
connection.query("SELECT * FROM projects WHERE id=" + id, callback);
};
exports.deleteProject = function (id, callback) {
connection.query("DELETE FROM projects WHERE id=" + id, callback);
};