Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/database/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ For advanced use cases, please see the
If you are using a NoSQL database (e.g. MongoDB), you can work with it in Reflex by installing the appropriate Python client library. In this case, Reflex will not provide any ORM features.
```

## Installation

The ORM dependencies (SQLModel and Alembic) are an optional extra.
Install them with the `db` extra:

```bash
pip install "reflex[db]"
```

## Connecting

Reflex provides a built-in SQLite database for storing and retrieving data.
Expand Down
29 changes: 29 additions & 0 deletions docs/database/queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,35 @@ class State(rx.State):

Reflex provides an async version of the session function called `rx.asession` for asynchronous database operations. This is useful when you need to perform database operations in an async context, such as within async event handlers.

### Configuring the Async Database URL

`rx.asession` needs its own `async_db_url` in `rxconfig.py`. It must point at
the same database as `db_url` but use an async driver. Calling `rx.asession()`
when it is unset raises an error.

```python
config = rx.Config(
app_name="my_app",
db_url="sqlite:///reflex.db",
async_db_url="sqlite+aiosqlite:///reflex.db",
)
```

The matching async DBAPI driver is not part of the `reflex[db]` extra, so
install it separately. For the SQLite URL above, install `aiosqlite`:

```bash
pip install aiosqlite
```

For PostgreSQL, install `psycopg` (psycopg3). It works as both the sync and
async driver, so `db_url` and `async_db_url` can share one
`postgresql+psycopg://...` scheme:

```bash
pip install "psycopg[binary]"
```

The `rx.asession` function returns an async SQLAlchemy session that must be used with an async context manager. Most operations against the `asession` must be awaited.

```python
Expand Down
Loading