Skip to content

Commit 8b67870

Browse files
authored
docs: cleanup examples (#151)
Refactor and improve various documentation examples for clarity and organization, ensuring they effectively demonstrate SQLSpec features and integrations. Update example paths and configurations for better usability.
1 parent 67c1640 commit 8b67870

File tree

97 files changed

+1390
-4108
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+1390
-4108
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ repos:
1717
- id: mixed-line-ending
1818
- id: trailing-whitespace
1919
- repo: https://github.com/charliermarsh/ruff-pre-commit
20-
rev: "v0.14.1"
20+
rev: "v0.14.2"
2121
hooks:
2222
- id: ruff
2323
args: ["--fix"]

docs/examples/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# SQLSpec Example Catalog
2+
3+
This directory now mirrors the way developers explore SQLSpec:
4+
5+
- `shared/` contains reusable helpers for configs and demo datasets.
6+
- `frameworks/` groups runnable apps (Litestar for now) that rely on lightweight backends (aiosqlite, duckdb).
7+
- `adapters/` holds connection-focused snippets for production drivers such as asyncpg, psycopg, and oracledb.
8+
- `patterns/` demonstrates SQL builder usage, migrations, and multi-tenant routing.
9+
- `loaders/` shows how to hydrate SQL from files for quick demos.
10+
- `extensions/` keeps integration-specific samples (Adapter Development Kit in this pass).
11+
12+
All scripts keep to a single entry point and stay under 75 lines so they are easy to read and embed directly into docs. Inline comments are avoided per the project standards; docstrings explain the scenario instead.
13+
14+
## Ruff configuration
15+
16+
`pyproject.toml` now scopes two Ruff ignore codes to `docs/examples/**/*.py`:
17+
18+
- `T201` – Allow `print()` calls in CLI-oriented examples.
19+
- `INP001` – Ignore namespace-package warnings because these files intentionally live in a flat docs tree.
20+
21+
This keeps the rest of the rule set active, so examples remain formatted and type-safe without sprinkling `# noqa` markers.
22+
23+
## Running the smoke suite
24+
25+
```
26+
make examples-smoke
27+
```
28+
29+
The smoke target imports SQLite/AioSQLite/DuckDB demos (the adapters that do not need external services) via `docs/examples/run_smoke.py`. Adapter-specific files that expect a database simply print instructions rather than failing the run.

docs/examples/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Namespace package for runnable SQLSpec documentation examples."""

docs/examples/adapters/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Adapter-specific SQLSpec usage snippets."""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""AsyncPG connection-focused examples."""
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""AsyncPG connection pool configured through SQLSpec."""
2+
3+
import asyncio
4+
import os
5+
6+
from sqlspec.adapters.asyncpg import AsyncpgConfig, AsyncpgPoolConfig
7+
8+
__all__ = ("main",)
9+
10+
11+
DSN = os.getenv("SQLSPEC_ASYNCPG_DSN", "postgresql://postgres:postgres@localhost:5432/postgres")
12+
config = AsyncpgConfig(bind_key="docs_asyncpg", pool_config=AsyncpgPoolConfig(dsn=DSN, min_size=1, max_size=5))
13+
14+
15+
async def main() -> None:
16+
"""Connect to Postgres and return the server version."""
17+
async with config.provide_session() as session:
18+
result = await session.execute("SELECT version() AS version")
19+
row = result.one_or_none()
20+
if row:
21+
print({"adapter": "asyncpg", "version": row["version"]})
22+
else:
23+
print({"adapter": "asyncpg", "version": "unknown"})
24+
25+
26+
if __name__ == "__main__":
27+
asyncio.run(main())
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
AsyncPG Connection Pool
2+
=======================
3+
4+
Configure SQLSpec with the AsyncPG adapter and verify the server version. The DSN defaults to
5+
``postgresql://postgres:postgres@localhost:5432/postgres`` and can be overridden via the
6+
``SQLSPEC_ASYNCPG_DSN`` environment variable.
7+
8+
.. code-block:: console
9+
10+
SQLSPEC_ASYNCPG_DSN=postgresql://user:pass@host/db \
11+
uv run python docs/examples/adapters/asyncpg/connect_pool.py
12+
13+
Source
14+
------
15+
16+
.. literalinclude:: connect_pool.py
17+
:language: python
18+
:linenos:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""oracledb adapter examples."""
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Async Oracle connection powered by SQLSpec."""
2+
3+
import asyncio
4+
import os
5+
6+
from sqlspec.adapters.oracledb import OracleAsyncConfig
7+
8+
__all__ = ("main",)
9+
10+
11+
USER = os.getenv("SQLSPEC_ORACLE_USER", "system")
12+
PASSWORD = os.getenv("SQLSPEC_ORACLE_PASSWORD", "oracle")
13+
DSN = os.getenv("SQLSPEC_ORACLE_DSN", "localhost/FREE")
14+
config = OracleAsyncConfig(
15+
bind_key="docs_oracle_async", pool_config={"user": USER, "password": PASSWORD, "dsn": DSN, "min": 1, "max": 4}
16+
)
17+
18+
19+
async def main() -> None:
20+
"""Connect to Oracle and print the current timestamp."""
21+
async with config.provide_session() as session:
22+
result = await session.select_one_or_none("SELECT systimestamp AS ts FROM dual")
23+
if result:
24+
print({"adapter": "oracledb", "timestamp": str(result["ts"])})
25+
else:
26+
print({"adapter": "oracledb", "timestamp": "unknown"})
27+
28+
29+
if __name__ == "__main__":
30+
asyncio.run(main())
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Oracle Async Connection
2+
=======================
3+
4+
Use SQLSpec's async Oracle adapter to fetch the current timestamp. Override credentials with
5+
``SQLSPEC_ORACLE_USER``, ``SQLSPEC_ORACLE_PASSWORD``, and ``SQLSPEC_ORACLE_DSN``.
6+
7+
.. code-block:: console
8+
9+
SQLSPEC_ORACLE_USER=system SQLSPEC_ORACLE_PASSWORD=oracle SQLSPEC_ORACLE_DSN=localhost/FREE \
10+
uv run python docs/examples/adapters/oracledb/connect_async.py
11+
12+
Source
13+
------
14+
15+
.. literalinclude:: connect_async.py
16+
:language: python
17+
:linenos:

0 commit comments

Comments
 (0)