diff --git a/README.md b/README.md index 25756ee..e08f4af 100644 --- a/README.md +++ b/README.md @@ -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..9db9df1 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 || false }; if (settings.pool) { @@ -317,9 +318,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,11 +362,11 @@ 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) { + objs = data.map(function (obj) { return self.fromDatabase(model, obj); }); if (filter && filter.include) { diff --git a/package.json b/package.json index 0768908..ea0262a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jugglingdb-mysql", - "version": "0.0.6", + "version": "0.0.7-jvskelton.2", "description": "MySQL adapter for JugglingDB", "main": "index.js", "scripts": { diff --git a/test/basic-querying.test.js b/test/basic-querying.test.js index b8d06f6..88e0760 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.all({ + 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.all({ + 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 : [{