Database API#
fastapi_restly.db implements the connection layer: configure() for
process-wide setup, session context managers and FastAPI session dependencies,
engine accessors, table creation helpers for development, and the
savepoint-only mode used in testing.
- fastapi_restly.db.AsyncSessionDep#
Annotatedalias that supplies a SQLAlchemyAsyncSessionthrough FastAPI dependency injection. The session uses the configuration set byconfigure().
- fastapi_restly.db.SessionDep#
Annotatedalias that supplies a SQLAlchemySessionthrough FastAPI dependency injection. The session uses the configuration set byconfigure().
- async fastapi_restly.db.async_create_all(base_or_metadata: type[DeclarativeBase] | MetaData) None#
Async equivalent of
create_all(), on the configured async engine.Usage:
await fr.db.async_create_all(Base)
- fastapi_restly.db.configure(app: FastAPI | None = None, *, async_database_url: str | None = None, async_engine: AsyncEngine | None = None, async_make_session: async_sessionmaker[Any] | None = None, database_url: str | None = None, engine: Engine | None = None, make_session: sessionmaker[Any] | None = None, session_generator: Callable[[...], AsyncIterator[AsyncSession]] | None = None, sync_session_generator: Callable[[...], Iterator[Session]] | None = None, warn_on_misuse: bool | None = None, warn_on_uncommitted: bool | None = None, install_default_exception_handlers: bool = True, health: str | None = None) None#
Configure FastAPI-Restly. Call once at startup.
Async support comes from
async_database_url,async_engine, orasync_make_session; sync support fromdatabase_url,engine, ormake_session. Pass both sets if the application uses both.A URL is the one form Restly builds an engine from, and that engine gets defaults suited to a web application:
StaticPoolfor in-memory SQLite,PRAGMA foreign_keys=ONon every SQLite connection, andpool_pre_pingwithpool_recycleon PostgreSQL. Pool sizing is left alone, and so is anything written into the database itself, which is whyjournal_mode=WALis not among them. Every other form is used as given, so passingengine=declines all of this.Restly owns the commit: the CRUD handlers and
write_actionrunbefore_action_commit-> commit ->after_action_commitaround your domain logic. A custom session generator constructs, yields, and cleans up, and must not commit. A custom write route brackets its mutation withwrite_action(...)or commits the session itself.Configure custom generators before registering routes that use
SessionDeporAsyncSessionDep. FastAPI resolves the generator as a sub-dependency, sharing its cached session with the application’sDepends(get_db). Explicit view dependencies remain as declared. Generators used outside a request throughopen_session()oropen_async_session()must be callable without arguments.fastapi_restly.testing.configure_tests()freezes the database sources for the rest of that pytest process, so configure the application before enabling managed testing; changing them afterwards raises.- Parameters:
app – Application to install the default exception handlers and the health route on. Restly also checks its OpenAPI spec for two classes with the same name (see
RestlyDuplicateSchemaNameWarning).async_database_url – Async URL to build an
AsyncEnginefrom.async_engine – Async engine to use as given.
async_make_session –
async_sessionmakerto use as given.database_url – Sync URL to build an
Enginefrom.engine – Sync engine to use as given.
make_session –
sessionmakerto use as given.session_generator – Callable yielding the
AsyncSessionfor each request.sync_session_generator – Callable yielding the
Sessionfor each request.warn_on_misuse – Emit
RestlyMisuseWarningwheninclude_viewregisters a view with an endpoint method override, a directsession.commit(), a hand-rolled CRUD route set on a bareView, or a scalar foreign key typed as anIDRef/IDSchemareference instead offr.MustExist. Off by default; set it before registering views.warn_on_uncommitted – Emit
RestlyUncommittedChangesWarningwhen a request finishes with uncommitted changes. On by default. Suppress one deliberate case withsession.info["_fr_suppress_uncommitted"] = Truerather than turning the check off.install_default_exception_handlers – Install the translator that turns
IntegrityErrorinto HTTP 409. On by default. Withoutapp, the firstinclude_view()installs them instead.health – Path to mount a liveness endpoint on, such as
"/health". It answers200with{"status": "ok"}, appears in the OpenAPI schema, and makes no database round-trip. Off unless set, and requiresapp. A route already mounted at that path is left in place. Readiness is a separate endpoint of your own, not a mode of this one.
- Raises:
TypeError – No setup argument was given.
RestlyConfigurationError –
healthis not an absolute path or was passed withoutapp, a generator changed after route registration, or the database configuration changed afterconfigure_tests()recorded it.
- fastapi_restly.db.create_all(base_or_metadata: type[DeclarativeBase] | MetaData) None#
Create all tables for
base_or_metadataon the configured sync engine.A dev/demo convenience over
metadata.create_all(engine)so a quickstart can create its schema without reaching for the raw engine:fr.db.create_all(Base) # or fr.db.create_all(Base.metadata)
Accepts a
DeclarativeBasesubclass (its.metadatais used) or aMetaData. Requiresconfigure()first. Use Alembic migrations in production.
- fastapi_restly.db.get_async_engine() AsyncEngine#
Return the async engine registered via configure().
- fastapi_restly.db.open_async_session() AsyncGenerator[AsyncSession]#
Open an async database session for use outside of request context.
Resolves the same source as
AsyncSessionDep: a customsession_generatorpassed toconfigure()if one is configured, otherwise the built-in async session factory. (The request-only uncommitted-changes check is not armed here – off-HTTP code owns its commit, exactly as a custom write route does.)In a managed test this yields a session from the test’s isolated factory, even when no public session or client fixture is requested.
Example:
async with fr.open_async_session() as session: result = await session.execute(select(User))
- fastapi_restly.db.open_session() Generator[Session]#
Open a sync database session for use outside of request context.
Resolves the same source as
SessionDep: a customsync_session_generatorpassed toconfigure()if one is configured, otherwise the built-in sync session factory. (The request-only uncommitted-changes check is not armed here – off-HTTP code owns its commit, exactly as a custom write route does.)In a managed test this yields a session from the test’s isolated factory, even when no public session or client fixture is requested.
Example:
with fr.open_session() as session: result = session.execute(select(User))
See also
Use Restly in an Existing Project shows how to wire Restly into existing engines and sessions, and Deploying covers production engine configuration.