sqlode

CI Gitleaks Trivy OSV-Scanner tested with atago measured with himorime Hex Hex Docs Hex Downloads GitHub Downloads (all assets, all releases) License OpenSSF Scorecard

sqlode reads SQL schema and query files and generates typed Gleam code: write SQL, run the generator, call the generated functions. The workflow follows sqlc, and sqlode reads an existing sqlc.yaml, but its macros use the sqlode.* prefix.

It supports PostgreSQL (pog), MySQL 8.0 (shork) and SQLite (sqlight).

Quickstart

gleam new myapp
cd myapp
gleam add sqlode sqlight
gleam run -m sqlode -- init --engine=sqlite --runtime=native

init writes db/schema.sql, db/query.sql and this sqlode.yaml:

version: "2"
sql:
  - schema: "db/schema.sql"
    queries: "db/query.sql"
    engine: "sqlite"
    gen:
      gleam:
        out: "src/db"
        runtime: "native"

Put your schema and queries in the two SQL files:

-- db/schema.sql
CREATE TABLE authors (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL,
  bio TEXT
);

-- db/query.sql
-- name: GetAuthor :one
SELECT id, name, bio FROM authors WHERE id = ?;

-- name: ListAuthors :many
SELECT id, name FROM authors ORDER BY name;

-- name: CreateAuthor :exec
INSERT INTO authors (name, bio) VALUES (sqlode.arg(name), sqlode.narg(bio));
$ gleam run -m sqlode -- generate
Loading config from: sqlode.yaml

Successfully generated 4 files
  Generated: src/db/params.gleam
  Generated: src/db/queries.gleam
  Generated: src/db/models.gleam
  Generated: src/db/sqlight_adapter.gleam
  Formatted generated code

The adapter binds parameters, runs the query and decodes the rows:

import db/params
import db/sqlight_adapter
import gleam/io
import gleam/option.{Some}
import sqlight

pub fn main() {
  let assert Ok(db) = sqlight.open(":memory:")
  let assert Ok(_) =
    sqlight.exec(
      "CREATE TABLE authors (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, bio TEXT);",
      db,
    )

  let assert Ok(_) =
    sqlight_adapter.create_author(
      db,
      params.CreateAuthorParams(name: "Alice", bio: Some("Poet")),
    )
  let assert Ok(Some(author)) =
    sqlight_adapter.get_author(db, params.GetAuthorParams(id: 1))
  io.println(author.name)
  // -> Alice
}

With runtime: "raw" (the default) sqlode skips the adapter: queries.prepare_get_author(id: 1) returns the SQL and the parameter values for any driver, and queries.get_author_decoder() decodes a row. examples/sqlite-basic is a runnable project.

Writing queries

Parameter and column types come from the schema. Where sqlode cannot infer a parameter type, add a cast such as $1::int. The capabilities page lists every engine, annotation and macro, and configuration covers type overrides, the type mapping and the YAML keys.

Command line

sqlode init     [--output=sqlode.yaml] [--engine=postgresql|sqlite|mysql] [--runtime=raw|native]
sqlode generate [--config=sqlode.yaml]
sqlode verify   [--config=sqlode.yaml]
sqlode version

verify checks the project like generate without writing files and reports every problem. Errors start with a stable code such as SQD4003, explained on the errors page.

The command runs as gleam run -m sqlode --, as a standalone escript from GitHub Releases (needs Erlang/OTP), or with Docker:

curl -fsSL https://raw.githubusercontent.com/nao1215/sqlode/main/scripts/install.sh | sh
docker run --rm -v "$PWD:/work" ghcr.io/nao1215/sqlode:latest generate

Generated code imports sqlode/runtime, so keep sqlode as a regular dependency, or set vendor_runtime: true to copy the runtime into the output directory.

Verifying releases

Each release carries SHA256SUMS for the escript, its CycloneDX SBOM and the bundled third-party licenses, with a cosign keyless signature and SLSA provenance. With the release files in the current directory:

sha256sum -c SHA256SUMS
cosign verify-blob --bundle SHA256SUMS.sigstore.json \
  --certificate-identity-regexp '^https://github.com/nao1215/sqlode/' \
  --certificate-oidc-issuer https://token.actions.githubusercontent.com \
  SHA256SUMS
gh attestation verify sqlode --repo nao1215/sqlode

The Docker image is signed by digest: cosign verify ghcr.io/nao1215/sqlode:0.36.0 --certificate-identity-regexp '^https://github.com/nao1215/sqlode/' --certificate-oidc-issuer https://token.actions.githubusercontent.com.

License

MIT