Skip to content

Commit 827bb6a

Browse files
committed
Update documentation
1 parent 476fe83 commit 827bb6a

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,13 @@ be used for repository operations, **assumed the same bind is used for all the r
182182
```python
183183
class MyRepo(SQLAlchemyRepository):
184184
_model = MyModel
185-
class MyOtherRepo(SQLAlchemyRepository):
186-
_model = MyOtherModel
187185

188186
bind = sa_manager.get_bind()
189-
uow = UnitOfWork(bind, (MyRepo, MyOtherRepo))
187+
uow = UnitOfWork(bind)
188+
uow.register_repository("repo_a", MyRepo)
189+
uow.register_repository("repo_b", SQLAlchemyRepository, MyOtherModel)
190190

191191
with uow.transaction():
192-
uow.MyRepo.save(some_model)
193-
uow.MyOtherRepo.save(some_other_model)
192+
uow.repository("repo_a").save(some_model)
193+
uow.repository("repo_b").save(some_other_model)
194194
```

docs/lifecycle.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ What you can do is:
3434
* Save the repositories in global variables and start a thread / asyncio task to handle
3535
a scoped request (e.g. one thread per HTTP request)
3636

37-
What you cannot do is:
37+
What you should not do is:
3838

3939
* Get a list of models
4040
* Save the models using `save()` in parallel threads / tasks (each task will have a different session)
4141

42-
/// tip | The recommendation is of course to try to use a single repository instance, where possible.
42+
/// warning | Remember: Concurrent writes to the db can cause undesired scenarios like locks and deadlocks!
43+
44+
///
45+
46+
/// tip | The recommendation is to try to use a single repository instance, where possible.
4347

4448
For example a strategy similar to this would be optimal, if possible:
4549

docs/repository/uow.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@ be used for repository operations.
1010
```python
1111
class MyRepo(SQLAlchemyRepository):
1212
_model = MyModel
13-
class MyOtherRepo(SQLAlchemyRepository):
14-
_model = MyOtherModel
1513

1614
bind = sa_manager.get_bind()
17-
uow = UnitOfWork(bind, (MyRepo, MyOtherRepo))
15+
uow = UnitOfWork(bind)
16+
uow.register_repository("repo_a", MyRepo)
17+
# args and kwargs are forwarded so we can also use directly `SQLAlchemyRepository` class
18+
uow.register_repository("repo_b", SQLAlchemyRepository, MyOtherModel)
1819

1920
with uow.transaction():
20-
uow.MyRepo.save(some_model)
21-
uow.MyOtherRepo.save(some_other_model)
21+
uow.repository("repo_a").save(some_model)
22+
uow.repository("repo_b").save(some_other_model)
2223

2324
# Optionally disable the commit/rollback handling
2425
with uow.transaction(read_only=True):
25-
model1 = uow.MyRepo.get(1)
26-
model2 = uow.MyOtherRepo.get(2)
26+
model1 = uow.repository("repo_a").get(1)
27+
model2 = uow.repository("repo_b").get(2)
2728
```
2829

2930
/// admonition | The unit of work implementation is still experimental.

0 commit comments

Comments
 (0)