From 78b493da1490cb599d27e7fc1bbe43064c25c02c Mon Sep 17 00:00:00 2001 From: jskelton Date: Wed, 2 Jul 2014 20:13:22 +0000 Subject: [PATCH 1/6] Created ability to select specific columns --- README.md | 22 ++++++++++++++++++- lib/mysql.js | 44 ++++++++++++++++++++++++++++++++----- test/basic-querying.test.js | 38 ++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 25756ee..7146e27 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -## JugglingDB-MySQL [![Build Status](https://travis-ci.org/jugglingdb/mysql-adapter.png)](https://travis-ci.org/jugglingdb/mysql-adapter) +## JugglingDB-MySQL [![Build Status](https://travis-ci.org/jugglingdb/mysql-adapter.png)](https://travis-ci.org/jvskelton/mysql-adapter) MySQL adapter for JugglingDB. @@ -161,6 +161,26 @@ where : { } ``` +## Select specific columns + +In some cases, you may wish to select specific columns from the table, to do so simply use the `attributes` param. + +###Examples: + +returns array of column data(ex. ['1','2','3'] + + Model.all({where: {name: 'Bill'}, attributes: 'id'},cb) + +returns array of object literals(ex. [{id:'1'}, {id:'2'}, {id:'3'}] + + Model.all({where: {name: 'Bill'}, attributes: ['id']},cb) + +returns array of object literals(ex. [{id:'1', age:'25'}, {id:'2', age:'56'}, {id:'3', age: '44'}] + + Model.all({where: {name: 'Bill'}, attributes: ['id', 'age']},cb) + + + ## Connection Pooling Mysql adapter uses the pooling provided by the node-mysql module. Simply set `pool` option to true in the connection settings. diff --git a/lib/mysql.js b/lib/mysql.js index 05b7edd..46d4757 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -317,9 +317,31 @@ MySQL.prototype.escapeName = function (name) { return '`' + name.replace(/\./g, '`.`') + '`'; }; +MySQL.prototype.escapeCol = function (col) { + return '`' + col + '`'; +} + MySQL.prototype.all = function all(model, filter, callback) { - var sql = 'SELECT * FROM ' + this.tableEscaped(model); + var sql = 'SELECT '; + if (filter && filter.attributes) { + var attributes = filter.attributes; + + if(attributes && !Array.isArray(attributes)) { + attributes = [attributes]; + } + + attributes.forEach(function (attr, index) { + sql+= this.escapeCol(attr); + if (index < attributes.length -1 ) { + sql+= ', '; + } + }.bind(this)); + } else { + sql += '*'; + } + sql += ' FROM ' + this.tableEscaped(model); + var self = this; if (filter) { @@ -339,13 +361,25 @@ MySQL.prototype.all = function all(model, filter, callback) { } this.query(sql, function (err, data) { + var objs; if (err) { return callback(err, []); } - - var objs = data.map(function (obj) { - return self.fromDatabase(model, obj); - }); + if (filter && filter.attributes) { + if (!Array.isArray(filter.attributes) && attributes.length == 1) { + var valueArray = [] + data.forEach(function (value, index) { + valueArray.push(value[attributes[0]]); + }); + objs = valueArray; + } else { + objs = data; + } + } else { + objs = data.map(function (obj) { + return self.fromDatabase(model, obj); + }); + } if (filter && filter.include) { this._models[model].model.include(objs, filter.include, callback); } else { diff --git a/test/basic-querying.test.js b/test/basic-querying.test.js index b8d06f6..06c445e 100644 --- a/test/basic-querying.test.js +++ b/test/basic-querying.test.js @@ -42,6 +42,44 @@ describe('basic-query-mysql', function () { }); }); + it('should query collection with given attributes array, and return array Of Objects', function (done) { + UserData.select({ + where : { + or : [{ + order : 1 + }, { + order : 5 + }] + }, attributes: ['id'] + }, function (err, users) { + should.exists(users); + should.not.exists(err); + users.should.have.lengthOf(2); + users.should.be.instanceOf(Array); + users.pop().should.be.instanceOf(Object).and.have.property('id'); + done(); + }); + }); + + it('should query collection with given attributes array, and return array ids', function (done) { + UserData.select({ + where : { + or : [{ + order : 1 + }, { + order : 5 + }] + }, attributes: 'id' + }, function (err, users) { + should.exists(users); + should.not.exists(err); + users.should.have.lengthOf(2); + users.should.be.instanceOf(Array); + users.pop().should.be.a.Number; + done(); + }); + }); + it('should count collection where order is 1 or 5', function (done) { UserData.count({ or : [{ From dc522f904f8d1e0b536321d062417d796f1f2ca0 Mon Sep 17 00:00:00 2001 From: jskelton Date: Wed, 2 Jul 2014 20:18:43 +0000 Subject: [PATCH 2/6] reverted travic-ci url back to jugglingdb/mysql-adapter --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7146e27..e08f4af 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -## JugglingDB-MySQL [![Build Status](https://travis-ci.org/jugglingdb/mysql-adapter.png)](https://travis-ci.org/jvskelton/mysql-adapter) +## JugglingDB-MySQL [![Build Status](https://travis-ci.org/jugglingdb/mysql-adapter.png)](https://travis-ci.org/jugglingdb/mysql-adapter) MySQL adapter for JugglingDB. From be0a0a42a32f7cf07cfb5bad6b8bd9d183490746 Mon Sep 17 00:00:00 2001 From: jskelton Date: Thu, 3 Jul 2014 14:49:54 +0000 Subject: [PATCH 3/6] reverted functionality that specifies returned data based upon given attributes to match jugglingdb core changes --- lib/mysql.js | 18 +++--------------- test/basic-querying.test.js | 4 ++-- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index 46d4757..62f81d2 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -365,21 +365,9 @@ MySQL.prototype.all = function all(model, filter, callback) { if (err) { return callback(err, []); } - if (filter && filter.attributes) { - if (!Array.isArray(filter.attributes) && attributes.length == 1) { - var valueArray = [] - data.forEach(function (value, index) { - valueArray.push(value[attributes[0]]); - }); - objs = valueArray; - } else { - objs = data; - } - } else { - objs = data.map(function (obj) { - return self.fromDatabase(model, obj); - }); - } + objs = data.map(function (obj) { + return self.fromDatabase(model, obj); + }); if (filter && filter.include) { this._models[model].model.include(objs, filter.include, callback); } else { diff --git a/test/basic-querying.test.js b/test/basic-querying.test.js index 06c445e..88e0760 100644 --- a/test/basic-querying.test.js +++ b/test/basic-querying.test.js @@ -43,7 +43,7 @@ describe('basic-query-mysql', function () { }); it('should query collection with given attributes array, and return array Of Objects', function (done) { - UserData.select({ + UserData.all({ where : { or : [{ order : 1 @@ -62,7 +62,7 @@ describe('basic-query-mysql', function () { }); it('should query collection with given attributes array, and return array ids', function (done) { - UserData.select({ + UserData.all({ where : { or : [{ order : 1 From 7a65d273fe9790400801aa12196939ece0c7175b Mon Sep 17 00:00:00 2001 From: Jonathan Skelton Date: Mon, 11 Aug 2014 20:27:56 +0000 Subject: [PATCH 4/6] updated getConnection to include ssl settings --- lib/mysql.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/mysql.js b/lib/mysql.js index 62f81d2..8e6e5ab 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -99,7 +99,8 @@ function getConnection(settings) { socketPath: settings.socketPath, charset: settings.collation.toUpperCase(), supportBigNumbers: settings.supportBigNumbers, - insecureAuth: settings.insecureAuth || false + insecureAuth: settings.insecureAuth || false, + ssl: settings.ssl || {} }; if (settings.pool) { From 92f59158336e4b17435be57eaf4dd5398dd4e8c6 Mon Sep 17 00:00:00 2001 From: Jonathan Skelton Date: Mon, 11 Aug 2014 20:38:12 +0000 Subject: [PATCH 5/6] bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0768908..f7f7899 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jugglingdb-mysql", - "version": "0.0.6", + "version": "0.0.7-jvskelton.1", "description": "MySQL adapter for JugglingDB", "main": "index.js", "scripts": { From e6c6da1f554213c8f228ac543ebe1c37f19fd8e6 Mon Sep 17 00:00:00 2001 From: Jonathan Skelton Date: Tue, 12 Aug 2014 13:19:25 +0000 Subject: [PATCH 6/6] modified getConnection to check for ssl or false, bumped --- lib/mysql.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index 8e6e5ab..9db9df1 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -100,7 +100,7 @@ function getConnection(settings) { charset: settings.collation.toUpperCase(), supportBigNumbers: settings.supportBigNumbers, insecureAuth: settings.insecureAuth || false, - ssl: settings.ssl || {} + ssl: settings.ssl || false }; if (settings.pool) { diff --git a/package.json b/package.json index f7f7899..ea0262a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jugglingdb-mysql", - "version": "0.0.7-jvskelton.1", + "version": "0.0.7-jvskelton.2", "description": "MySQL adapter for JugglingDB", "main": "index.js", "scripts": {