Skip to content

Commit 8904ae9

Browse files
committed
Polish the WSGI example and its docs.
Clarify in the tutorial that the URL slug for each kind is derived from the model class name (so ``DwarfPlanet`` is reached at ``/dwarfplanets``, no underscore), and that the SQLite database persists across runs. Seed the database via a plain SQLAlchemy ``Session`` rather than spinning up a throwaway ``falcon_sqla.Manager`` just to use its ``session_scope`` inside ``init_engine``.
1 parent 0de8035 commit 8904ae9

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

docs/examples.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ Perform an API request::
3333

3434
$ curl http://localhost:8000/stars
3535

36+
Each collection is exposed under a URL slug derived from its model class name
37+
lower-cased with a trailing ``s``; ``DwarfPlanet`` is therefore reached at
38+
``/dwarfplanets`` (no underscore), alongside ``/stars``, ``/planets`` and
39+
``/satellites``.
40+
3641
The astute reader will notice that minor dwarf planets and lesser known
3742
satellites are missing from the responses.
3843

@@ -49,7 +54,11 @@ manager to add records; for instance, let's add
4954
... name='eris', mass=1.6466e22, radius=1163.0, distance=1.01237e10))
5055
...
5156

52-
Let's run the server again. We can also add celestial bodies via the API; we'll
57+
Let's run the server again. The SQLite file lives next to the script and
58+
persists across runs, so Eris is still in the database from the previous
59+
step.
60+
61+
We can also add celestial bodies via the API; we'll
5362
use the popular Python ``requests`` client. Open a separate interpreter in
5463
parallel to the API:
5564

examples/solar_sync.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from sqlalchemy.orm import mapped_column
2222
from sqlalchemy.orm import MappedAsDataclass
2323
from sqlalchemy.orm import relationship
24+
from sqlalchemy.orm import Session
2425

2526
import falcon_sqla
2627

@@ -160,14 +161,15 @@ def _sqlite_enable_foreign_keys(dbapi_connection, _) -> None:
160161
Base.metadata.create_all(engine)
161162

162163
# Seed with data from solar_system.json
163-
with falcon_sqla.Manager(engine).session_scope() as session:
164+
with Session(engine) as session:
164165
data = json.loads(DATA_PATH.read_text())
165166
for kind, items in data.items():
166167
for item in items:
167168
item = {**item, 'name': item['name'].lower()}
168169
if 'primary' in item:
169170
item['primary'] = item['primary'].lower()
170171
session.add(_KINDS[kind](**item))
172+
session.commit()
171173
else:
172174
print(f'Using existing database: {engine.url}')
173175

0 commit comments

Comments
 (0)