Query Conversion

formatQuery

formatQuery signature
export const formatQuery = (
value: DenormalizedQuery,
format: QueryFormat,
options?:
| IFormatSqlOptions
| IFormatMongoOptions
| IFormatAqlOptions
| IFormatJsonataOptions
| IFormatJsonLogicOptions
| IFormatCelOptions
| IFormatDjangoOptions
| IFormatDynamoOptions
| IFormatElasticsearchOptions
| IFormatSpelOptions
| IFormatPrismaOptions
| IFormatODataOptions
| IFormatRsqlOptions
) => string;
Supported formats
export type QueryFormat =
| 'SQL'
| 'Mongo'
| 'AQL'
| 'JSONata'
| 'JsonLogic'
| 'CEL'
| 'Elasticsearch'
| 'SpEL'
| 'Prisma'
| 'OData'
| 'RSQL'
| 'Dynamo'
| 'Django';
Options types
export interface IFormatQueryBaseOptions {
rootlessCombinator?: 'AND' | 'OR';
modifierlessGroupCombinator?: 'AND' | 'OR';
fields?: IBuilderFieldProps[];
}
export interface IFormatSqlOptions extends IFormatQueryBaseOptions {
wrapWhereClause?: boolean;
}
export interface IFormatAqlOptions extends IFormatQueryBaseOptions {
wrapFilterClause?: boolean;
variableName?: string;
}
export interface IFormatElasticsearchOptions extends IFormatQueryBaseOptions {
wrapQueryClause?: boolean;
}
export interface IFormatPrismaOptions extends IFormatQueryBaseOptions {
wrapWhereClause?: boolean;
}
export interface IFormatODataOptions extends IFormatQueryBaseOptions {
wrapFilterClause?: boolean;
}

Parameters

  • value The denormalized query tree to serialize.
  • format The target format. This determines both the formatter used and the shape of accepted options.
  • options Optional formatter configuration.

Shared options

  • fields Supplies field metadata to the formatter. This is often required when output depends on field type, option labels, or value semantics.
  • rootlessCombinator Chooses how root-level items are combined when the query does not have a single explicit root group. This matters most when singleRootGroup is disabled.
  • modifierlessGroupCombinator Chooses how children of modifierless groups are combined when a group has structure but no embedded AND/OR modifier.

Behavior notes

  • fields Treat this as recommended for anything beyond trivial string-only formatting, especially for typed values and list-backed fields.
  • rootlessCombinator If your tree has multiple top-level nodes, this decides whether the exported expression joins them with AND or OR.
  • modifierlessGroupCombinator Use this when your UI allows groups without modifiers and your target syntax still needs a combinator in serialized output.

Format-specific options

  • wrapWhereClause Supported by SQL and Prisma format options. Prefixes the output with WHERE .
  • wrapFilterClause Supported by AQL and OData format options. Prefixes the output with FILTER or equivalent filter clause semantics.
  • variableName Supported by AQL format options. Sets the document variable prefix used in generated expressions.
  • wrapQueryClause Supported by Elasticsearch format options. Wraps the generated expression in the outer query clause shape.

Field comparisons

Formatting a field-reference rule
const sql = formatQuery(data, 'SQL', {
fields,
wrapWhereClause: true,
});
// WHERE ORDER_TOTAL >= ORDER_APPROVAL_LIMIT
  • Native support SQL, Mongo, AQL, JSONata, JsonLogic, CEL, SpEL, Prisma, OData, Dynamo, and Django serialize field comparisons when the operator has a direct right-hand-side field form.
  • fields option Treat field metadata as strongly recommended here because formatter behavior depends on field semantics and operator compatibility.
  • Explicit rejection Elasticsearch and RSQL intentionally throw for valueSource: 'field' rules instead of inventing backend-only syntax.
See also
For a live sandbox that exercises these formats, use Parsing and Formatting.

Related recipes

© Vojtěch Václav Porteš 2026 - All library contents are available under the MIT license.
Loading privacy preferences...