Skip to content

Commit b58d537

Browse files
authored
Merge pull request #82 from paustint/feature-81
getFlattenedFields should allow Query or FieldSubquery or Subquery #81
2 parents 8fdd851 + d8e6ecd commit b58d537

File tree

6 files changed

+63
-10
lines changed

6 files changed

+63
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 2.1.0
4+
5+
1. The method signature for `getFlattenedFields` has changed to allow `Query | Subquery | FieldSubquery` to be passed in. this is not being considered a breaking change because it is fully backwards compatible.
6+
2. A new helper method `isFieldSubquery(value: any)` was added to allow determining if a Field is a FieldSubquery. This is used internally for `getFlattenedFields()`.
7+
38
## 2.0.0
49

510
### Summary

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ isQueryValid('SELECT Id Foo FROM Baz'); // false
4646

4747
## Utility Functions
4848

49-
| Function | Description | Arguments |
50-
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ |
51-
| getField | Convenience method to construct fields in the correct format when using `composeQuery()`. Look in the data models section below for the structure of `ComposeFieldInput`. | input: `string | ComposeFieldInput` |
52-
| isSubquery | Returns `true` if the data passed in is a subquery. | query: `Query | Subquery` |
53-
| getFlattenedFields | Flatten a Salesforce record based on the parsed SOQL Query. | soql: Query<br> config?: SoqlComposeConfig |
49+
| Function | Description | Arguments |
50+
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------- |
51+
| getField | Convenience method to construct fields in the correct format when using `composeQuery()`. Look in the data models section below for the structure of `ComposeFieldInput`. | input: `string | ComposeFieldInput` |
52+
| isSubquery | Returns `true` if the data passed in is a subquery. | query: `Query | Subquery` |
53+
| isFieldSubquery | Returns `true` if the data passed in is a FieldSubquery. | value: `any` |
54+
| getFlattenedFields | Flatten a Salesforce record based on the parsed SOQL Query. | soql: `Query | Subquery | FieldSubquery`<br> config?: `SoqlComposeConfig` |
5455

5556
**ParseQueryConfig**
5657

@@ -382,7 +383,7 @@ The following utility functions are available:
382383
2. returns one of the following data structures: `SoqlModels.FieldFunctionExpression | SoqlModels.Field | SoqlModels.FieldRelationship | SoqlModels.FieldSubquery | SoqlModels.FieldTypeOf`.
383384
2. `isSubquery(query: Query | Subquery)`
384385
1. Returns `true` if the data passed in is a subquery
385-
3. `getFlattenedFields(query: Query)`
386+
3. `getFlattenedFields(query: Query | Subquery | FieldSubquery)`
386387
1. Flatten a Salesforce record based on the parsed SOQL Query. this is useful if you have relationships in your query and want to show the results in a table, using `.` dot notation for the relationship field headings.
387388
2. Returns an array of strings.
388389
3. Refer to `tests/publicUtils.spec.ts` for usage examples.

src/api/public-utils.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
isComposeFieldTypeof,
99
isString,
1010
isSubquery,
11+
isFieldSubquery,
1112
} from '../utils';
1213

1314
export { isSubquery };
@@ -113,11 +114,14 @@ export function getField(input: string | ComposeFieldInput): SoqlModels.FieldTyp
113114
* @param [isAggregateResult] pass in true to force expr0...1 for all non-aliased functions even if field is not explicitly an aggregate expression
114115
* @returns flattened fields
115116
*/
116-
export function getFlattenedFields(query: SoqlModels.Query, isAggregateResult?: boolean): string[] {
117+
export function getFlattenedFields(
118+
query: SoqlModels.Query | SoqlModels.Subquery | SoqlModels.FieldSubquery,
119+
isAggregateResult?: boolean,
120+
): string[] {
121+
query = isFieldSubquery(query) ? query.subquery : query;
117122
const fields = query.fields;
118123
let currUnAliasedAggExp = -1;
119-
120-
let sObject = (query.sObject || '').toLowerCase();
124+
let sObject = (isSubquery(query) ? query.relationshipName : query.sObject || '').toLowerCase();
121125
let sObjectAlias = (query.sObjectAlias || '').toLowerCase();
122126

123127
const parsedFields = fields

src/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IToken } from 'chevrotain';
2-
import { FieldFunctionExpression, LiteralType, Query, Subquery, WhereClause, ValueQuery, Condition } from './api/api-models';
2+
import { FieldFunctionExpression, LiteralType, Query, Subquery, WhereClause, ValueQuery, Condition, FieldSubquery } from './api/api-models';
33
import { ComposeField, ComposeFieldFunction, ComposeFieldRelationship, ComposeFieldSubquery, ComposeFieldTypeof } from './api/public-utils';
44
import { isUndefined } from 'util';
55

@@ -76,6 +76,10 @@ export function isSubquery(query: Query | Subquery): query is Subquery {
7676
return isString((query as any).relationshipName);
7777
}
7878

79+
export function isFieldSubquery(value: any): value is FieldSubquery {
80+
return value && value.type && value.type === 'FieldSubquery';
81+
}
82+
7983
/**
8084
* Gets params from a FieldFunctionExpression. If there are multiple nested functions as multiple parameters
8185
* within another function, only the first argument will be considered.

test/public-utils-test-data.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,24 @@ export const testCases: FlattenedObjTestCase[] = [
259259
},
260260
},
261261
},
262+
{
263+
testCase: 8,
264+
expectedFields: ['Id', 'Contacts'],
265+
query: {
266+
fields: [
267+
{
268+
type: 'Field',
269+
field: 'Id',
270+
},
271+
{ type: 'FieldSubquery', subquery: { fields: [{ type: 'Field', field: 'LastName' }], relationshipName: 'Contacts' } },
272+
],
273+
sObject: 'Account',
274+
},
275+
sfdcObj: {
276+
Id: '0011800000ahbs3AAA',
277+
Name: 'Amendment Demo',
278+
BillingCity: 'Missoula',
279+
Contacts: {},
280+
},
281+
},
262282
];

test/public-utils.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as utils from '../src/api/public-utils';
22
import { expect } from 'chai';
33
import 'mocha';
44
import { testCases } from './public-utils-test-data';
5+
import { FieldSubquery, Query } from '../src';
56
const lodashGet = require('lodash.get');
67

78
describe('getField', () => {
@@ -214,4 +215,22 @@ describe('getFlattenedFields', () => {
214215
});
215216
});
216217
});
218+
219+
it(`Should allow a FieldSubquery to be passed in`, () => {
220+
const fieldSubquery: FieldSubquery = {
221+
type: 'FieldSubquery',
222+
subquery: { fields: [{ type: 'Field', field: 'LastName' }], relationshipName: 'Contacts' },
223+
};
224+
const fields = utils.getFlattenedFields(fieldSubquery);
225+
expect(fields).to.deep.equal(['LastName']);
226+
});
227+
228+
it(`Should allow a Subquery to be passed in`, () => {
229+
const fieldSubquery: FieldSubquery = {
230+
type: 'FieldSubquery',
231+
subquery: { fields: [{ type: 'Field', field: 'LastName' }], relationshipName: 'Contacts' },
232+
};
233+
const fields = utils.getFlattenedFields(fieldSubquery.subquery);
234+
expect(fields).to.deep.equal(['LastName']);
235+
});
217236
});

0 commit comments

Comments
 (0)