Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

soql-parser-js

Package Overview
Dependencies
Maintainers
1
Versions
90
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

soql-parser-js

Salesforce.com SOQL parser and composer

  • 4.6.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6.3K
decreased by-54.98%
Maintainers
1
Weekly downloads
 
Created
Source

build npm version dependencies

Description

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

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 for a full list of changes.

Migrating from version 2 to version 3? Check out the changelog for a full list of changes.

Documentation

Read the documentation on our docs site.

Compatibility

Node: version 11 or higher, or a polyfill for 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

import { parseQuery, composeQuery, isQueryValid } from '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

FunctionDescriptionArguments
parseQueryParse a SOQL query string into a Query data structure.soql: Query
config?: ParseQueryConfig
isQueryValidReturns true if the query was able to be parsed.soql: Query
config?: ParseQueryConfig
composeQueryTurn a Query object back into a SOQL statement.soql: Query
config?: SoqlComposeConfig
formatQueryFormat a SOQL query string.soql: Query
config?: FormatOptions

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.

FunctionDescriptionArguments
hasAliasReturns true if the field passed in has the alias property.input: string | ComposeFieldInput
getFieldConvenience 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
getFlattenedFieldsFlatten 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
config?: SoqlComposeConfig
isSubqueryReturns true if the data passed in is a subquery.query: Query | Subquery
isFieldSubqueryReturns true if the data passed in is a FieldSubquery.value: any
isWhereClauseWithRightConditionReturns true if the value passed in is a WhereClause with an operator and right propertyvalue: WhereClause
isHavingClauseWithRightConditionReturns true if the value passed in is a HavingClause with an operator and right propertyvalue: HavingClause
isWhereOrHavingClauseWithRightConditionReturns true if the value passed in is a WhereClause or HavingClause with an operator and right propertyvalue: WhereClause | HavingClause
isValueConditionReturns true if the value passed in has field, operator and value propertiesvalue: Condition
isValueWithDateLiteralConditionReturns 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
isValueWithDateNLiteralConditionReturns true if the value passed in has field, operator, value and dateLiteralVariable propertiesvalue: Condition
isValueFunctionConditionReturns true if the value passed in has fn, operator and value propertiesvalue: Condition
isNegationConditionReturns true if the value passed in has a openParen property and does not have fn, field, operator, value, and closeParen propertiesvalue: Condition
isValueQueryConditionReturns true if the value passed in has field, operator and valueQuery properties and does not have a value propertyvalue: Condition | ValueQueryCondition
isOrderByFieldReturns true if the value passed in has field propertyvalue: OrderByClause
isOrderByFnReturns true if the value passed in has fn propertyvalue: OrderByClause
isGroupByFieldReturns true if the value passed in has field propertyvalue: GroupByClause
isGroupByFnReturns true if the value passed in has fn propertyvalue: GroupByClause

ParseQueryConfig

PropertyTypeDescriptionrequireddefault
allowApexBindVariablesbooleanDetermines 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)FALSEFALSE
allowPartialQuerybooleanIf 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.FALSEFALSE
ignoreParseErrorsbooleanIf 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.FALSEFALSE
logErrorsbooleanIf true, parsing and lexing errors will be logged to the console.FALSEFALSE

SoqlComposeConfig

PropertyTypeDescriptionrequireddefault
formatbooleanApply formatting the the composed query. This will result in a multi-line soql statement.FALSETRUE
formatOptionsFormatOptionsOptions to apply to the formatter.FALSE
autoComposebooleanIf 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.FALSETRUE
loggingbooleanPrint out logging statements to the console about the format operation.FALSEFALSE

FormatOptions

PropertyTypeDescriptionrequireddefault
numIndentnumberThe number of tab characters to indent.FALSE1
fieldMaxLineLengthnumberThe 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.FALSE60
fieldSubqueryParensOnOwnLinebooleanIf true, the opening and closing parentheses will be on their own line for subqueries.FALSETRUE
newLineAfterKeywordsbooleanAdds 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.FALSEFALSE
whereClauseOperatorsIndentedbooleanDeprecated If true, indents the where clause operators.FALSEFALSE
loggingbooleanPrint out logging statements to the console about the format operation.FALSEFALSE

Contributing

All contributions are welcome on the project. Please read the contribution guidelines.

Keywords

FAQs

Package last updated on 18 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc