diff --git a/lib/sql.js b/lib/sql.js index 4176996..484f4cd 100644 --- a/lib/sql.js +++ b/lib/sql.js @@ -1216,6 +1216,9 @@ SQLConnector.prototype.buildOrderBy = function(model, order) { clauses.push(self.columnEscaped(model, t[0]) + ' ' + t[1]); } } + if (!clauses.length) { + return ''; + } return 'ORDER BY ' + clauses.join(','); }; diff --git a/test/sql.test.js b/test/sql.test.js index 7243f29..1b323b6 100644 --- a/test/sql.test.js +++ b/test/sql.test.js @@ -270,6 +270,16 @@ describe('sql connector', function() { expect(orderBy).to.eql('ORDER BY `NAME` ASC,`VIP` DESC'); }); + it('builds order by with empty array', function() { + const orderBy = connector.buildOrderBy('customer', []); + expect(orderBy).to.eql(''); + }); + + it('builds order by with empty string', function() { + const orderBy = connector.buildOrderBy('customer', ''); + expect(orderBy).to.eql(''); + }); + it('builds fields for columns', function() { const fields = connector.buildFields('customer', {name: 'John', vip: true, unknown: 'Random'});