Squirrelix (Squirrelix v0.5.1)

Copy Markdown View Source

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.

Supported public API

Pre-1.0, the supported programmatic surface is intentionally small:

Other modules under Squirrelix.* are internal (@moduledoc false) and may change without notice. See Configuration for the full inventory.

Guides

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 :params and :returns. Load from squirr_elix.exs or pass a map to generate/3 and check/3.
  • An inferrer — a (query -> {:ok, keyword()} | {:error, struct()}) function or a module implementing Squirrelix.Inference.Inferrer. The Mix task uses this mode with --infer to 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.

Generates query modules for SQL files discovered under a Mix project root.

Types

metadata()

@type metadata() :: %{required(Path.t()) => keyword()}

query_source()

@type query_source() :: metadata() | Squirrelix.Inference.inferrer()

Functions

check(root, query_source, opts \\ [])

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.

generate(root, query_source, opts \\ [])

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 to Postgrex)

Returns a Squirrelix.CodegenSummary struct summarizing writes and errors.