From f71f65dfd0e24afe56356879fda0da62441731fa Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Fri, 22 Aug 2014 00:44:45 -0700 Subject: [PATCH] Update or syntax to match mysql adapter - arrays https://github.com/jugglingdb/mysql-adapter --- lib/postgres.js | 16 ++++++++++++---- test/postgres.test.js | 26 +++++++++++++++----------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/lib/postgres.js b/lib/postgres.js index de019e7..3496395 100644 --- a/lib/postgres.js +++ b/lib/postgres.js @@ -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') { diff --git a/test/postgres.test.js b/test/postgres.test.js index 2abe3cd..7fbe55f 100644 --- a/test/postgres.test.js +++ b/test/postgres.test.js @@ -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(); + }); }); }); }); }); }); -