Skip to content

Index

db

Defines the Mirumoji Database layer

Configures the SQLAlchemy AsyncEngine + Session Factory lazily so that importing the package has no side effects and tests can point elsewhere. In addition, defines the UnitOfWork that bundles the entity repositories behind a single transaction boundary

UnitOfWork

Async transaction boundary bundling the entity repositories

Usage
  • Enter the async context to open a session and access the mirumoji database repositories (profiles, files, transcripts, templates, clips, jobs)

  • Call commit to persist any changes

  • On exit, any open transaction is rolled back if an exception propagated, and the session is always closed

Parameters:

Name Type Description Default
session_factory async_sessionmaker[AsyncSession]

The AsyncSession factory to use for creating a new session when entering the async context

None

Attributes:

Name Type Description
session_factory async_sessionmaker[AsyncSession]

Factory for new sessions

session AsyncSession

The active session (within the context)

profiles ProfileRepository

Profile data access

files FileRepository

File data access

transcripts TranscriptRepository

Transcript data access

templates LlmTemplateRepository

LLM-template data access

clips ClipRepository

Clip data access

jobs JobRepository

Job data access

__aenter__() async

Enters the asynchronous runtime context

Initializes the database session and the associated repositories

Returns:

Type Description
UnitOfWork

The current UnitOfWork instance

__aexit__(exc_type, exc_val, traceback) async

Exits the asynchronous runtime context

Automatically rolls back the transaction and logs the crash if an exception occurred within the context block. Always closes the active database session

Expected Conditions Are Not Crashes
  • A missing row is an ordinary client condition, so logging it as a failed transaction with a full traceback buried real failures in noise (a stale id in the UI produced an ERROR per click)

  • Domain exceptions already carry their severity in MirumojiServerError.http_status, so anything below 500 is logged as a warning without a traceback, while a genuine failure keeps the ERROR and its traceback

  • The rollback is unconditional either way

Parameters:

Name Type Description Default
exc_type type[BaseException] | None

The exception class, if an error was raised

required
exc_val BaseException | None

The exception instance, if an error was raised

required
traceback TracebackType | None

The traceback object

required

commit() async

Commits the active database transaction

rollback() async

Rolls back the active database transaction

_set_sqlite_pragma(dbapi_connection, connection_record)

Enforces SQLite pragmas on every new connection

Enables foreign keys, Write-Ahead Logging, and normal synchronous mode for better concurrency and performance. A busy timeout makes a writer wait for a held write lock rather than fail immediately, which matters once batch jobs run several write transactions concurrently

Parameters:

Name Type Description Default
dbapi_connection Any

The raw DBAPI connection passed by SQLAlchemy's sync connect event (the aiosqlite DBAPI adapter)

required
connection_record _ConnectionRecord

The connection record

required

get_engine() cached

Builds the AsyncEngine bound to DB_URL or returns a cached engine if the function has already been called once during the application's lifetime

Additional Information
  • The PRAGMA listener (_set_sqlite_pragma) is attached on creation

  • This funcion is cached so that the whole process shares a single engine

Returns:

Type Description
AsyncEngine

The async SQLAlchemy engine

get_sessionmaker() cached

Builds an SQLAlchemy AsyncSession factory bound to the engine returned by get_engine, or returns a cached factory object if the function has already been called once during the application's lifetime

Returns:

Type Description
async_sessionmaker[AsyncSession]

The async session factory bound to the engine

init_db() async

Creates all database tables on startup if they don't already exist

Ensures the database's parent directory exists (the platform data dir isn't auto-created), then runs the synchronous schema-creation step