Skip to content

Commit

Permalink
fix: disregard empty and/or fields
Browse files Browse the repository at this point in the history
Signed-off-by: Abhilash Murthy <abhilashmurthy92@gmail.com>
  • Loading branch information
abhilashmurthy authored and dhmlau committed Sep 24, 2021
1 parent ce2310e commit ecf91b5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/postgresql.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,11 +625,13 @@ PostgreSQL.prototype._buildWhere = function(model, where) {
branches.push(stmtForClause.sql);
}
}
stmt.merge({
sql: '(' + branches.join(' ' + key.toUpperCase() + ' ') + ')',
params: branchParams,
});
whereStmts.push(stmt);
if (branches.length > 0) {
stmt.merge({
sql: '(' + branches.join(' ' + key.toUpperCase() + ' ') + ')',
params: branchParams,
});
whereStmts.push(stmt);
}
continue;
}
// The value is not an array, fall back to regular fields
Expand Down
9 changes: 9 additions & 0 deletions test/postgresql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,15 @@ describe('postgresql connector', function() {
});
});

it('should disregard empty and/or fields', function(done) {
Post.find({where: {and: []}}, function(err, post) {
should.not.exist(err);
should.exist(post);
post.length.should.not.equal(0);
done();
});
});

it('should preserve order of and/or in where', async function() {
await Post.create({title: 'T3', content: 'C3', approved: false});
// WHERE (title='T3' OR approved=false) AND (content='C2')
Expand Down

0 comments on commit ecf91b5

Please sign in to comment.