Skip to content

Expressions

DescriptionThe full Expression API: core methods, operator helpers, AST node types, searching, walking, and transforming.

Every parsed SQL element is an Expression. The Guide introduced .find(), .transform(), and .sql(). This page is the complete API reference.

Core methods

These are available on every Expression instance.

Method Returns Description
.sql(options?)stringGenerate SQL. Pass { dialect, pretty } for dialect-specific output
.find(Type)Expression or undefinedFirst descendant of the given type
.findAll(Type)Iterable<Expression>All descendants of the given type
.walk()Iterable<Expression>All nodes, depth-first
.transform(fn)ExpressionReplace nodes by returning a new node from fn
.copy()ExpressionDeep copy the entire subtree

Operator methods

These return new AST nodes. They are useful in builder chains or when constructing conditions programmatically.

Method Returns Description
.as(alias)AliasExprAlias the expression
.eq(other)EqExprEquality comparison
.and(other)AndExprLogical AND
.or(other)OrExprLogical OR

Expression types

Each SQL concept has its own Expression subclass. Import them from the main package.

typescript
import { SelectExpr, ColumnExpr, TableExpr, FuncExpr } from "sqlingo";
Class Represents
SelectExprA SELECT statement
ColumnExprA column reference
TableExprA table reference
WhereExprA WHERE clause
FuncExprA function call
LiteralExprA literal value (number, string)
AliasExprAn aliased expression (x AS y)
JoinExprA JOIN clause

Example: extracting metadata

Use .findAll() to pull specific node types out of a parsed query.

typescript
import { parseOne, ColumnExpr } from "sqlingo";

const ast = parseOne("SELECT a, b FROM t WHERE x > 1");

for (const col of ast.findAll(ColumnExpr)) {
  console.log(col.name);
}
// "a", "b", "x"

Example: dialect-specific output

Pass a dialect to .sql() to get output that matches a specific database.

typescript
import { Postgres } from "sqlingo/postgres";

ast.sql({ dialect: Postgres });