Skip to content

Compiler & Dialects

The compiler turns a QueryPlan into { sql, params, columnTypes }. A Dialect decides the SQL dialect and parameter style.

import { compilePlan } from "@mountsqli/compiler";
const { sql, params } = compilePlan(plan, "postgres");
// sql: SELECT * FROM "users" WHERE "age" > $1
// params: [18]
Dialect Placeholder
sqlite ?
mysql ?
postgres $1, $2, …

compilePlan never string-concatenates values. Every value becomes a bound parameter. This is a structural guarantee:

db.from(users).where("email", "=", userInput);
// → WHERE "email" = ? (userInput is a param, never inlined)

Even raw-SQL hatches (selectExpr/whereExpr) reject ;, --, and /* at build time.

  • optimize(plan) — rewrites the plan for better SQL.
  • suggestIndexes(plan) — proposes indexes (powers mountsqli analyze).

Switching databases changes the dialect, not your code. The same plan runs on SQLite, Postgres, or MySQL.

  • Trust the compiler for parameterization — don’t inline values.
  • Use compilePlan in tests to assert SQL shape per dialect.
  • Manually building SQL strings instead of relying on compilePlan.
  • Forgetting the dialect arg (defaults to sqlite).