Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created ability to query specific colums #91

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
31 changes: 27 additions & 4 deletions lib/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
38 changes: 38 additions & 0 deletions test/basic-querying.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 : [{
Expand Down