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
psycopgwith pooled connections and transaction helpers. - Safe-by-default: helper methods enforce expected row counts instead of returning silent
None.
Quick start
- Install
iron_sql,psycopg,psycopg-pool, andpydantic. - Install
sqlcv2 and ensure it is available in your PATH. - Add a Postgres schema dump, for example
db/mydatabase_schema.sql. - 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), runssqlc, and writes a module such asmyapp/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 tosqlc.narg('param')) - Positional placeholders (
$1) stay as-is.
- Required:
- Multi-column results can opt into a custom dataclass with
row_type="MyResult". Single-column queries return a scalar type; statements without results exposeexecute().
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 pooledpsycopg.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 tosrc_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, andto_pascal_fnif you need naming overrides or want to keepsqlcinputs for inspection.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file iron_sql-0.2.0.tar.gz.
File metadata
- Download URL: iron_sql-0.2.0.tar.gz
- Upload date:
- Size: 11.8 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f24b0855cecc32343bfd96ab4a8296317a429b8fe5b050b568935848f40a843
|
|
| MD5 |
6d9ba3e9869c5f9ba93b00c4b20b444a
|
|
| BLAKE2b-256 |
16c1c257aa3237de80b771c9124d9f643f12f19af7f626751b23cb218224384d
|
File details
Details for the file iron_sql-0.2.0-py3-none-any.whl.
File metadata
- Download URL: iron_sql-0.2.0-py3-none-any.whl
- Upload date:
- Size: 13.1 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aeb9716ba51b1c87740e4ec697839ada146f34317cef4240d264978edf45d138
|
|
| MD5 |
75967797634a605a1bbad4a0569b94cd
|
|
| BLAKE2b-256 |
24c8dcb8ce95b02e21ca384ad6834414ecb8884ff8eaf0cfd2c1f4628a2fb731
|