Skip to main content

Ergonomic, type-safe, and SQL-injection-safe PostgreSQL interface using PEP 750 template strings.

Project description

Fassung

CI codecov

Fassung combines asyncpg, pydantic and the template strings to provide an ergonomic, type- and SQL-injection-safe interface for working with PostgreSQL databases. It also allows safe nested query composition.

Fassung is very similar to asyncpg. The main difference is that fassung does neither accept positional parameters nor normal python string ("" or f"") as arguments for it's query methods. Instead it relies on python's new template string literals (t"") for query construction. This has several benefits:

  • It is SQL-injection-safe: you can't pass a string with SQL code as a parameter
  • It is ergonomic: you can pass variables directly into the query, your IDE will provide autocompletion and type checking
  • It is composable: you can compose queries by concatenating template strings

Additionally, fassung also uses pydantic to map the query results to python objects, like dataclasses or pydantic models.

It is currently just a proof of concept and not ready for production use. Some features like COPY are not implemented yet.

Documentation

The documentation can be found at https://smorokin.github.io/fassung/.

Installation

Fassung requires python 3.14 because it relies on template string literals (PEP 750). You can install it with

pip install fassung

or

uv add fassung

Usage

The central class is the Pool class, which is a context manager for a connection pool. You can use the Pool class to create connections. Each connection can execute queries and create transactions.

Basic example:

import asyncio
from datetime import date
from string.templatelib import Template
from pydantic import BaseModel

from fassung import Pool, Transaction


class Student(BaseModel):
    id: int
    name: str
    birth_date: date


async def fetch_all(
    transaction: Transaction, limit: int | None = None, offset: int | None = None, where_clause: Template = t""
) -> tuple[int, list[Student]]:

    limit_query = t""
    if limit is not None:
        limit_query = t"LIMIT {limit}"

    offset_query = t""
    if offset is not None:
        offset_query = t"OFFSET {offset}"

    count_query = t"SELECT COUNT(*) FROM students {where_clause}"
    count = await transaction.fetchval(int, count_query)

    query = t"SELECT * FROM students {limit_query} {offset_query} {where_clause}"
    students = await transaction.fetch(Student, query)

    return count, students


async def main():
    pool = Pool.from_connection_string("postgresql://user:password@localhost:5432/testdb")
    async with pool.acquire() as connection:
        async with connection as transaction:
            age = 18
            students: list[Student] = await transaction.fetch(
                Student, t"SELECT * FROM students WHERE age = {age}"
            )
            for student in students:
                print(student.name)

            since = date(2002, 5, 14)
            count, students = await fetch_all(transaction, where_clause=t"WHERE birth_date = {since}")

if __name__ == "__main__":
    asyncio.run(main())

More examples can be found in the tests/examples directory.

Contributing

We use uv for dependency management, ruff for formatting & linting, pyright for type checking, and pytest for testing. For information on how to set up the development environment and contribute to fassung, please see the Contributing Guide.

License

Fassung is licensed under the MIT license.

Why is it called fassung?

Fassung is the german word for "frame" or "socket". Since it assembles several great python libraries and features (asyncpg, pydantic and template strings) into a single package, it seemed appropriate.

Influences

The idea for the integration of pydantic with asyncpg came from the onlymaps project.

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

fassung-0.3.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

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

fassung-0.3.0-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file fassung-0.3.0.tar.gz.

File metadata

  • Download URL: fassung-0.3.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fassung-0.3.0.tar.gz
Algorithm Hash digest
SHA256 cbe6063578494d60833b88e776fa9d2655d4d437f1356c4f74ed4be0c9bd597e
MD5 bc333c6066cf2f6d54f33539ed8f5364
BLAKE2b-256 537042581dc0f91797e0d5ab3401eb00ceb0a53b6a42200f135ddad3504f7f2a

See more details on using hashes here.

Provenance

The following attestation bundles were made for fassung-0.3.0.tar.gz:

Publisher: ci.yml on smorokin/fassung

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fassung-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: fassung-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fassung-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 34191a744e5f5c1acf1c7e0867b6c679ae6ef18bfa414e85e2fde1a7b0ff39d7
MD5 f65dcf4c0aef24908340f8e0d7d27fa2
BLAKE2b-256 dc2d0cbd1abbe77d0323448e2808f3754e46ab4507e732487ce48bad4ae02552

See more details on using hashes here.

Provenance

The following attestation bundles were made for fassung-0.3.0-py3-none-any.whl:

Publisher: ci.yml on smorokin/fassung

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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