/soql-parse

Parse SOQL string to abstract syntax tree expression in JavaScript

Primary LanguageHTML

soql-parse

Parse SOQL query string to abstract syntax tree in JavaScript.

Build Status

Online Demo

https://stomita.github.io/soql-parse/

Install

npm install soql-parse

Usage

import { parse } from 'soql-parse';

const soql = `
Select Id, Name, toLabel(Type) from Account
WHERE Name like 'A%' and Type IN ('Partner', 'Customer')
ORDER by CreatedDate DESC
LIMIT 10
`;

const parsed = parse(soql);
console.log(parsed);

Result:

{
    "type": "Query",
    "fields": [
        {
            "type": "FieldReference",
            "path": [
                "Id"
            ]
        },
        {
            "type": "FieldReference",
            "path": [
                "Name"
            ]
        },
        {
            "type": "FunctionCall",
            "name": "toLabel",
            "arguments": [
                {
                    "type": "FieldReference",
                    "path": [
                        "Type"
                    ]
                }
            ]
        }
    ],
    "object": {
        "type": "ObjectReference",
        "name": "Account"
    },
    "condition": {
        "type": "LogicalCondition",
        "operator": "AND",
        "left": {
            "type": "ComparisonCondition",
            "field": {
                "type": "FieldReference",
                "path": [
                    "Name"
                ]
            },
            "operator": "LIKE",
            "value": {
                "type": "string",
                "value": "A%"
            }
        },
        "right": {
            "type": "ComparisonCondition",
            "field": {
                "type": "FieldReference",
                "path": [
                    "Type"
                ]
            },
            "operator": "IN",
            "value": {
                "type": "list",
                "values": [
                    {
                        "type": "string",
                        "value": "Partner"
                    },
                    {
                        "type": "string",
                        "value": "Customer"
                    }
                ]
            }
        }
    },
    "sort": [
        {
            "field": {
                "type": "FieldReference",
                "path": [
                    "CreatedDate"
                ]
            },
            "direction": "DESC"
        }
    ],
    "limit": {
        "type": "number",
        "value": 10
    }
}