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.

90 lines
3.0 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. }
  25. //-------- Methods ---------
  26. DB.prototype.start = function() {
  27. var __self = this, commands ='', viewers = '', scores = '';
  28. // table structure for table commands
  29. commands += 'CREATE TABLE IF NOT EXISTS `commands` (';
  30. commands += '`id` int(11) NOT NULL AUTO_INCREMENT,';
  31. commands += '`command` text COLLATE utf8_unicode_ci NOT NULL,';
  32. commands += '`text` longtext COLLATE utf8_unicode_ci NOT NULL,';
  33. commands += '`auth` int(11) NOT NULL DEFAULT \'1\',';
  34. commands += 'PRIMARY KEY (`id`)';
  35. commands += ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1';
  36. // table structure for table viewers
  37. viewers += 'CREATE TABLE IF NOT EXISTS `viewers` (';
  38. viewers += '`user` varchar(64) COLLATE utf8_unicode_ci NOT NULL,';
  39. viewers += '`points` int(11) NOT NULL,';
  40. viewers += 'PRIMARY KEY (`user`)';
  41. viewers += ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci';
  42. // table structure for table highscores
  43. scores += 'CREATE TABLE IF NOT EXISTS `highscores` (';
  44. scores += '`name` varchar(64) COLLATE utf8_unicode_ci NOT NULL,';
  45. scores += '`user` varchar(64) COLLATE utf8_unicode_ci,';
  46. scores += '`value` int(11) NOT NULL,';
  47. scores += '`date` timestamp ON UPDATE current_timestamp,';
  48. scores += 'PRIMARY KEY (`name`)';
  49. scores += ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;';
  50. // execute sql, create tables if they don't exist
  51. __self.execute(commands + '; ' + viewers + '; ' + scores, function(){});
  52. };
  53. DB.prototype.execute = function(sql, callback) {
  54. var __self = this,
  55. connection = mysql.createConnection({
  56. host : __self.host,
  57. user : __self.user,
  58. password : __self.password,
  59. database : __self.database,
  60. multipleStatements : true
  61. });
  62. // execute query
  63. connection.query(sql, function (err, rows, fields) {
  64. // error handling
  65. if (err) {
  66. file.appendFile(__dirname+'/../logs/error-log.txt', err.message + '\r\n' + err.stack + '\r\n', function() {});
  67. return;
  68. }
  69. // close connection
  70. connection.end();
  71. // return results
  72. callback(rows, fields);
  73. });
  74. };
  75. module.exports = function (options) {
  76. return new DB(options);
  77. };