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, orjson, and pydantic.
  2. Install sqlc v2 and ensure /usr/local/bin/sqlc is in PATH.
  3. Add a Postgres schema dump, for example db/adept_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, runs sqlc, and writes a module such as adept/db/adept.py.

Authoring queries

  • Use the package helper for your DB, e.g. adept_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.
  • package_full_name: target module, e.g. adept.db.analytics.
  • dsn_import: import path to a DSN string, e.g. adept.config:CONFIG.analytics_db_url.value.
  • 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.1.1.tar.gz (10.4 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.1.1-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: iron_sql-0.1.1.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"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.1.1.tar.gz
Algorithm Hash digest
SHA256 d529d51d3fa824729952a7d3fd3637af118c4ae2adfd04c797e6d7e1aca04a97
MD5 a2d43cb94fcea98e5a894eae44b2a18a
BLAKE2b-256 0e66ea632fe138ad6d05eff36dc68410a1e09dbad6e16e59f2262cb7a9638bd4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: iron_sql-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"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.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9dbe585b1fd7f78479229cf2fb89182874d420746fa2f0c9af45657172c5f940
MD5 61a1a1566395ca57d7ba1c723fb26cee
BLAKE2b-256 471c8e9370a410bafcb8dcff0438eafb7d4855bd14a97011ba59e2df0eddeac1

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