diff --git a/browserid/lib/db_mysql.js b/browserid/lib/db_mysql.js index d11b31cb25f36734ac47c150ffcec4579507edd8..a3f97297ade4259b115db26dfb0c59f229ffb903 100644 --- a/browserid/lib/db_mysql.js +++ b/browserid/lib/db_mysql.js @@ -1,11 +1,52 @@ +/* This is a mysql driver for the browserid server. It maps the data + * storage requirements of browserid onto a relational schema. This + * driver is intended to be fast and scalable. + */ +/* + * The Schema: + * + * +--- user ------+ +--- email ----+ +----- key ------+ + * |*int id | <-\ |*int id | <-\ |*int id | + * | text password | \- |*int user | \-- |*int email | + * +---------------+ | text address | | text key | + * +--------------+ | int expires | + * +----------------+ + * + * + * + */ -exports.open = function() { - throw "not implemented"; +const +mysql = require('mysql'); + +var client = new mysql.Client(); + +// open & create the mysql database +exports.open = function(cfg, cb) { + // mysql config requires + const defParams = { + host: '127.0.0.1', + port: "3306", + user: 'test', + password: 'pass' + }; + + Object.keys(defParams).forEach(function(param) { + client[param] = cfg[param] ? config.param : defParams[param]; + }); + + var database = cfg.database ? cfg.database : 'browserid'; + + client.connect(function(error) { + cb(error); + }); }; -exports.close = function() { - throw "not implemented"; +exports.close = function(cb) { + client.end(function(err) { + if (cb) cb(err); + }); }; exports.emailKnown = function() { diff --git a/browserid/tests/db-test.js b/browserid/tests/db-test.js index 1feb09d2130d35ca93be2398b0da84456e3e7ed2..eba4e9f73eb776e645b78ad46a69ced9363715f8 100755 --- a/browserid/tests/db-test.js +++ b/browserid/tests/db-test.js @@ -15,6 +15,29 @@ suite.options.error = false; function addTestsForDriver(driver) { var dbPath = temp.path({suffix: '.db'}); + if (driver === 'mysql') { + // let's check to see if we can connect and render a nice + // error message if not. For community members making casual + // contributions, we should expect that they might not want to + // set up mysql. + suite.addBatch({ + "mysql server": { + topic: function() { db.open({driver: driver}, this.callback) }, + "accepting connections": function(err) { + if (err) { + console.log("SKIPPING MYSQL TESTING: I cannot connect to the mysql database (" + err.message + ")"); + } + }, + "connection closes": { + topic: function() { db.close(this.callback); }, + "without error": function(err) { + assert.isNull(err); + } + } + } + }); + } + suite.addBatch({ "onReady": { topic: function() { db.onReady(this.callback); }, @@ -400,7 +423,9 @@ files = fs.readdirSync(path.join(__dirname, "..", "lib")); files.forEach(function(f) { var m = /^db_(.+)\.js$/.exec(f); - if (m) addTestsForDriver(m[1]); + if (m) { + addTestsForDriver(m[1]); + } }); // run or export the suite.