<img src="./assets/soql-parser-js-logo.svg">

![build](https://github.com/jetstreamapp/soql-parser-js/actions/workflows/ci.yml/badge.svg)
[![npm version](https://badge.fury.io/js/@jetstreamapp%2Fsoql-parser-js.svg)](https://badge.fury.io/js/@jetstreamapp%2Fsoql-parser-js)

## Description

**This library allows parsing and composing SOQL queries from Salesforce using JavaScript or Typescript.**

### Installation

👉 **As of version 6.0.0**, this package is now part of the `@jetstreamapp` npm organization.

```bash
npm install @jetstreamapp/soql-parser-js
```

**Available Features:**

1. Parse SOQL queries into a common `Query` data structure.
2. Deterministically compose a `Query` data structure back into a SOQL query string.
3. Validate a query to check if the syntax is valid.
   1. _Even if a query is returned as valid, it might still be invalid based on your Salesforce configuration_

Migrating from version 1 to version 2? [Check out the changelog](CHANGELOG.md#200) for a full list of changes.

Migrating from version 2 to version 3? [Check out the changelog](CHANGELOG.md#300) for a full list of changes.

## Documentation

**[Read the documentation on our docs site](https://paustint.github.io/soql-parser-js/)**.

## Compatibility

**Node**: version 11 or higher, or a polyfill for [Array.flat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat)  
**Browser**: Tested in all modern browsers, may not work with older browsers.

The **commander** dependency is only required for the cli, the other two dependencies **chevrotain** and **lodash.get** are bundled with the non-cli code.

## Quick Start

```javascript
import { parseQuery, composeQuery, isQueryValid } from '@jetstreamapp/soql-parser-js';

const query = parseQuery(`SELECT Id FROM Account WHERE Id = 'FOO'`);
console.log('query', query);

const soql = composeQuery(query);
console.log('soql', soql); // SELECT Id FROM Account WHERE Id = 'FOO'

isQueryValid('SELECT Id, Foo FROM Baz'); // true
isQueryValid('SELECT Id Foo FROM Baz'); // false
```

## Available Features

| Function     | Description                                            | Arguments                                  |
| ------------ | ------------------------------------------------------ | ------------------------------------------ |
| parseQuery   | Parse a SOQL query string into a Query data structure. | soql: Query<br> config?: ParseQueryConfig  |
| isQueryValid | Returns true if the query was able to be parsed.       | soql: Query<br> config?: ParseQueryConfig  |
| composeQuery | Turn a Query object back into a SOQL statement.        | soql: Query<br> config?: SoqlComposeConfig |
| formatQuery  | Format a SOQL query string.                            | soql: Query<br> config?: FormatOptions     |

Queries may contain comments (`// single-line` and `/* multi-line */`), which are ignored during parsing. See [Comments in queries](#comments-in-queries) for details.

## Utility Functions

**General Utility**

Many of hte utility functions are provided to easily determine the shape of specific data since there are many variants. If you are using Typescript in strict mode, you can use these to narrow your types.

| Function                                | Description                                                                                                                                                                                                             | Arguments                                                                   |
| --------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------- |
| hasAlias                                | Returns `true` if the field passed in has the `alias` property.                                                                                                                                                         | input: `string \| ComposeFieldInput`                                        |
| 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`                                        |
| getFlattenedFields                      | 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. | soql: `Query \| Subquery \| FieldSubquery`<br> config?: `SoqlComposeConfig` |
| hasComments                             | Returns `true` if the query string contains at least one comment. Operates on the raw string without parsing it, so it works on any input and never throws.                                                             | soql: `string`                                                              |
| getComments                             | Returns all comments in the query string (type, text, and position), in order of appearance. Operates on the raw string without parsing it, so it works on any input and never throws.                                  | soql: `string`                                                              |
| stripComments                           | Removes comments from a query string without otherwise modifying it. Returns the original string as-is if there are no comments. See [Comments in queries](#comments-in-queries).                                       | soql: `string`                                                              |
| isSubquery                              | Returns `true` if the data passed in is a subquery.                                                                                                                                                                     | query: `Query \| Subquery`                                                  |
| isFieldSubquery                         | Returns `true` if the data passed in is a FieldSubquery.                                                                                                                                                                | value: `any`                                                                |
| isWhereClauseWithRightCondition         | Returns `true` if the value passed in is a `WhereClause` with an `operator` and `right` property                                                                                                                        | value: `WhereClause`                                                        |
| isHavingClauseWithRightCondition        | Returns `true` if the value passed in is a `HavingClause` with an `operator` and `right` property                                                                                                                       | value: `HavingClause`                                                       |
| isWhereOrHavingClauseWithRightCondition | Returns `true` if the value passed in is a `WhereClause` or `HavingClause` with an `operator` and `right` property                                                                                                      | value: `WhereClause \| HavingClause`                                        |
| isValueCondition                        | Returns `true` if the value passed in has `field`, `operator` and `value` properties                                                                                                                                    | value: `Condition`                                                          |
| isValueWithDateLiteralCondition         | Returns `true` if the value passed in has `field`, `operator` and `value` properties and has a `literalType` property that is `DATE_LITERAL` of `['DATE_LITERAL',...]`                                                  | value: `Condition`                                                          |
| isValueWithDateNLiteralCondition        | Returns `true` if the value passed in has `field`, `operator`, `value` and `dateLiteralVariable` properties                                                                                                             | value: `Condition`                                                          |
| isValueFunctionCondition                | Returns `true` if the value passed in has `fn`, `operator` and `value` properties                                                                                                                                       | value: `Condition`                                                          |
| isNegationCondition                     | Returns `true` if the value passed in has a `openParen` property and does not have `fn`, `field`, `operator`, `value`, and `closeParen` properties                                                                      | value: `Condition`                                                          |
| isValueQueryCondition                   | Returns `true` if the value passed in has `field`, `operator` and `valueQuery` properties and does not have a `value` property                                                                                          | value: `Condition \| ValueQueryCondition`                                   |
| isOrderByField                          | Returns `true` if the value passed in has `field` property                                                                                                                                                              | value: `OrderByClause`                                                      |
| isOrderByFn                             | Returns `true` if the value passed in has `fn` property                                                                                                                                                                 | value: `OrderByClause`                                                      |
| isGroupByField                          | Returns `true` if the value passed in has `field` property                                                                                                                                                              | value: `GroupByClause`                                                      |
| isGroupByFn                             | Returns `true` if the value passed in has `fn` property                                                                                                                                                                 | value: `GroupByClause`                                                      |

**ParseQueryConfig**

| Property               | Type    | Description                                                                                                                                                                                                                                   | required | default |
| ---------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ------- |
| allowApexBindVariables | boolean | Determines if apex variables are allowed in parsed query. Example: `WHERE Id IN :accountIds`. Only simple Apex is supported. Function calls are not supported. (e.x. `accountMap.keyset()` is not supported)                                  | FALSE    | FALSE   |
| allowPartialQuery      | boolean | If provided, you can provide an incomplete soql query. This is useful if you need to parse WHERE clauses, for example. Subqueries are required to be valid.                                                                                   | FALSE    | FALSE   |
| ignoreParseErrors      | boolean | If set to true, then queries with partially invalid syntax will still be parsed, but any clauses with invalid parts will be omitted. The SELECT clause and FROM clause must always be valid, but all other clauses can contain invalid parts. | FALSE    | FALSE   |
| logErrors              | boolean | If true, parsing and lexing errors will be logged to the console.                                                                                                                                                                             | FALSE    | FALSE   |

**SoqlComposeConfig**

| Property      | Type          | Description                                                                                                                                                                                                              | required | default |
| ------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------- | ------- |
| format        | boolean       | Apply formatting to the composed query. This will result in a multi-line soql statement.                                                                                                                                 | FALSE    | FALSE   |
| formatOptions | FormatOptions | Options to apply to the formatter.                                                                                                                                                                                       | FALSE    |         |
| autoCompose   | boolean       | If you need to compose just part of a query, you can create your own instance of the Compose class and set this to false, then call any methods that you need to just for what you would like to turn into a SOQL query. | FALSE    | TRUE    |
| logging       | boolean       | Print out logging statements to the console about the format operation.                                                                                                                                                  | FALSE    | FALSE   |

**FormatOptions**

| Property                         | Type    | Description                                                                                                                                                                                        | required | default |
| -------------------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ------- |
| numIndent                        | number  | The number of times `indentString` is repeated for each level of indentation.                                                                                                                      | FALSE    | 1       |
| indentString                     | string  | The string used for one unit of indentation. e.g. `{ indentString: ' ', numIndent: 2 }` indents with two spaces per level. Must be whitespace only (spaces and/or tabs); other values are ignored. | FALSE    | `\t`    |
| fieldMaxLineLength               | number  | The number of characters that the fields should take up before making a new line. Set this to 1 to have every field on its own line. Also applies to GROUP BY and ORDER BY items.                  | FALSE    | 60      |
| fieldSubqueryParensOnOwnLine     | boolean | If true, the opening and closing parentheses will be on their own line for subqueries.                                                                                                             | FALSE    | TRUE    |
| newLineAfterKeywords             | boolean | Adds a new line and indent after all keywords (such as SELECT, FROM, WHERE, ORDER BY, etc..) Setting this to true will add new lines in other places as well, such as complex WHERE clauses        | FALSE    | FALSE   |
| ~~whereClauseOperatorsIndented~~ | boolean | **Deprecated** If true, indents the where clause operators.                                                                                                                                        | FALSE    | FALSE   |
| ~~logging~~                      | boolean | **Deprecated** - this is ignored and will be removed in a future version.                                                                                                                          | FALSE    | FALSE   |

## Examples

### Parsing Queries

Parsing a SOQL query can be completed by calling `parseQuery(soqlQueryString)`. A `Query` data structure will be returned.

```typescript
import { parseQuery } from '@jetstreamapp/soql-parser-js';

const soql = `
  SELECT UserId, COUNT(Id)
  FROM LoginHistory
  WHERE LoginTime > 2010-09-20T22:16:30.000Z
  AND LoginTime < 2010-09-21T22:16:30.000Z
  GROUP BY UserId
`;

const soqlQuery = parseQuery(soql);

console.log(JSON.stringify(soqlQuery, null, 2));
```

<details>
  <summary><b>Results (click to show)</b></summary>

```json
{
  "fields": [
    {
      "type": "Field",
      "field": "UserId"
    },
    {
      "type": "FieldFunctionExpression",
      "functionName": "COUNT",
      "parameters": ["Id"],
      "isAggregateFn": true,
      "rawValue": "COUNT(Id)"
    }
  ],
  "sObject": "LoginHistory",
  "where": {
    "left": {
      "field": "LoginTime",
      "operator": ">",
      "value": "2010-09-20T22:16:30.000Z",
      "literalType": "DATETIME"
    },
    "operator": "AND",
    "right": {
      "left": {
        "field": "LoginTime",
        "operator": "<",
        "value": "2010-09-21T22:16:30.000Z",
        "literalType": "DATETIME"
      }
    }
  },
  "groupBy": {
    "field": "UserId"
  }
}
```

</details>

### Comments in queries

Queries passed to `parseQuery`, `isQueryValid`, and `formatQuery` may contain comments, which are ignored during parsing:

- Single-line comments: everything from `//` through the end of the line
- Multi-line comments: everything from `/*` through the closing `*/` (comments do not nest)

Comment markers inside string literals are treated as literal text, e.g. `WHERE Url = 'https://example.com'` works as expected. Since comments are not represented in the parsed `Query`, they are not preserved when composing a query back to a string.

> [!NOTE]
> Salesforce itself does not support comments in SOQL, so strip or compose the query before sending it to the Salesforce API.

```typescript
import { parseQuery, composeQuery } from '@jetstreamapp/soql-parser-js';

const soql = `
  SELECT Id, Name // the fields we need
  FROM Account /* the target object */
`;

composeQuery(parseQuery(soql)); // SELECT Id, Name FROM Account
```

If you want to remove comments from a query **without otherwise modifying it** (no reformatting, no whitespace or keyword normalization), use `stripComments`. It operates on the raw string without parsing it, so it works even on invalid SOQL and never throws — an unterminated `/*` is stripped through the end of the input rather than throwing like `parseQuery` does. When the query contains no comments the original string is returned unchanged, so it is safe to call on every query before sending it to the Salesforce API. A single space is inserted where removing a comment would otherwise merge adjacent tokens.

`hasComments` and `getComments` follow the same rules and can be used to detect or inspect comments without modifying the query.

```typescript
import { stripComments, hasComments, getComments } from '@jetstreamapp/soql-parser-js';

stripComments(`SELECT Id, Name // the fields we need
FROM Account`);
// 'SELECT Id, Name \nFROM Account'

stripComments('SELECT Id/* comment */FROM Account'); // 'SELECT Id FROM Account'

const soql = "SELECT Id FROM Account WHERE Url = 'https://example.com'";
stripComments(soql) === soql; // true - no comments, original string returned as-is
hasComments(soql); // false

getComments('SELECT Id /* fields */ FROM Account // trailing');
// [
//   { type: 'block', text: '/* fields */', start: 10, end: 22 },
//   { type: 'line', text: '// trailing', start: 36, end: 47 },
// ]
```

### Parsing a partial query

Added support for `allowPartialQuery` in version `4.4.0`

```typescript
import { parseQuery } from '@jetstreamapp/soql-parser-js';

const soql = `
  WHERE LoginTime > 2010-09-20T22:16:30.000Z
  AND LoginTime < 2010-09-21T22:16:30.000Z
  GROUP BY UserId
`;

const soqlQuery = parseQuery(soql, { allowPartialQuery: true });

console.log(JSON.stringify(soqlQuery, null, 2));
```

<details>
  <summary><b>Results (click to show)</b></summary>

```json
{
  "where": {
    "left": {
      "field": "LoginTime",
      "operator": ">",
      "value": "2010-09-20T22:16:30.000Z",
      "literalType": "DATETIME"
    },
    "operator": "AND",
    "right": {
      "left": {
        "field": "LoginTime",
        "operator": "<",
        "value": "2010-09-21T22:16:30.000Z",
        "literalType": "DATETIME"
      }
    }
  },
  "groupBy": {
    "field": "UserId"
  }
}
```

</details>

### Validating Queries

```typescript
import { isQueryValid } from '@jetstreamapp/soql-parser-js';

const invalidSoql = `SELECT UserId, COUNT(Id) Account`;
const validSoql = `SELECT UserId, COUNT(Id) Account`;

console.log(isQueryValid(soql));
console.log(isQueryValid(soql));
```

### Composing Queries

Build a `Query` data structure to have it converted back into a SOQL query.

Composing a query will turn a Query object back to a SOQL query string. The exact same data structure returned from `parseQuery()` can be used,
but depending on your use-case, you may need to build your own data structure to compose a query.
These examples show building your own Query object with the minimum required fields.

Some utility methods have been provided to make it easier to build the field data structures.

**Note:** Some operators may be converted to uppercase (e.x. NOT, AND)

**Note:** There are a number of fields populated on the Query object when `parseQuery()` is called that are not required to compose a query. Look at the examples below and the comments in the data model for more information.

```typescript
import { composeQuery, getField, Query } from '@jetstreamapp/soql-parser-js';

// Build a subquery
const oppLineItemsSubquery = {
  fields: [
    getField('Quantity'),
    getField('ListPrice'),
    getField({
      field: 'UnitPrice',
      relationships: ['PricebookEntry'],
    }),
    getField({
      field: 'Name',
      relationships: ['PricebookEntry'],
    }),
  ],
  relationshipName: 'OpportunityLineItems',
};

// build the main query and add the subquery as a field
const soqlQuery: Query = {
  fields: [
    getField('Id'),
    getField('Name'),
    getField({
      functionName: 'FORMAT',
      parameters: 'Amount',
      alias: 'MyFormattedAmount',
    }),
    getField({ subquery: oppLineItemsSubquery }),
  ],
  sObject: 'Opportunity',
  where: {
    left: {
      field: 'CreatedDate',
      operator: '>',
      value: 'LAST_N_YEARS:1',
    },
    operator: 'AND',
    right: {
      left: {
        field: 'StageName',
        operator: '=',
        value: 'Closed Won',
        // literalType is optional, but if set to STRING and our value is not already wrapped in "'", they will be added
        // All other literalType values are ignored when composing a query
        literalType: 'STRING',
      },
    },
  },
  limit: 150,
};

const composedQuery = composeQuery(soqlQuery, { format: true });

console.log(composedQuery);
```

**Results**

```sql
SELECT Id, Name, FORMAT(Amount) MyFormattedAmount,
  (
    SELECT Quantity, ListPrice, PricebookEntry.UnitPrice,
      PricebookEntry.Name
    FROM OpportunityLineItems
  )
FROM Opportunity
WHERE CreatedDate > LAST_N_YEARS:1
AND StageName = 'Closed Won'
LIMIT 150
```

### Composing a partial query

Starting in version `4.4`, compose will not fail if there are missing `SELECT` and `FROM` clauses in your query.

Partial compose support it supported without any additional steps.

```typescript
import { Compose, parseQuery } from '@jetstreamapp/soql-parser-js';

const soql = `WHERE Name LIKE 'A%' AND MailingCity = 'California`;
const parsedQuery = parseQuery(soql, { allowPartialQuery: true });

// Results of Parsed Query:
/**
{
  where: {
    left: { field: 'Name', operator: 'LIKE', value: "'A%'", literalType: 'STRING' },
    operator: 'AND',
    right: { left: { field: 'MailingCity', operator: '=', value: "'California'", literalType: 'STRING' } },
  },
}
*/

const composedQuery = composeQuery(soqlQuery, { format: true });

console.log(composedQuery);
```

**Results**

```sql
WHERE Name LIKE 'A%' AND MailingCity = 'California
```

<details>
  <summary><b>See the alternate way to compose partial queries by calling the Compose class directly</b></summary>

If you need to compose just a part of a query instead of the entire query, you can create an instance of the Compose class directly.

For example, if you just need the `WHERE` clause from a query as a string, you can do the following:

```typescript
import { Compose, parseQuery } from '@jetstreamapp/soql-parser-js';

const soql = `SELECT Id FROM Account WHERE Name = 'Foo'`;
const parsedQuery = parseQuery(soql);

// Results of Parsed Query:
  // const parsedQuery = {
  //   fields: [
  //     {
  //       type: 'Field',
  //       field: 'Id',
  //     },
  //   ],
  //   sObject: 'Account',
  //   where: {
  //     left: {
  //       field: 'Name',
  //       operator: '=',
  //       value: "'Foo'",
  //       literalType: 'STRING',
  //     },
  //   },
  // };

  // Create a new instance of the compose class and set the autoCompose to false to avoid composing the entire query
  const composer = new Compose(parsedQuery, { autoCompose: false });


  const whereClause = composer.parseWhereOrHavingClause(parsedQuery.where);

  console.log(whereClause);
}
```

#### Available methods on the `Compose` class

These are used internally, but are public and available for use.

```typescript
parseQuery(query: Query | Subquery): string;
parseFields(fields: FieldType[]): { text: string; typeOfClause?: string[] }[];
parseTypeOfField(typeOfField: FieldTypeOf): string[];
parseWhereOrHavingClause(whereOrHaving: WhereClause | HavingClause, indent = 0): string; // indent only applies when format is enabled
parseGroupByClause(groupBy: GroupByClause | GroupByClause[]): string;
parseOrderBy(orderBy: OrderByClause | OrderByClause[]): string;
parseWithDataCategory(withDataCategory: WithDataCategoryClause): string;
```

</details>

## Format Query

This function is provided as a convenience and just calls parse and compose.
[Check out the playground](https://soql-parser-js.getjetstream.app/playground) to see the outcome of the various format options.

```typescript
import { formatQuery } from '@jetstreamapp/soql-parser-js';

const query = `SELECT Id, Name, AccountNumber, AccountSource, AnnualRevenue, BillingAddress, BillingCity, BillingCountry, BillingGeocodeAccuracy, ShippingStreet, Sic, SicDesc, Site, SystemModstamp, TickerSymbol, Type, Website, (SELECT Id, Name, AccountId, Amount, CampaignId, CloseDate, CreatedById, Type FROM Opportunities), (SELECT Id, Name, AccountNumber, AccountSource, AnnualRevenue, BillingAddress, Website FROM ChildAccounts) FROM Account WHERE Name LIKE 'a%' OR Name LIKE 'b%' OR Name LIKE 'c%'`;

const formattedQuery1 = formatQuery(query);
const formattedQuery2 = formatQuery(query, { fieldMaxLineLength: 20, fieldSubqueryParensOnOwnLine: false });
const formattedQuery3 = formatQuery(query, { newLineAfterKeywords: true });
const formattedQuery4 = formatQuery(query, { indentString: ' ', numIndent: 2, fieldMaxLineLength: 40 });
```

```sql
-- formattedQuery1
SELECT Id, Name, AccountNumber, AccountSource, AnnualRevenue,
	BillingAddress, BillingCity, BillingCountry,
	BillingGeocodeAccuracy, ShippingStreet, Sic, SicDesc, Site,
	SystemModstamp, TickerSymbol, Type, Website,
	(
		SELECT Id, Name, AccountId, Amount, CampaignId, CloseDate,
			CreatedById, Type
		FROM Opportunities
	),
	(
		SELECT Id, Name, AccountNumber, AccountSource, AnnualRevenue,
			BillingAddress, Website
		FROM ChildAccounts
	)
FROM Account
WHERE Name LIKE 'a%'
	OR Name LIKE 'b%'
	OR Name LIKE 'c%'

-- formattedQuery2
SELECT Id, Name,
	AccountNumber,
	AccountSource,
	AnnualRevenue,
	BillingAddress,
	BillingCity,
	BillingCountry,
	BillingGeocodeAccuracy,
	ShippingStreet,
	Sic, SicDesc, Site,
	SystemModstamp,
	TickerSymbol, Type,
	Website,
	(SELECT Id, Name,
		AccountId, Amount,
		CampaignId,
		CloseDate,
		CreatedById, Type
	FROM Opportunities),
	(SELECT Id, Name,
		AccountNumber,
		AccountSource,
		AnnualRevenue,
		BillingAddress,
		Website
	FROM ChildAccounts)
FROM Account
WHERE Name LIKE 'a%'
	OR Name LIKE 'b%'
	OR Name LIKE 'c%'

-- formattedQuery3
SELECT
	Id, Name, AccountNumber, AccountSource, AnnualRevenue,
	BillingAddress, BillingCity, BillingCountry,
	BillingGeocodeAccuracy, ShippingStreet, Sic, SicDesc, Site,
	SystemModstamp, TickerSymbol, Type, Website,
	(
		SELECT
			Id, Name, AccountId, Amount, CampaignId, CloseDate,
			CreatedById, Type
		FROM
			Opportunities
	),
	(
		SELECT
			Id, Name, AccountNumber, AccountSource, AnnualRevenue,
			BillingAddress, Website
		FROM
			ChildAccounts
	)
FROM
	Account
WHERE
	Name LIKE 'a%'
	OR Name LIKE 'b%'
	OR Name LIKE 'c%'

-- formattedQuery4
SELECT Id, Name, AccountNumber, AccountSource,
  AnnualRevenue, BillingAddress,
  BillingCity, BillingCountry,
  BillingGeocodeAccuracy, ShippingStreet,
  Sic, SicDesc, Site, SystemModstamp,
  TickerSymbol, Type, Website,
  (
    SELECT Id, Name, AccountId, Amount,
      CampaignId, CloseDate, CreatedById, Type
    FROM Opportunities
  ),
  (
    SELECT Id, Name, AccountNumber, AccountSource,
      AnnualRevenue, BillingAddress, Website
    FROM ChildAccounts
  )
FROM Account
WHERE Name LIKE 'a%'
  OR Name LIKE 'b%'
  OR Name LIKE 'c%'
```

## Using in LWC

The library can be used as a Lightning Web Component in your Salesforce org. The build produces two artifacts to support different workflows:

| Artifact                                   | What it is                                                                            | Best for                                                                             |
| ------------------------------------------ | ------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------ |
| `dist/lwc/soqlParserJs.js`                 | Standalone bundled JS file (no metadata)                                              | SFDX projects — drop into your existing `lwc/soqlParserJs/` folder                   |
| `dist/lwc-packaged/` (and the release zip) | Full Salesforce metadata package: `package.xml` + `lwc/soqlParserJs/{js,js-meta.xml}` | Direct deployment to an org via `sf project deploy start` — no SFDX project required |

The Salesforce API version used for the metadata package is configured via the `salesforceApiVersion` field in [package.json](package.json) and is applied to both `package.xml` and `soqlParserJs.js-meta.xml` at build time.

### Option 1: SFDX project — drop in the standalone JS file

If you already have an SFDX project, the easiest path is to grab the standalone `soqlParserJs.js` and place it in your project's `lwc/soqlParserJs/` folder alongside a `soqlParserJs.js-meta.xml` file you provide yourself.

Sources for the standalone JS:

- **GitHub Releases** — every release attaches `soqlParserJs.js` directly to the [Releases page](https://github.com/jetstreamapp/soql-parser-js/releases).
- **npm** — `npm install @jetstreamapp/soql-parser-js`, then copy from `node_modules/@jetstreamapp/soql-parser-js/dist/lwc/soqlParserJs.js`.
- **Local build** — clone the repo, run `npm install && npm run build:lwc`, and find the file at `dist/lwc/soqlParserJs.js`.

Your SFDX folder should look like:

```
force-app/main/default/lwc/soqlParserJs/
├── soqlParserJs.js          <-- copied from this project
└── soqlParserJs.js-meta.xml <-- your own (example below)
```

A minimal `soqlParserJs.js-meta.xml`:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>66.0</apiVersion>
    <isExposed>false</isExposed>
</LightningComponentBundle>
```

### Option 2: Direct deployment — use the pre-built metadata package

If you don't have an SFDX project, download the pre-zipped metadata package, extract, and deploy:

```bash
# Download soql-parser-js-lwc-v<version>.zip from the GitHub Releases page, then:
unzip soql-parser-js-lwc-v<version>.zip -d soql-parser-js-lwc
sf project deploy start --metadata-dir soql-parser-js-lwc --target-org <your-org-alias>
```

Or, if you've built locally, the equivalent directory is `dist/lwc-packaged/`:

```bash
sf project deploy start --metadata-dir dist/lwc-packaged --target-org <your-org-alias>
```

Legacy `sfdx` CLI alternative:

```bash
sfdx force:mdapi:deploy -d <path-to-package> -w 10
```

### Using in Salesforce

The deployed component is `c/soqlParserJs`. It is intentionally a library component (`isExposed=false`), so you don't drop it on a Lightning page directly — instead, import its exports from your own LWC:

```js
import { LightningElement } from 'lwc';
import { parseQuery } from 'c/soqlParserJs';

export default class MyComponent extends LightningElement {
  parsedQuery;

  get parsedQueryString() {
    return this.parsedQuery ? JSON.stringify(this.parsedQuery, null, 2) : '';
  }

  handleClick() {
    this.parsedQuery = parseQuery("SELECT Id, Name FROM Account WHERE Industry = 'Technology'");
  }
}
```

<!-- LWC event bindings must be unquoted (onclick={handleClick}); prettier would add quotes -->
<!-- prettier-ignore -->
```html
<template>
  <button class="slds-button slds-button_neutral" onclick={handleClick}>Click Me</button>

  <p>Parsed Query: {parsedQueryString}</p>
</template>
```

All public exports of the library (`parseQuery`, `composeQuery`, `formatQuery`, `isQueryValid`, the various utility functions, etc.) are available as named imports from `c/soqlParserJs`.

## CLI

Install globally or use `npx` to interact with the cli.

### Available Commands

- `soql-parser-js --help` (or using `npx`: `npx soql-parser-js --help`)
- `soql-parser-js parse --help`
- `soql-parser-js compose --help`
- `soql-parser-js format --help`

### Examples

#### Parse

`npx soql-parser-js parse "SELECT Id FROM Account"`

```bash
{"fields":[{"type":"Field","field":"Id"}],"sObject":"Account"}
```

#### Compose

`npx soql-parser-js compose "{\"fields\":[{\"type\":\"Field\",\"field\":\"Id\"}],\"sObject\":\"Account\"}"`

```bash
SELECT Id FROM Account
```

`npx soql-parser-js compose "{\"fields\":[{\"type\":\"Field\",\"field\":\"Id\"}],\"sObject\":\"Account\"}" --json` or -j

```json
{ "query": "SELECT Id FROM Account" }
```

#### Format

`npx soql-parser-js format "SELECT Name, COUNT(Id) FROM Account GROUP BY Name HAVING COUNT(Id) > 1"`

```bash
SELECT Name, COUNT(Id)
FROM Account
GROUP BY Name
HAVING COUNT(Id) > 1
```

`npx soql-parser-js format "SELECT Name, COUNT(Id) FROM Account GROUP BY Name HAVING COUNT(Id) > 1 -j`

```json
{ "query": "SELECT Name, COUNT(Id)\nFROM Account\nGROUP BY Name\nHAVING COUNT(Id) > 1" }
```

#### Is Valid

`npx soql-parser-js valid "SELECT Id FROM Account"`

```bash
true
```

`npx soql-parser-js valid "SELECT Id invalid FROM Account"`

ℹ️ this returns an exit code of 1

```bash
false
```

`npx soql-parser-js valid "SELECT Id FROM Account" -j`

```json
{ "isValid": true }
```

`npx soql-parser-js valid "SELECT Id invalid invalid FROM Account" -j`

ℹ️ this returns an exit code of 0

```json
{ "isValid": false }
```

### List of options

`soql-parser-js --help`

```bash
Usage: soql-parser-js [options] [command]

Options:
  -h, --help                 output usage information

Commands:
  parse [options] <sql>
  compose [options] <query>
  format [options] <sql>
  valid <sql>
```

`soql-parser-js parse --help`

```bash
Usage: parse [options] <sql>

Options:
  -a, --allow-apex     allow apex bind variables
  -p, --allow-partial  allow partial queries
  -i, --ignore-errors  ignore parse errors, return as much of query as possible
  -h, --help           output usage information
```

`soql-parser-js compose --help`

```bash
Usage: compose [options] <query>

Options:
  -f, --format                   format output
  -i --indent <count>            number of times the indent string is repeated per level (default: 1)
  -t --indent-string <string>    string used for one unit of indentation (default: tab), e.g. --indent-string "  "
  -m --line-length <chars>       max number of characters per line (default: 60)
  -s --subquery-parens-new-line  subquery parens on own line
  -k --keywords-new-line         new line after keywords
  -j, --json                     output as JSON
  -h, --help                     output usage information
```

`soql-parser-js format --help`

```bash
Usage: format [options] <sql>

Options:
  -a, --allow-apex     allow apex bind variables
  -p, --allow-partial  allow partial queries
  -i --indent <count>            number of times the indent string is repeated per level (default: 1)
  -t --indent-string <string>    string used for one unit of indentation (default: tab), e.g. --indent-string "  "
  -m --line-length <chars>       max number of characters per line (default: 60)
  -s --subquery-parens-new-line  subquery parens on own line
  -k --keywords-new-line         new line after keywords
  -j, --json                     output as JSON
  -h, --help                     output usage information
```

`soql-parser-js valid --help`

```bash
Usage: valid [options] <sql>

Options:
  -a, --allow-apex     allow apex bind variables
  -p, --allow-partial  allow partial queries
  -j, --json  output as JSON
  -h, --help  output usage information
```

## Data Models

### Query

```typescript
export type LogicalOperator = 'AND' | 'OR' | 'NOT';
export type Operator = '=' | '!=' | '<=' | '>=' | '>' | '<' | 'LIKE' | 'IN' | 'NOT IN' | 'INCLUDES' | 'EXCLUDES';
export type FieldTypeOfConditionType = 'WHEN' | 'ELSE';
export type GroupSelector = 'ABOVE' | 'AT' | 'BELOW' | 'ABOVE_OR_BELOW';
export type ForClause = 'VIEW' | 'UPDATE' | 'REFERENCE';
export type UpdateClause = 'TRACKING' | 'VIEWSTAT';
export type LiteralType =
  | 'STRING'
  | 'INTEGER'
  | 'DECIMAL'
  | 'INTEGER_WITH_CURRENCY_PREFIX'
  | 'DECIMAL_WITH_CURRENCY_PREFIX'
  | 'BOOLEAN'
  | 'NULL'
  | 'DATETIME'
  | 'DATE'
  | 'DATE_LITERAL'
  | 'DATE_N_LITERAL'
  | 'APEX_BIND_VARIABLE';
export type FieldType =
  | Field
  | FieldWithAlias
  | FieldFunctionExpression
  | FieldRelationship
  | FieldRelationshipWithAlias
  | FieldSubquery
  | FieldTypeOf;
export type OrderByCriterion = 'ASC' | 'DESC';
export type NullsOrder = 'FIRST' | 'LAST';
export type GroupByType = 'CUBE' | 'ROLLUP';
export type DateLiteral =
  | 'YESTERDAY'
  | 'TODAY'
  | 'TOMORROW'
  | 'LAST_WEEK'
  | 'THIS_WEEK'
  | 'NEXT_WEEK'
  | 'LAST_MONTH'
  | 'THIS_MONTH'
  | 'NEXT_MONTH'
  | 'LAST_90_DAYS'
  | 'NEXT_90_DAYS'
  | 'THIS_QUARTER'
  | 'LAST_QUARTER'
  | 'NEXT_QUARTER'
  | 'THIS_YEAR'
  | 'LAST_YEAR'
  | 'NEXT_YEAR'
  | 'THIS_FISCAL_QUARTER'
  | 'LAST_FISCAL_QUARTER'
  | 'NEXT_FISCAL_QUARTER'
  | 'THIS_FISCAL_YEAR'
  | 'LAST_FISCAL_YEAR'
  | 'NEXT_FISCAL_YEAR';

export type DateNLiteral =
  | 'YESTERDAY'
  | 'NEXT_N_DAYS'
  | 'LAST_N_DAYS'
  | 'N_DAYS_AGO'
  | 'NEXT_N_WEEKS'
  | 'LAST_N_WEEKS'
  | 'N_WEEKS_AGO'
  | 'NEXT_N_MONTHS'
  | 'LAST_N_MONTHS'
  | 'N_MONTHS_AGO'
  | 'NEXT_N_QUARTERS'
  | 'LAST_N_QUARTERS'
  | 'N_QUARTERS_AGO'
  | 'NEXT_N_YEARS'
  | 'LAST_N_YEARS'
  | 'N_YEARS_AGO'
  | 'NEXT_N_FISCAL_QUARTERS'
  | 'LAST_N_FISCAL_QUARTERS'
  | 'N_FISCAL_QUARTERS_AGO'
  | 'NEXT_N_FISCAL_YEARS'
  | 'LAST_N_FISCAL_YEARS'
  | 'N_FISCAL_YEARS_AGO';

export interface Field {
  type: 'Field';
  field: string;
  alias?: string;
}

export interface FieldWithAlias extends Field {
  objectPrefix: string;
  rawValue: string;
}

export interface FieldFunctionExpression {
  type: 'FieldFunctionExpression';
  functionName: string;
  parameters: (string | FieldFunctionExpression)[];
  alias?: string;
  isAggregateFn?: boolean; // not required for compose, will be populated if SOQL is parsed
  rawValue?: string; // not required for compose, will be populated if SOQL is parsed
}

export interface FieldRelationship {
  type: 'FieldRelationship';
  field: string;
  relationships: string[];
  rawValue?: string; // not required for compose, will be populated if SOQL is parsed with the raw value of the entire field
}

export interface FieldRelationshipWithAlias extends FieldRelationship {
  objectPrefix: string;
  alias: string;
}

export interface FieldSubquery {
  type: 'FieldSubquery';
  subquery: Subquery;
}

export interface FieldTypeOf {
  type: 'FieldTypeof';
  field: string;
  conditions: FieldTypeOfCondition[];
}

export interface FieldTypeOfCondition {
  type: FieldTypeOfConditionType;
  objectType?: string; // not present when ELSE
  fieldList: string[];
}

export interface QueryBase {
  fields?: FieldType[];
  sObjectAlias?: string;
  usingScope?: string;
  where?: WhereClause;
  limit?: number;
  offset?: number;
  groupBy?: GroupByClause;
  orderBy?: OrderByClause | OrderByClause[];
  withDataCategory?: WithDataCategoryClause;
  withSecurityEnforced?: boolean;
  withAccessLevel?: boolean;
  for?: ForClause;
  update?: UpdateClause;
}

export interface Query extends QueryBase {
  sObject?: string;
}

export interface Subquery extends QueryBase {
  relationshipName: string;
  sObjectPrefix?: string[];
}

export type WhereClause = WhereClauseWithoutOperator | WhereClauseWithRightCondition;

export interface WhereClauseWithoutOperator {
  left: ConditionWithValueQuery;
}

export interface WhereClauseWithRightCondition extends WhereClauseWithoutOperator {
  operator: LogicalOperator;
  right: WhereClause;
}

export type Condition =
  | ValueCondition
  | ValueWithDateLiteralCondition
  | ValueWithDateNLiteralCondition
  | ValueFunctionCondition
  | NegationCondition;

export type ConditionWithValueQuery = Condition | ValueQueryCondition;

export interface OptionalParentheses {
  openParen?: number;
  closeParen?: number;
}

export interface ValueCondition extends OptionalParentheses {
  field: string;
  operator: Operator;
  value: string | string[];
  literalType?: LiteralType | LiteralType[];
}

export interface ValueWithDateLiteralCondition extends OptionalParentheses {
  field: string;
  operator: Operator;
  value: DateLiteral | DateLiteral[];
  literalType?: 'DATE_LITERAL' | 'DATE_LITERAL'[];
}

export interface ValueWithDateNLiteralCondition extends OptionalParentheses {
  field: string;
  operator: Operator;
  value: string | string[];
  literalType?: 'DATE_N_LITERAL' | 'DATE_N_LITERAL'[];
  dateLiteralVariable: number | number[];
}

export interface ValueQueryCondition extends OptionalParentheses {
  field: string;
  operator: Operator;
  valueQuery: Query;
}

export interface ValueFunctionCondition extends OptionalParentheses {
  fn: FunctionExp;
  operator: Operator;
  value: string | string[];
  literalType?: LiteralType | LiteralType[];
}

export interface NegationCondition {
  openParen: number;
}

export type OrderByClause = OrderByFieldClause | OrderByFnClause;

export interface OrderByOptionalFieldsClause {
  order?: OrderByCriterion;
  nulls?: NullsOrder;
}

export interface OrderByFieldClause extends OrderByOptionalFieldsClause {
  field: string;
}

export interface OrderByFnClause extends OrderByOptionalFieldsClause {
  fn: FunctionExp;
}

export type GroupByClause = GroupByFieldClause | GroupByFnClause;

export interface GroupByOptionalFieldsClause {
  having?: HavingClause;
}

export interface GroupByFieldClause extends GroupByOptionalFieldsClause {
  field: string | string[];
}

export interface GroupByFnClause extends GroupByOptionalFieldsClause {
  fn: FunctionExp;
}

export type HavingClause = HavingClauseWithoutOperator | HavingClauseWithRightCondition;

export interface HavingClauseWithoutOperator {
  left: Condition;
}

export interface HavingClauseWithRightCondition extends HavingClauseWithoutOperator {
  operator: LogicalOperator;
  right: HavingClause;
}

export interface FunctionExp {
  rawValue?: string; // only used for compose fields if useRawValueForFn=true. Should be formatted like this: Count(Id)
  functionName?: string; // only used for compose fields if useRawValueForFn=false, will be populated if SOQL is parsed
  alias?: string;
  parameters?: (string | FunctionExp)[]; // only used for compose fields if useRawValueForFn=false, will be populated if SOQL is parsed
  isAggregateFn?: boolean; // not used for compose, will be populated if SOQL is parsed
}

export interface WithDataCategoryClause {
  conditions: WithDataCategoryCondition[];
}

export interface WithDataCategoryCondition {
  groupName: string;
  selector: GroupSelector;
  parameters: string[];
}
```

## Contributing

All contributions are welcome on the project. Please read the [contribution guidelines](https://github.com/jetstreamapp/soql-parser-js/blob/master/CONTRIBUTING.md).
