Metadata-Version: 2.4
Name: sway-orm
Version: 1.0.0
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.15
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Rust
Classifier: Framework :: AsyncIO
Classifier: Topic :: Database
Classifier: Typing :: Typed
Requires-Dist: pydantic>=2.14.3
Requires-Dist: msgpack>=1.2.4
Requires-Dist: typer>=0.29.1
Requires-Dist: pytest>=9.2.0 ; extra == 'dev'
Requires-Dist: pytest-asyncio>=1.4.1.post1 ; extra == 'dev'
Requires-Dist: pytest-cov>=7.1.0 ; extra == 'dev'
Requires-Dist: ruff>=0.17.4 ; extra == 'dev'
Requires-Dist: pre-commit>=4.7.0 ; extra == 'dev'
Requires-Dist: mypy==2.4.1 ; extra == 'dev'
Requires-Dist: pyright[nodejs]==1.1.414 ; extra == 'dev'
Requires-Dist: ty==0.0.100 ; extra == 'dev'
Requires-Dist: testcontainers[postgres]>=4.15.0 ; extra == 'dev'
Requires-Dist: psycopg2-binary>=2.9.13 ; extra == 'dev'
Requires-Dist: mkdocs>=1.6.1,<2 ; extra == 'docs'
Requires-Dist: mkdocs-material>=9.8.1 ; extra == 'docs'
Requires-Dist: pymdown-extensions>=13.0 ; extra == 'docs'
Requires-Dist: mike>=2.2.0 ; extra == 'docs'
Provides-Extra: dev
Provides-Extra: docs
License-File: LICENSE.md
License-File: vendor/sqlx-postgres/LICENSE-MIT
License-File: vendor/sqlx-postgres/LICENSE-APACHE
Summary: Type-safe asynchronous ORM for PostgreSQL with a Rust core
Keywords: orm,async,database,postgresql,pydantic
Author-email: E Brake <ebrake123@icloud.com>, Nikita Ryzhenkov <nikita.ryzhenkoff@gmail.com>
License-Expression: MIT
Requires-Python: >=3.15
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Documentation, https://github.com/ebrake/sway-orm/tree/main/docs
Project-URL: Homepage, https://github.com/ebrake/sway-orm
Project-URL: Issues, https://github.com/ebrake/sway-orm/issues
Project-URL: Repository, https://github.com/ebrake/sway-orm

# Sway ORM

Sway ORM is a type-safe, asynchronous ORM for PostgreSQL with Pydantic models and a Rust core.

**Requires PostgreSQL 18+ and Python 3.15+.** CI tests PostgreSQL 18 and 19. The Rust core checks the server version when connecting. Both `postgres://` and `postgresql://` connection URLs are supported.

## Features

- Pydantic v2 models with validation, type hints, and serialization.
- Familiar `Model.objects.filter()` queries, joins, relationship prefetch, aggregates, and bulk operations.
- Native PostgreSQL arrays, JSONB, enums, UUIDs, decimals, `RETURNING`, and row locks.
- Array and JSON lookups: `contains`, `contained_by`, and `overlap`.
- Async transactions with nested savepoints and public transaction-depth reporting.
- PostgreSQL `LISTEN`/`NOTIFY` subscriptions on dedicated connections with `db.listen()`.
- Composite primary keys and declarative TimescaleDB hypertables.
- Schema migrations with extension management, PostgreSQL advisory locking, and transactional DDL.
- Rust SQL generation, connection pooling, and native Python rows for ORM reads.
- Atomic get-or-create and scan I/O statistics on PostgreSQL 19.
- Cache-disabled unnamed statements for PostgreSQL/PgBouncer, including cold custom-type decoding without replaying queries.

- Generated type stubs for models and queries, checked with mypy, pyright, and ty.

## Installation

```bash
pip install sway-orm
```

`sway-orm` includes the Python ORM and its Rust extension, `sway._sway_core`. The ORM import and CLI are both `sway`.

To build this checkout:

```bash
uv venv --python 3.15
source .venv/bin/activate
uv pip install maturin
make build-core-dev
```

## Quick start

```python
import asyncio
from sway import Field, Model, db
from sway.db.schema import create_tables


class User(Model):
    id: int | None = Field(default=None, db_pk=True)
    name: str
    email: str = Field(db_unique=True)
    tags: list[str] = Field(default_factory=list)

    class Meta:
        is_table = True


async def main():
    await db.init(default="postgresql://user:password@localhost:5432/app")
    try:
        connection = await db.get_connection()
        await create_tables(connection)
        await User.objects.create(
            name="Alice", email="alice@example.com", tags=["python"]
        )
        users = await User.objects.filter(tags__contains=["python"]).all()
        print(users)
    finally:
        await db.close()


asyncio.run(main())
```

For managed schema changes, run `sway init`, `sway makemigrations`, and `sway migrate`. See the [documentation](https://github.com/ebrake/sway-orm/blob/main/docs/index.md), [connection guide](https://github.com/ebrake/sway-orm/blob/main/docs/guide/connections.md), and [contributor guide](https://github.com/ebrake/sway-orm/blob/main/.github/CONTRIBUTING.md).

See the [changelog](https://github.com/ebrake/sway-orm/blob/main/docs/changelog.md) for release notes.

## License

Sway ORM is available under the [MIT license](https://github.com/ebrake/sway-orm/blob/main/LICENSE.md). The bundled SQLx PostgreSQL driver carries its MIT and Apache-2.0 licenses.

