A Twitch.tv viewer reward and games system.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.4 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. /**
  2. * api:
  3. * DB([required options])
  4. * required options - {host, user, password, database}
  5. *
  6. * example:
  7. * var db = require('./lib/plugins/db.js')({
  8. * host : 'localhost',
  9. * user : 'user',
  10. * password : 'password',
  11. * database : 'database',
  12. * });
  13. */
  14. var mysql = require('mysql'),
  15. file = require('fs');
  16. //-------- Construct ---------
  17. function DB(options) {
  18. var __self = this;
  19. // config
  20. __self.host = options.host || '';
  21. __self.user = options.user || '';
  22. __self.password = options.password || '';
  23. __self.database = options.database || '';
  24. // Open escape method to other modules
  25. __self.string_escape = mysql.escape;
  26. }
  27. //-------- Methods ---------
  28. DB.prototype.start = function() {
  29. var __self = this, commands ='', viewers = '', scores = '', quotes = '';
  30. // table structure for table commands
  31. commands += 'CREATE TABLE IF NOT EXISTS `commands` (';
  32. commands += '`id` int(11) NOT NULL AUTO_INCREMENT,';
  33. commands += '`command` text COLLATE utf8_unicode_ci NOT NULL,';
  34. commands += '`text` longtext COLLATE utf8_unicode_ci NOT NULL,';
  35. commands += '`auth` int(11) NOT NULL DEFAULT \'1\',';
  36. commands += 'PRIMARY KEY (`id`)';
  37. commands += ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1';
  38. // table structure for table viewers
  39. viewers += 'CREATE TABLE IF NOT EXISTS `viewers` (';
  40. viewers += '`user` varchar(64) COLLATE utf8_unicode_ci NOT NULL,';
  41. viewers += '`points` int(11) NOT NULL,';
  42. viewers += 'PRIMARY KEY (`user`)';
  43. viewers += ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
  44. // table structure for table highscores
  45. scores += 'CREATE TABLE IF NOT EXISTS `highscores` (';
  46. scores += '`name` varchar(64) COLLATE utf8_unicode_ci NOT NULL,';
  47. scores += '`user` varchar(64) COLLATE utf8_unicode_ci,';
  48. scores += '`value` int(11) NOT NULL,';
  49. scores += '`date` timestamp ON UPDATE current_timestamp,';
  50. scores += 'PRIMARY KEY (`name`)';
  51. scores += ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
  52. // table structure for QUOTES
  53. quotes += 'CREATE TABLE IF NOT EXISTS `quotes` (';
  54. quotes += '`id` int(11) NOT NULL AUTO_INCREMENT,';
  55. quotes += '`text` longtext COLLATE utf8_unicode_ci NOT NULL,';
  56. quotes += 'PRIMARY KEY (`id`)';
  57. quotes += ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;';
  58. // execute sql, create tables if they don't exist
  59. __self.execute(commands + '; ' + viewers + '; ' + scores + '; ' + quotes, function(){});
  60. };
  61. DB.prototype.execute = function(sql, callback) {
  62. var __self = this,
  63. connection = mysql.createConnection({
  64. host : __self.host,
  65. user : __self.user,
  66. password : __self.password,
  67. database : __self.database,
  68. multipleStatements : true
  69. });
  70. // execute query
  71. connection.query(sql, function (err, rows, fields) {
  72. // error handling
  73. if (err) {
  74. file.appendFile('./../logs/error-log.txt', err.message + '\r\n' + err.stack + '\r\n', function() {});
  75. return;
  76. }
  77. // close connection
  78. connection.end();
  79. // return results
  80. callback(rows, fields);
  81. });
  82. };
  83. module.exports = function (options) {
  84. return new DB(options);
  85. };