· 8 years ago · Jan 30, 2018, 12:58 AM
1// init variable defining and config value imports for mysql
2var mysql = require('mysql');
3
4// Define your connection settings here
5var connection = mysql.createConnection({
6 host: 'localhost',
7 user: 'username',
8 password: 'password',
9 database: 'something'
10});
11
12
13//Testing if the connection is sound for use
14connection.connect(function(err, results) {
15 if(err) { // if there was an error
16 console.error("Error: " + err.message + ' | ' + "Error connecting to the database!");
17 } else { // otherwise we have connected
18 console.log(results.message + "Database connected successfully!")
19 }
20 });
21
22// Variable assignment for SQL table injection and construction
23
24// PlayerInfo Table Creation
25let createPlayers = `CREATE TABLE IF NOT EXISTS PlayerInfo(
26 uuid int primary key auto_increment,
27 playername varchar(255) not null,
28 admin tinyint(1) not null default 0,
29 dev tinyint(1) not null default 0,
30 owner tinyint(1) not null default 0,
31 moderator tinyint(1) not null default 0,
32 wallet int(10) not null default 100,
33 bankbalance int(20) not null default 0,
34 kills int(20) not null default 0,
35 deaths int(20) not null default 0,
36 inventory varchar(255) not null,
37 licenses varchar(255) not null,
38 weapons varchar(255) not null,
39 ownedvehicles varchar(255) not null
40 )
41 ENGINE=InnoDB DEFAULT CHARSET=utf8`;
42
43// Jobs Table | This is a placeholder for developers who want to add in jobs
44let createJobs = `CREATE TABLE IF NOT EXISTS Jobs(
45 jobid int primary key auto_increment,
46 title varchar(255) not null,
47 payrate int(10) not null default 0,
48 rank varchar(255) not null,
49 whitelist tinyint(1) not null default 0
50 )
51 ENGINE=InnoDB DEFAULT CHARSET=utf8`;
52
53// SQL table injection
54connection.query(createPlayers, function(err, results, fields) {
55 if(!err) {
56 console.log(results.message + "Successfully injected SQL table(s)");
57 } else console.error("Error: " + err.message + ' | ' + "SQL injection failed");
58});
59
60// Closing the connection as a failsafe to prevent possible redundant or rogue connections
61connection.end(function(err) {
62 if (!err) {
63 console.log("MySQL session closed successfully");
64 } else console.error("Error: " + err.message + ' | ' + "SQL session failed to terminate");
65 });