Skip to main content

nyxa-db — batteries-included database layer for Nyxa

SQLAlchemy 2.0 models, sessions, Alembic migrations, and seeders for apps built with nyxa. Core nyxa does not depend on this package.

Layer Choice Notes
App / CLI nyxa No SQLAlchemy required
Persistence nyxa-db Soft-loaded into nyxa CLI when installed
Default local DB SQLite {project}/database/app.db when DATABASE_URL is unset
Production DB PostgreSQL Set DATABASE_URL=postgresql+psycopg://…
Postgres extras nyxa-postgres (optional) URL helper, pool defaults soft-applied by get_engine, compose snippet

Swap later by changing DATABASE_URL only — models, migrations, and get_session stay the same. Install nyxa-postgres for tuned pool settings and docker compose scaffolding (init --database postgres). Alternate adapters (e.g. SQLModel) are out of scope for Phase 2; apps import from nyxa_db so a future adapter package can replace the binding without rewriting controllers.

Scaffold a new app with DB wiring:

uv run nyxa init myapi --database sqlite   # or postgres | none

Install

uv add nyxa-db

Models & sessions

from typing import Annotated

from fastapi import Depends
from sqlalchemy.orm import Session

from nyxa_db import Field, Model, get_session


class User(Model):
    id: int
    name: str
    email: str = Field(unique=True)


def list_users(session: Annotated[Session, Depends(get_session)]) -> list[User]:
    _ = session  # binds request session for Model helpers
    return User.all()

Slim annotations compile to SQLAlchemy columns. Use Field(...) for unique / FK / defaults. Explicit Mapped / mapped_column still works. Model API helpers: all / find / where / create / save / paginate (requires bound session).

Relationships: HasOne / HasMany / BelongsTo / BelongsToMany, plus User.with_("notes").get() for selectinload. Docs: docs/database/relationships.mdx.

Default URL: DATABASE_URL, or SQLite at {project}/database/app.db.

CLI

uv run nyxa make:migration create_posts_table  # Schema Builder stub
uv run nyxa make:model User -m                 # model + Schema Builder migration
uv run nyxa make:model User -m --autogenerate  # model + Alembic autogenerate
uv run nyxa make:model Post -f                 # model + factory
uv run nyxa make:factory User                  # database/factories/user_factory.py
uv run nyxa make:seeder User                   # database/seeders/user_seeder.py
uv run nyxa db migrate                         # upgrade head
uv run nyxa db rollback                        # downgrade -1
uv run nyxa db status                          # URL, connection, revision
uv run nyxa db seed                            # run all seeders

First migrate / make:migration scaffolds database/migrations/ and alembic.ini when missing.

Schema Builder

Hand-written migrations (recommended) use a fluent Schema Builder / Blueprint API (compiles to Alembic). The migration owns the schema; models define the Python mapping.

from nyxa_db.schema import Schema

def upgrade() -> None:
    with Schema.create("posts") as table:
        table.increments("id")
        table.string("title")
        table.timestamps()

def downgrade() -> None:
    Schema.drop("posts")

Raw Alembic revisions and Schema Builder revisions coexist under database/migrations/versions/. Put models under src/{app}/models/ (needed for --autogenerate).

Factories

from database.factories.user_factory import UserFactory

user = UserFactory.make(email="a@example.com")       # unsaved
user = UserFactory.create(session, name="Ada")       # add + flush
users = UserFactory.create_many(session, 3)          # batch
database/factories/
  user_factory.py    # class UserFactory(Factory[User])

Subclass nyxa_db.Factory, set model, and implement definition().

Seeders

database/seeders/
  user_seeder.py    # def run(session) or async def run(session)

Each module exposing run(session) is imported and executed with a short-lived session (committed on success). Example playground seeder inserts one sample user.

Release files for nyxa-db 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nyxa-db 0.1.0
File Size Uploaded
nyxa_db-0.1.0.tar.gz 23.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nyxa-db 0.1.0
File Interpreter ABI Platform
nyxa_db-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 53.8 kB

Release files / nyxa_db-0.1.0.tar.gz

Download URL nyxa_db-0.1.0.tar.gz
Size 23.2 kB
Tags Source
SHA-256 checksum
How to use checksums
72ef5378f932d3ad24641282c13f27e03df584e4d94d9cfc5c587b585dd2530e
BLAKE2b-256 checksum
How to use checksums
71b0cc5cd9ed772db8cb78c48aa6b669f55b2765cfc5f676330d8df15172fde1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.20 {"installer":{"name":"uv","version":"0.12.20","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}

Release files / nyxa_db-0.1.0-py3-none-any.whl

Download URL nyxa_db-0.1.0-py3-none-any.whl
Size 30.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
80397dea62c465a3cbfc01afc6ccdf1a2819ae088065c9d90e2e7ee0ecee57d7
BLAKE2b-256 checksum
How to use checksums
00df91dd75546078b1d5c976254af4162a6803dcb6376ced701169d3b3347322
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.20 {"installer":{"name":"uv","version":"0.12.20","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}

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page