From 97c972e3ea79066e933b688ec0060fdef421af9c Mon Sep 17 00:00:00 2001 From: Amir Jafarian Date: Sat, 9 Jan 2016 03:23:49 -0500 Subject: [PATCH] Apply feedback --- lib/connector.js | 1 + lib/sql.js | 19 +++++++------------ test/sql.test.js | 2 +- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/connector.js b/lib/connector.js index 8b7106a..bd706c0 100644 --- a/lib/connector.js +++ b/lib/connector.js @@ -271,6 +271,7 @@ Connector.defineAliases = function(cls, methodOrPropertyName, aliases) { */ Connector.defineAliases(Connector.prototype, 'execute', ['command', 'query']); +Connector.defineAliases(Connector.prototype, '_buildUpdate', 'buildUpdate'); diff --git a/lib/sql.js b/lib/sql.js index dd05b6f..3ef8bba 100644 --- a/lib/sql.js +++ b/lib/sql.js @@ -8,6 +8,8 @@ var Transaction = require('./transaction'); module.exports = SQLConnector; +function NOOP() {} + /** * Base class for connectors that connect to relational databases using SQL * @class @@ -555,10 +557,7 @@ SQLConnector.prototype.buildDelete = function(model, where, options) { */ SQLConnector.prototype.destroyAll = function(model, where, options, cb) { var stmt = this.buildDelete(model, where, options); - this._executeAlteringQuery(model, stmt.sql, stmt.params, options, function(err, data) { - if (cb) - cb(err, data); - }); + this._executeAlteringQuery(model, stmt.sql, stmt.params, options, cb || NOOP); }; // Alias to `destroyAll`. Juggler checks `destroyAll` only. @@ -668,10 +667,7 @@ SQLConnector.prototype._constructUpdateQuery = function(model, where, fields) { */ SQLConnector.prototype.update = function(model, where, data, options, cb) { var stmt = this._buildUpdate(model, where, data, options); - this._executeAlteringQuery(model, stmt.sql, stmt.params, options, function(err, data) { - if (cb) - cb(err, data); - }); + this._executeAlteringQuery(model, stmt.sql, stmt.params, options, cb || NOOP); }; /** @@ -945,9 +941,8 @@ SQLConnector.prototype.buildOrderBy = function(model, order) { * @returns {{names: Array, values: Array, properties: Array}} */ SQLConnector.prototype.buildFields = function(model, data, excludeIds) { -// var props = this.getModelDefinition(model).properties; var keys = Object.keys(data); - return this._createFields(model, data, keys, excludeIds); + return this._buildFieldsForKeys(model, data, keys, excludeIds); }; /** @@ -960,7 +955,7 @@ SQLConnector.prototype.buildFields = function(model, data, excludeIds) { SQLConnector.prototype._buildReplaceFields = function(model, data, excludeIds) { var props = this.getModelDefinition(model).properties; var keys = Object.keys(props); - return this._createFields(model, data, keys, excludeIds); + return this._buildFieldsForKeys(model, data, keys, excludeIds); }; /* @@ -970,7 +965,7 @@ SQLConnector.prototype._buildReplaceFields = function(model, data, excludeIds) { * @param {Boolean} excludeIds Exclude id properties or not, default to false * @private */ -SQLConnector.prototype._createFields = function(model, data, keys, excludeIds) { +SQLConnector.prototype._buildFieldsForKeys = function(model, data, keys, excludeIds) { var props = this.getModelDefinition(model).properties; var fields = { names: [], // field names diff --git a/test/sql.test.js b/test/sql.test.js index 0d84392..dd89e1e 100644 --- a/test/sql.test.js +++ b/test/sql.test.js @@ -281,7 +281,7 @@ describe('sql connector', function() { }); it('builds UPDATE', function() { - var sql = connector._buildUpdate('customer', {name: 'John'}, {vip: false}); + var sql = connector.buildUpdate('customer', {name: 'John'}, {vip: false}); expect(sql.toJSON()).to.eql({ sql: 'UPDATE `CUSTOMER` SET `VIP`=$1 WHERE `NAME`=$2', params: [false, 'John']