-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_helper.js
61 lines (52 loc) · 1.82 KB
/
db_helper.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
var credentials = require("./db_credentials");
var mysql = require("mysql");
var files = require("fs");
var handler = function(err) {
if(err) {
throw err;
}
}
exports.createTables = function() {
var params = credentials.connectionParams;
params.multipleStatements = true;
var connection = mysql.createConnection(params);
connection.connect(handler);
files.readFile("./create_tables.sql", "ascii", function(err, data) {
handler(err);
connection.query(data, handler);
console.log("Successfully created tables");
});
};
exports.deleteTables = function() {
var params = credentials.connectionParams;
params.multipleStatements = true;
var connection = mysql.createConnection(params);
connection.connect(handler);
files.readFile("./drop_tables.sql", "ascii", function(err, data) {
handler(err);
connection.query(data, handler);
console.log("Successfully deleted tables");
});
};
exports.insertTestData = function() {
var params = credentials.connectionParams;
params.multipleStatements = true;
var connection = mysql.createConnection(params);
connection.connect(handler);
files.readFile("./test_data.sql", "ascii", function(err, data) {
handler(err);
connection.query(data, handler);
console.log("Successfully inserted test data");
});
};
exports.createDB = function() {
var connection = mysql.createConnection(credentials.rootConnectionParams);
connection.connect(handler);
connection.query("CREATE DATABASE testbank;", handler);
connection.query("CREATE USER 'testbank_user'@'" + credentials.rootConnectionParams.host + "' IDENTIFIED BY 'deltataudeltabetapi';", handler);
connection.query("GRANT ALL PRIVILEGES ON testbank.* TO 'testbank_user';", handler);
console.log("Successfully created database \"testbank\" and created user \"testbank_user\" with full privileges");
};
var main = function() {
};
main();