Repository: paustint/soql-parser-js (ts)

Issue:
If an empty orderby array is included, the composed query is invalid
<!--
  Please include the following classification.
  If your classification is not added, you can modify the list below
-->
- [ ] Feature
- [x] Bug Request

<!--If this is a bug, please include the steps to reproduce the bug-->
## Description
If an empty array is provided when composing a query, then `ORDER BY` should be omitted from the query.

The following query results in an invalid generated query:

```json
{
  "fields": [
    {
      "type": "Field",
      "field": "Id"
    }
  ],
  "sObject": "Account",
  "orderBy": [],
  "limit": 1,
  "offset": 5
}
```

```sql
SELECT Id
FROM Account
ORDER BY 
LIMIT 1
OFFSET 5
```

Pull request:
If an empty orderby array is included, the composed query is invalid #87
Added check to ensure order by is only added if when an object or array with more than one element is provided

resolves #87

Patch:
diff --git a/src/composer/composer.ts b/src/composer/composer.ts
index 41bd84d..9c7d2f3 100644
--- a/src/composer/composer.ts
+++ b/src/composer/composer.ts
@@ -14,6 +14,7 @@ import {
 import * as utils from '../utils';
 import { FieldData, Formatter, FormatOptions } from '../formatter/formatter';
 import { parseQuery } from '../parser/visitor';
+import { isArray } from 'util';
 
 export interface SoqlComposeConfig {
   logging: boolean; // default=false
@@ -186,7 +187,7 @@ export class Compose {
       }
     }
 
-    if (query.orderBy) {
+    if (query.orderBy && (!isArray(query.orderBy) || query.orderBy.length > 0)) {
       output += this.formatter.formatClause('ORDER BY');
       output += ` ${this.parseOrderBy(query.orderBy)}`;
       this.log(output);


Test patch:
diff --git a/test/test.spec.ts b/test/test.spec.ts
index 7dfb8dd..73f59e7 100644
--- a/test/test.spec.ts
+++ b/test/test.spec.ts
@@ -98,6 +98,20 @@ describe('compose queries', () => {
     const soqlQuery = composeQuery(query);
     expect(soqlQuery).to.equal(`SELECT Id FROM Account WHERE Foo IN ('1', '2', '3') OR Bar = 'foo'`);
   });
+  it('Should not add extraneous order by clauses', () => {
+    const query: Query = {
+      fields: [
+        {
+          type: 'Field',
+          field: 'Id',
+        },
+      ],
+      sObject: 'Account',
+      orderBy: [],
+    };
+    const soqlQuery = composeQuery(query);
+    expect(soqlQuery).to.equal(`SELECT Id FROM Account`);
+  });
 });
 
 describe('format queries', () => {