galchemy

Package Version Hex Docs

galchemy is a PostgreSQL-first SQL toolkit for Gleam:

The design goal is explicitness:

Installation

gleam add galchemy

Quick Start

import galchemy
import galchemy/ast/query
import galchemy/dsl/expr
import galchemy/dsl/predicate
import galchemy/dsl/select
import galchemy/dsl/table
import gleam/io
import gleam/string

pub fn main() -> Nil {
  let users = table.as_(table.table("users"), "u")
  let id = table.int(users, "id")
  let name = table.text(users, "name")
  let active = table.bool(users, "active")

  let q =
    select.select([
      expr.item(expr.col(id)),
      expr.item(expr.col(name)),
    ])
    |> select.from(users)
    |> select.where_(predicate.eq(expr.col(active), expr.bool(True)))
    |> query.Select

  io.println(string.inspect(galchemy.compile(q)))
}

Core Workflow

  1. Define table/column references with galchemy/dsl/table.
  2. Build query AST values with galchemy/dsl/*.
  3. Compile with galchemy.compile (or compile_with).
  4. Optionally execute through galchemy/sql/postgres.

Example Catalog

All runnable examples live in src/galchemy/examples/*.
Root examples/* contains matching entry-point wrappers.

ScenarioModule
Simple select + decodergalchemy/examples/simple_select
CRUD buildersgalchemy/examples/crud
Joinsgalchemy/examples/join_example
RETURNINGgalchemy/examples/returning_example
Advanced SQL (CTE, derived source/join, subquery, window, UNION ALL)galchemy/examples/advanced_select
Schema diff + migration plan + code generationgalchemy/examples/schema_tooling
PostgreSQL schema introspectiongalchemy/examples/schema_introspection
Eager/lazy loading plannersgalchemy/examples/loading
Entity + unit of work + runtime sessiongalchemy/examples/orm
Declarative models + model-first query + result mappinggalchemy/examples/orm_declarative

Run them with:

gleam run -m galchemy/examples/simple_select
gleam run -m galchemy/examples/crud
gleam run -m galchemy/examples/join_example
gleam run -m galchemy/examples/returning_example
gleam run -m galchemy/examples/advanced_select
gleam run -m galchemy/examples/schema_tooling
gleam run -m galchemy/examples/schema_introspection
gleam run -m galchemy/examples/loading
gleam run -m galchemy/examples/orm
gleam run -m galchemy/examples/orm_declarative

SQL DSL Highlights

CRUD

Use dedicated modules:

Each builder is immutable and returns a new query value.

Predicates and Expressions

galchemy/dsl/predicate supports:

galchemy/dsl/expr supports:

Advanced Select Features

Supported select composition:

See galchemy/examples/advanced_select for a combined real-world example.

PostgreSQL Execution

galchemy/sql/postgres bridges compiled queries to pog.

import galchemy/ast/query
import galchemy/sql/postgres
import gleam/dynamic/decode
import pog

pub fn run(connection: pog.Connection, q: query.Query) {
  let decoder = decode.at([0], decode.int)
  postgres.execute_with(q, decoder, connection)
}

Schema Tooling

galchemy can help with the full schema pipeline:

  1. introspect PostgreSQL to SchemaSnapshot;
  2. infer relations;
  3. diff snapshots;
  4. compile DDL operations;
  5. build and apply migration plans;
  6. generate Gleam table modules from snapshots.

Main modules:

See:

ORM and Session Building Blocks

galchemy ORM is explicit and composable. It provides primitives, not hidden magic:

Recommended starting path:

  1. galchemy/examples/orm_declarative
  2. galchemy/examples/orm
  3. galchemy/examples/loading

Public API Map

Current Scope

Intentionally in scope:

Intentionally out of scope:

Development

gleam check
gleam test
Search Document