-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
87 lines (78 loc) · 2.69 KB
/
db.js
File metadata and controls
87 lines (78 loc) · 2.69 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Simple OO MySQL helper module.
*
* Copyright (C) 2017 Ferenc Kretz <ferkretz@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var mysql = require('mysql');
// Exports instance
module.exports = (function () {
// Public functions
var db = {};
// Private variables
var connectionLimit;
var host;
var user;
var database;
var password;
var debug;
var pool;
// You can specify version number
db.version = '0.0.1';
// Initializing with optional (default) values
db.init = function (opts) {
connectionLimit = (typeof opts.connectionLimit === 'number') ? opts.connectionLimit : 100;
host = (typeof opts.host === 'string') ? opts.host : 'localhost';
user = (typeof opts.user === 'string') ? opts.user : 'root';
password = (typeof opts.password === 'string') ? opts.password : ''; // empty password
database = (typeof opts.database === 'string') ? opts.database : 'database';
debug = (typeof opts.debug === 'string') ? opts.debug : false; // we don't use debug
pool = mysql.createPool({
connectionLimit: connectionLimit,
host: host,
user: user,
password: password,
database: database,
debug: debug
});
};
// Simple SQL query
db.query = function (query, values, callback) {
pool.getConnection(function (err, connection) {
if (err) {
return callback(err, null);
}
connection.query(query, values, function (err, results) {
connection.release();
if (err) {
return callback(err, null);
}
callback(null, results);
});
});
};
// Table list
db.listTables = function (callback) {
this.query('SHOW TABLES', [], callback);
};
// Describe table
db.describeTable = function (table, callback) {
this.query('DESCRIBE ??', [table], callback);
};
db.getDatabase = function () {
return database;
};
return db;
}());