// namespace var WTF = {}; // -------------------------------------------------------------------- // database configuration WTF.dbConfig = { 'username': 'root', 'password': 'root', 'database': 'poopy', 'port': '/Applications/MAMP/tmp/mysql/mysql.sock' } // -------------------------------------------------------------------- // database class definition WTF.DB = function() { this.client = require('mysql').createClient({ user: WTF.dbConfig.username, password: WTF.dbConfig.password, port: WTF.dbConfig.port }); this.query('USE ' + WTF.dbConfig.database); }; WTF.DB.prototype = { query: function(sql, callback) { return this.client.query(sql, callback); }, close: function() { this.client.end(); } }; // -------------------------------------------------------------------- // query tests var db = new WTF.DB(), TEST_TABLE = 'test_table'; db.query('DROP TABLE IF EXISTS ' + TEST_TABLE); db.query('CREATE TABLE ' + TEST_TABLE + '( id INT(11) AUTO_INCREMENT, ' + ' title VARCHAR(255), ' + ' text TEXT, ' + ' created DATETIME, ' + ' PRIMARY KEY (id) )'); db.query('INSERT INTO ' + TEST_TABLE + ' ' + ' SET title = ?, text = ?, created = ?', ['super cool', 'this is a nice text', '2010-08-16 10:00:23']); db.query('INSERT INTO ' + TEST_TABLE + ' ' + ' SET title = ?, text = ?, created = ?', ['another entry', 'because 2 entries make a better test', '2010-08-16 12:42:15']); db.query('SELECT * FROM ' + TEST_TABLE, function selectCb(err, results, fields) { if (err) { throw err; } console.log(results); console.log(fields); }); db.close();