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.get_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.5.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.5-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: iron_sql-0.2.5.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","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.5.tar.gz
Algorithm Hash digest
SHA256 e66cdc80245436638819529edbffa1bcdc251f1fe8b48273cbb467f0718d4cf2
MD5 036e72ff65d002a4623ffb32a0451014
BLAKE2b-256 38f6b6a0871ef71c6a4e991fe911bd3c12aaaee38888e51146d3d0e2859c869b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: iron_sql-0.2.5-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 c77bd46a69926138bd8ef88ff97448713c349543190dfec8eeea2518f7f7cb8b
MD5 383f063bd7e1b4d4b6a1f785dce1fe0e
BLAKE2b-256 f3799ed9291f1b6aee800271d5a88a6305382293870196e95dd2ef671588424a

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