Permalink
Browse files

Adding alias methods all(), one(), and query()

  • Loading branch information...
1 parent 042ec29 commit 962d204ad6c6b536d8c74fc969494d3dfd2e8c8b @BryanDonovan BryanDonovan committed Sep 17, 2012
Showing with 173 additions and 152 deletions.
  1. +15 −12 .jshintrc
  2. +6 −9 lib/easy_client.js
  3. +18 −9 lib/easy_mysql.js
  4. +134 −122 test/easy_mysql.unit.js
View
27 .jshintrc
@@ -18,18 +18,21 @@
"dojo" : false,
"predef" : [ // Extra globals.
- "event",
- "exports",
- "require",
- "process",
- "module",
- "Buffer",
- "logger",
- "global",
- "__dirname",
- "describe",
- "it",
- "beforeEach"
+ "describe",
+ "it",
+ "beforeEach",
+ "event",
+ "exports",
+ "require",
+ "process",
+ "module",
+ "Buffer",
+ "logger",
+ "global",
+ "__dirname",
+ "describe",
+ "it",
+ "beforeEach"
],
View
15 lib/easy_client.js
@@ -7,9 +7,8 @@ var easy_pool = require('./easy_pool');
* @property {object} pool - The connection pool to use (if set).
*/
function EasyClient(client, pool) {
- var self = this;
- self.client = client;
- self.pool = pool;
+ this.client = client;
+ this.pool = pool;
}
/**
@@ -20,20 +19,18 @@ function EasyClient(client, pool) {
* @returns {object} - The result returned by node-mysql.
*/
EasyClient.prototype.query = function (sql, query_params, cb) {
- var self = this;
- return self.client.query(sql, query_params, cb);
+ return this.client.query(sql, query_params, cb);
};
/**
* If we're using a pool, release the client.
* If we're using a client directly, call its end() function
*/
EasyClient.prototype.end = function () {
- var self = this;
- if (self.pool) {
- self.pool.release(self.client);
+ if (this.pool) {
+ this.pool.release(this.client);
} else {
- self.client.end();
+ this.client.end();
}
};
View
27 lib/easy_mysql.js
@@ -27,7 +27,6 @@ function EasyMySQL(settings) {
* @private
*/
this.fetch_client = function (cb) {
- var self = this;
EasyClient.fetch(self.settings, function (err, client) {
if (err) {
cb(err);
@@ -153,25 +152,35 @@ EasyMySQL.prototype.get_all = function (sql, query_params, cb) {
});
};
+/**
+ * Alias for execute
+ * @see EasyMySQL#execute
+ */
+EasyMySQL.prototype.query = EasyMySQL.prototype.execute;
+
+/*
+ * Alias for get_one
+ * @see EasyMySQL#get_one
+ */
+EasyMySQL.prototype.getOne = EasyMySQL.prototype.get_one;
/**
* Alias for get_one
* @see EasyMySQL#get_one
*/
-EasyMySQL.prototype.getOne = function (sql, query_params, cb) {
- var self = this;
- return self.get_one(sql, query_params, cb);
-};
+EasyMySQL.prototype.one = EasyMySQL.prototype.get_one;
/**
* Alias for get_all
* @see EasyMySQL#get_all
*/
-EasyMySQL.prototype.getAll = function (sql, query_params, cb) {
- var self = this;
- return self.get_all(sql, query_params, cb);
-};
+EasyMySQL.prototype.getAll = EasyMySQL.prototype.get_all;
+/**
+ * Alias for get_all
+ * @see EasyMySQL#get_all
+ */
+EasyMySQL.prototype.all = EasyMySQL.prototype.get_all;
/**
* Directly establish a single connection for each query.
View
256 test/easy_mysql.unit.js
@@ -17,172 +17,184 @@ describe('EasyMySQL', function () {
});
});
- describe("execute", function () {
- describe("valid queries", function () {
- describe("with params", function () {
- it("passes query and params to mysql", function (done) {
- var sql = "insert into widgets(name) values (?)";
-
- easy_mysql.execute(sql, ['foo'], function (err, result) {
- assert.ifError(err);
- assert.strictEqual(err, null);
- assert.ok(result);
- var sql = "select * from widgets";
- easy_mysql.execute(sql, function (err, results) {
- assert.equal(results[0].name, 'foo');
- done();
+ var execute_funcs = ['execute', 'query'];
+
+ execute_funcs.forEach(function (meth) {
+ describe(meth, function () {
+ describe("valid queries", function () {
+ describe("with params", function () {
+ it("passes query and params to mysql", function (done) {
+ var sql = "insert into widgets(name) values (?)";
+
+ easy_mysql[meth](sql, ['foo'], function (err, result) {
+ assert.ifError(err);
+ assert.strictEqual(err, null);
+ assert.ok(result);
+ var sql = "select * from widgets";
+ easy_mysql.execute(sql, function (err, results) {
+ assert.equal(results[0].name, 'foo');
+ done();
+ });
});
});
});
- });
- describe("without params", function () {
- it("passes query to mysql", function (done) {
- var sql = "insert into widgets(name) values ('bob')";
-
- easy_mysql.execute(sql, function (err, result) {
- assert.ifError(err);
- assert.strictEqual(err, null);
- assert.ok(result);
- var sql = "select * from widgets";
- easy_mysql.execute(sql, function (err, results) {
- assert.equal(results[0].name, 'bob');
- done();
+ describe("without params", function () {
+ it("passes query to mysql", function (done) {
+ var sql = "insert into widgets(name) values ('bob')";
+
+ easy_mysql[meth](sql, function (err, result) {
+ assert.ifError(err);
+ assert.strictEqual(err, null);
+ assert.ok(result);
+ var sql = "select * from widgets";
+ easy_mysql.execute(sql, function (err, results) {
+ assert.equal(results[0].name, 'bob');
+ done();
+ });
});
});
});
});
- });
- describe("with invalid query", function () {
- it("calls done with error", function (done) {
- var sql = "BOGUSselect * from widgets where id = ?";
- easy_mysql.execute(sql, [1], function (err, result) {
- assert.ok(err instanceof Error);
- assert.strictEqual(result, null);
- done();
+ describe("with invalid query", function () {
+ it("calls done with error", function (done) {
+ var sql = "BOGUSselect * from widgets where id = ?";
+ easy_mysql[meth](sql, [1], function (err, result) {
+ assert.ok(err instanceof Error);
+ assert.strictEqual(result, null);
+ done();
+ });
});
});
});
});
- describe("get_one", function () {
- beforeEach(function (done) {
- var sql = "insert into widgets(name) values ('bob'), ('jim')";
- easy_mysql.execute(sql, function (err, result) {
- assert.ifError(err);
- done();
+ var get_one_funcs = ['get_one', 'getOne', 'one'];
+
+ get_one_funcs.forEach(function (getter) {
+ describe(getter, function () {
+ beforeEach(function (done) {
+ var sql = "insert into widgets(name) values ('bob'), ('jim')";
+ easy_mysql.execute(sql, function (err, result) {
+ assert.ifError(err);
+ done();
+ });
});
- });
- describe("valid queries", function () {
- describe("with params", function () {
- it("passes query and params to mysql, returns single object", function (done) {
- var sql = "select * from widgets where name = ?";
- easy_mysql.get_one(sql, ['bob'], function (err, result) {
- assert.ifError(err);
- assert.strictEqual(err, null);
- assert.equal(result.name, 'bob');
- done();
+ describe("valid queries", function () {
+ describe("with params", function () {
+ it("passes query and params to mysql, returns single object", function (done) {
+ var sql = "select * from widgets where name = ?";
+ easy_mysql[getter](sql, ['bob'], function (err, result) {
+ assert.ifError(err);
+ assert.strictEqual(err, null);
+ assert.equal(result.name, 'bob');
+ done();
+ });
});
});
- });
- describe("without params", function (done) {
- it("passes query to mysql, returns single object", function (done) {
- var sql = "select name from widgets order by name desc limit 1";
- easy_mysql.get_one(sql, function (err, result) {
- assert.ifError(err);
- assert.strictEqual(err, null);
- assert.equal(result.name, 'jim');
- done();
+ describe("without params", function (done) {
+ it("passes query to mysql, returns single object", function (done) {
+ var sql = "select name from widgets order by name desc limit 1";
+ easy_mysql[getter](sql, function (err, result) {
+ assert.ifError(err);
+ assert.strictEqual(err, null);
+ assert.equal(result.name, 'jim');
+ done();
+ });
+ });
+ });
+
+ describe("when no results found", function () {
+ it("returns null", function (done) {
+ var sql = "select * from widgets where name = ?";
+ easy_mysql[getter](sql, ['not real'], function (err, result) {
+ assert.ifError(err);
+ assert.strictEqual(err, null);
+ assert.strictEqual(result, null);
+ done();
+ });
});
});
});
- describe("when no results found", function () {
- it("returns null", function (done) {
- var sql = "select * from widgets where name = ?";
- easy_mysql.get_one(sql, ['not real'], function (err, result) {
- assert.ifError(err);
- assert.strictEqual(err, null);
+ describe("with invalid query", function () {
+ it("calls done with error", function (done) {
+ var sql = "BOGUSselect * from widgets";
+ easy_mysql[getter](sql, function (err, result) {
+ assert.ok(err instanceof Error);
assert.strictEqual(result, null);
done();
});
});
});
});
+ });
- describe("with invalid query", function () {
- it("calls done with error", function (done) {
- var sql = "BOGUSselect * from widgets";
- easy_mysql.get_one(sql, function (err, result) {
- assert.ok(err instanceof Error);
- assert.strictEqual(result, null);
+ var get_all_funcs = ['get_all', 'getAll', 'all'];
+
+ get_all_funcs.forEach(function (getter) {
+ describe(getter, function () {
+ beforeEach(function (done) {
+ var sql = "insert into widgets(name) values ('bob'), ('jim')";
+ easy_mysql.execute(sql, function (err, result) {
+ assert.ifError(err);
done();
});
});
- });
- });
-
- describe("get_all", function () {
- beforeEach(function (done) {
- var sql = "insert into widgets(name) values ('bob'), ('jim')";
- easy_mysql.execute(sql, function (err, result) {
- assert.ifError(err);
- done();
- });
- });
- describe("valid queries", function () {
- describe("with params", function () {
- it("passes query and params to mysql, returns results array", function (done) {
- var sql = "select * from widgets where name = ?";
- easy_mysql.get_all(sql, ['bob'], function (err, results) {
- assert.ifError(err);
- assert.strictEqual(err, null);
- assert.ok(Array.isArray(results));
- assert.equal(results[0].name, 'bob');
- done();
+ describe("valid queries", function () {
+ describe("with params", function () {
+ it("passes query and params to mysql, returns results array", function (done) {
+ var sql = "select * from widgets where name = ?";
+ easy_mysql[getter](sql, ['bob'], function (err, results) {
+ assert.ifError(err);
+ assert.strictEqual(err, null);
+ assert.ok(Array.isArray(results));
+ assert.equal(results[0].name, 'bob');
+ done();
+ });
});
});
- });
- describe("without params", function () {
- it("passes query to mysql, returns results array", function (done) {
- var sql = "select name from widgets order by name desc";
- easy_mysql.getAll(sql, function (err, results) {
- assert.ifError(err);
- assert.strictEqual(err, null);
- assert.ok(Array.isArray(results));
- assert.equal(results.length, 2);
- assert.equal(results[0].name, 'jim');
- done();
+ describe("without params", function () {
+ it("passes query to mysql, returns results array", function (done) {
+ var sql = "select name from widgets order by name desc";
+ easy_mysql[getter](sql, function (err, results) {
+ assert.ifError(err);
+ assert.strictEqual(err, null);
+ assert.ok(Array.isArray(results));
+ assert.equal(results.length, 2);
+ assert.equal(results[0].name, 'jim');
+ done();
+ });
});
});
- });
- describe("when no results found", function () {
- it("returns empty array", function (done) {
- var sql = "select * from widgets where name = ?";
- easy_mysql.get_all(sql, ['not real'], function (err, results) {
- assert.ifError(err);
- assert.strictEqual(err, null);
- assert.ok(Array.isArray(results));
- assert.equal(results.length, 0);
- done();
+ describe("when no results found", function () {
+ it("returns empty array", function (done) {
+ var sql = "select * from widgets where name = ?";
+ easy_mysql[getter](sql, ['not real'], function (err, results) {
+ assert.ifError(err);
+ assert.strictEqual(err, null);
+ assert.ok(Array.isArray(results));
+ assert.equal(results.length, 0);
+ done();
+ });
});
});
});
- });
- describe("with invalid query", function () {
- it("calls done with error", function (done) {
- var sql = "BOGUSselect * from widgets";
- easy_mysql.get_all(sql, function (err, results) {
- assert.ok(err instanceof Error);
- assert.strictEqual(results, null);
- done();
+ describe("with invalid query", function () {
+ it("calls done with error", function (done) {
+ var sql = "BOGUSselect * from widgets";
+ easy_mysql[getter](sql, function (err, results) {
+ assert.ok(err instanceof Error);
+ assert.strictEqual(results, null);
+ done();
+ });
});
});
});

0 comments on commit 962d204

Please sign in to comment.