Elixir-native SQL query code generation for Mix projects.
Squirrelix discovers .sql files under conventional Elixir source roots
(lib/, test/, dev/), then generates sibling sql.ex modules with
@spec-annotated functions that execute through Postgrex.
Guides
- Getting Started — installation, layout, first query
- Writing Queries — one query per file, comments, naming
- Types — Postgres to Elixir type mapping
- Configuration — inference, metadata, connection, CI
See also README.md for a full overview.
Query sources
Generation and checking accept a query source in one of two forms:
- A metadata map — keys are query file paths, values are keyword lists with
:paramsand:returns. Load fromsquirr_elix.exsor pass a map togenerate/3andcheck/3. An inferrer — a
(query -> {:ok, keyword()} | {:error, struct()})function or a module implementingSquirrelix.Inference.Inferrer. The Mix task uses this mode with--inferto ask Postgres for column and parameter types.
Generated code uses stdlib types in @spec output: String.t() for Postgres
enums, term() for JSON/JSONB columns, map() with required/1 for row
shapes, and plain maps at runtime rather than Gleam records or custom enum ADTs.
Summary
Functions
Checks that generated query modules are current without writing files.
Classifies file content to determine whether it was likely generated by Squirrelix.
Compares two code snippets ignoring comments and whitespace.
Generates query modules for SQL files discovered under a Mix project root.
Types
@type query_source() :: metadata() | Squirrelix.Inference.inferrer()
Functions
@spec check(Path.t(), query_source(), keyword()) :: Squirrelix.CodegenCheckSummary.t()
Checks that generated query modules are current without writing files.
Accepts the same query_source and options as generate/3.
Fails globally when any directory has query errors or drift — the summary
status is :error and every failing directory is included in :errors.
Returns a Squirrelix.CodegenCheckSummary struct.
@spec classify_file_content(String.t()) :: :likely_generated | :empty | :not_generated
Classifies file content to determine whether it was likely generated by Squirrelix.
Looks for the generation marker in the file header (first ~2KB), typically inside
@moduledoc. A marker buried later in a hand-written file is ignored.
Compares two code snippets ignoring comments and whitespace.
@spec generate(Path.t(), query_source(), keyword()) :: Squirrelix.CodegenSummary.t()
Generates query modules for SQL files discovered under a Mix project root.
Generation is project-wide atomic: if any sql/ directory has query errors
(invalid names, missing metadata, inference failures, …), nothing is written —
including directories that would otherwise succeed. Fix every error, then re-run.
Options
:version(required) — generator version string written into file headers:postgrex— module passed to generated code (defaults toPostgrex)
Returns a Squirrelix.CodegenSummary struct summarizing writes and errors.