Skip to content

Instantly share code, notes, and snippets.

@TrevorBasinger
Created April 22, 2015 18:18
Show Gist options
  • Save TrevorBasinger/940accf8f63cf92395ef to your computer and use it in GitHub Desktop.
Save TrevorBasinger/940accf8f63cf92395ef to your computer and use it in GitHub Desktop.
var
Future = require ('data.task'),
//Future = require ('data.future'),
//Future = require ('fantasy-promises'),
R = require ('ramda'),
M = require ('control.monads'),
mysql = require ('mysql'),
log = function (x) { console.log (x); },
logI = function (x) { log (x); return x; },
logM = R.curry (function (m, x) { logI (m); logI (x); return x; }),
// defaultHandler :: (b -> c) -> (a -> c) -> b -> a -> c
defaultHandler = R.curry (function (reject, resolve, err, data) {
if (err) { return reject (err); }
return resolve (data);
}),
// Ends connection and passes through result
// endConnection :: Connection -> a -> a
endConnection = R.curry (function (conn, x) { conn.end(); return x; }),
// getConnection :: Config -> Connection
getConnection = function (conf) { return mysql.createConnection (conf); },
// query :: Config -> Future Either Error [a]
query = R.curry (function (conf, sql) {
return new Future (function (reject, resolve) {
// Using Either ADT would make results more reasonable.
var conn = mysql.createConnection (conf);
var handler = defaultHandler (reject, R.compose (resolve, endConnection (conn)));
conn.query (sql, handler);
});
}),
// queryC :: Connection -> Future Either Error [a]
queryC = R.curry (function (conn, sql) {
return new Future (function (reject, resolve) {
// Using Either ADT would make results more reasonable.
var handler = defaultHandler (reject, resolve);
conn.query (sql, handler);
});
}),
/* CONFIG */
conf = 'mysql://localhost/test',
num = 5000,
nil = null;
(function main () {
var conn = getConnection (conf);
var xs = R.repeat (query (conf, 'select * from vendors limit 1'), num);
M.sequence (Future, xs).fork (log, log);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment