Skip to content

Commit

Permalink
Update or syntax to match mysql adapter - arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg committed Aug 22, 2014
1 parent 3f98c2f commit f71f65d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
16 changes: 12 additions & 4 deletions lib/postgres.js
Expand Up @@ -352,10 +352,18 @@ PG.prototype.processWhere = function(model, conds) {
}
}
}
else if (key === 'or') {
var or = this.processWhere(model, conds[key]);
if (or.length) {
fields.push('(' + or.join(' OR ') + ')');
else if (key === 'or' && util.isArray(conds[key])) {
var ors = [];

conds[key].forEach(function(and) {
and = this.processWhere(model, and);
if (and.length) {
ors.push('(' + and.join(' AND ') + ')');
}
}.bind(this));

if (ors.length) {
fields.push('(' + ors.join(' OR ') + ')');
}
}
else if (key === 'arbitrary') {
Expand Down
26 changes: 15 additions & 11 deletions test/postgres.test.js
Expand Up @@ -121,19 +121,23 @@ it('all should support \'or\' operator', function (done) {
Post.destroyAll(function () {
Post.create({title:'First Title',userId:1}, function (err, post1) {
Post.create({title:'Second Title',userId:2}, function (err, post2) {
var where = {
or: {
title: 'First Title',
userId: 2
}
};
Post.all({where: where}, function (err, posts) {
assert.ok(!err);
assert.ok(posts.length === 2);
done();
Post.create({title:'Third Title',userId:3}, function (err, post2) {
var where = {
or: [{
title: 'First Title',
userId: 1
},{
title: 'Second Title',
userId: 2
}]
};
Post.all({where: where}, function (err, posts) {
assert.ok(!err);
assert.ok(posts.length === 2);
done();
});
});
});
});
});
});

0 comments on commit f71f65d

Please sign in to comment.