Skip to main content

iron_sql generates typed async PostgreSQL clients and runtime helpers from schemas and SQL queries

Project description

iron_sql

iron_sql keeps SQL close to Python call sites while giving you typed, async query helpers. You write SQL once, keep it in version control, and get generated clients that match your schema without hand-written boilerplate.

Why use it

  • SQL-first workflow: write queries where they are used; no ORM layer to fight.
  • Strong typing: generated dataclasses and method signatures flow through your IDE and type checker.
  • Async-ready: built on psycopg with pooled connections and transaction helpers.
  • Safe-by-default: helper methods enforce expected row counts instead of returning silent None.

Quick start

  1. Install iron_sql, psycopg, psycopg-pool, and pydantic.
  2. Install sqlc v2 and ensure it is available in your PATH.
  3. Add a Postgres schema dump, for example db/mydatabase_schema.sql.
  4. Call generate_sql_package(schema_path=..., package_full_name=..., dsn_import=...) from a small script or task. The generator scans your code (defaults to current directory), runs sqlc, and writes a module such as myapp/db/mydatabase.py.

Authoring queries

  • Use the package helper for your DB, e.g. mydatabase_sql("select ..."). The SQL string must be a literal so the generator can find it.
  • Named parameters:
    • Required: @param
    • Optional: @param? (expands to sqlc.narg('param'))
    • Positional placeholders ($1) stay as-is.
  • Multi-column results can opt into a custom dataclass with row_type="MyResult". Single-column queries return a scalar type; statements without results expose execute().

Using generated clients

  • *_sql("...") returns a query object with methods derived from the result shape:
    • execute() when no rows are returned.
    • query_all_rows(), query_single_row(), query_optional_row() for result sets.
  • *_connection() yields a pooled psycopg.AsyncConnection; *_transaction() wraps it in a transaction context.
  • JSONB params are sent with pgjson.Jsonb; scalar row factories validate types and raise when they do not match.

Adding another database package

Provide the schema file and DSN import string, then call generate_sql_package() with:

  • schema_path: path to the schema SQL file (relative to src_path).
  • package_full_name: target module, e.g. myapp.db.
  • dsn_import: import path to a DSN string, e.g. myapp.config:CONFIG.db_url.value.
  • src_path: optional base source path for scanning queries (defaults current directory).
  • sqlc_path: optional path to the sqlc binary if not in PATH (e.g., Path("/custom/bin/sqlc")).
  • tempdir_path: optional path for temporary file generation (useful for Docker mounts).
  • Optional application_name, debug_path, and to_pascal_fn if you need naming overrides or want to keep sqlc inputs for inspection.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iron_sql-0.2.4.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

iron_sql-0.2.4-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file iron_sql-0.2.4.tar.gz.

File metadata

  • Download URL: iron_sql-0.2.4.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for iron_sql-0.2.4.tar.gz
Algorithm Hash digest
SHA256 907968309092bf21e0376441468acbf5a5eeaf8d344aa1869295ddb0117ec6bc
MD5 1382a82231bc25d87c2f36ba6a8a2b4d
BLAKE2b-256 d33934e99d8643ae30db7a96be44239f9952fc8f849071839fbde7070514aafe

See more details on using hashes here.

File details

Details for the file iron_sql-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: iron_sql-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for iron_sql-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6837a7f481dad697309822fd6f85768fd73931374628cd46845b92a4464267c4
MD5 d65c60706c5aa6f08c0d0ebd94c4639c
BLAKE2b-256 22420e048506aa12614ba8b5d9ac415a559d0493e266e45e276f620f9df5038a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page