Skip to content

Commit e553a4a

Browse files
committed
Update README.md
1 parent 2b7a1fd commit e553a4a

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SQLAlchemy bind manager
22
[![Stable Version](https://img.shields.io/pypi/v/sqlalchemy-bind-manager?color=blue)](https://pypi.org/project/sqlalchemy-bind-manager/)
3-
[![stability-alpha](https://img.shields.io/badge/stability-alpha-f4d03f.svg)](https://github.com/mkenney/software-guides/blob/master/STABILITY-BADGES.md#alpha)
3+
[![stability-beta](https://img.shields.io/badge/stability-beta-33bbff.svg)](https://github.com/mkenney/software-guides/blob/master/STABILITY-BADGES.md#beta)
44

55
[![Python 3.8](https://github.com/febus982/sqlalchemy-bind-manager/actions/workflows/python-3.8.yml/badge.svg?event=push)](https://github.com/febus982/sqlalchemy-bind-manager/actions/workflows/python-3.8.yml)
66
[![Python 3.9](https://github.com/febus982/sqlalchemy-bind-manager/actions/workflows/python-3.9.yml/badge.svg?event=push)](https://github.com/febus982/sqlalchemy-bind-manager/actions/workflows/python-3.9.yml)
@@ -30,7 +30,7 @@ pip install sqlalchemy-bind-manager
3030

3131
[//]: # (https://github.com/mkenney/software-guides/blob/master/STABILITY-BADGES.md)
3232
* [![stability-beta](https://img.shields.io/badge/stability-beta-33bbff.svg)](https://github.com/mkenney/software-guides/blob/master/STABILITY-BADGES.md#beta) **SQLAlchemy manager:** Implementation is mostly finalised, needs testing in production.
33-
* [![stability-wip](https://img.shields.io/badge/stability-wip-lightgrey.svg)](https://github.com/mkenney/software-guides/blob/master/STABILITY-BADGES.md#work-in-progress) **Repository / Unit of work:** Major work is stil necessary to finalise the interface, to hide the session management implementation details from the application.
33+
* [![stability-beta](https://img.shields.io/badge/stability-beta-33bbff.svg)](https://github.com/mkenney/software-guides/blob/master/STABILITY-BADGES.md#beta) **Repository / Unit of work:** Implementation is mostly finalised, needs testing in production.
3434

3535

3636
## SQLAlchemy manager
@@ -212,10 +212,10 @@ A `Repository` represents a generic interface to persist data object to a storag
212212
using SQLAlchemy. It makes sense that the lifecycle of a `Session` follows the one of the Repository
213213
(If we create a Repository, we're going to do a DB operation, otherwise we don't need a `Session`)
214214

215-
This ensures we the `Session` we use is isolated, and the same for all the operations we do with the
216-
repository.
215+
This ensures the `Session` we use is isolated, and the same for all the operations we do with the
216+
same repository.
217217

218-
The consequence of this choice is we can't use SQLAlchemy lazy loading, so we need to make sure
218+
The consequence of these choices is we can't use SQLAlchemy lazy loading, so we need to make sure
219219
relationship are loaded eagerly. You can do this by:
220220

221221
* Setup your model/table relationships to always use always eager loading
@@ -224,35 +224,35 @@ relationship are loaded eagerly. You can do this by:
224224
Also `AsyncSession` has [the same limitation on lazy loading](https://docs.sqlalchemy.org/en/20/orm/extensions/asyncio.html#asyncio-orm-avoid-lazyloads)
225225
so it makes sense that the two repository implementations behave consistently.
226226

227-
### Use the Unit Of Work to share a session among multiple repositories [alpha]
227+
### Use the Unit Of Work to share a session among multiple repositories
228228

229229
It is possible we need to run several operations in a single database get_session. While a single
230230
repository provide by itself an isolated session for single operations, we have to use a different
231231
approach for multiple operations.
232232

233-
We can use the `SessionHandler` or the `SessionHandler` class to provide a shared session to
233+
We can use the `UnitOfWork` or the `AsyncUnitOfWork` class to provide a shared session to
234234
be used for repository operations, **assumed the same bind is used for all the repositories**.
235235
(Two phase transactions are not currently supported).
236236

237-
All repositories operation methods accept the `session` parameter for this purpose. This makes the
238-
operations to bypass the internal repository-managed session.
239-
240237
```python
238+
class MyRepo(SQLAlchemyRepository):
239+
_model = MyModel
240+
class MyOtherRepo(SQLAlchemyRepository):
241+
_model = MyOtherModel
242+
241243
bind = sa_manager.get_bind()
242-
repo1 = MyRepo(bind)
243-
repo2 = MyOtherRepo(bind)
244-
uow = SessionHandler(bind)
244+
uow = UnitOfWork(bind, (MyRepo, MyOtherRepo))
245245

246-
with uow.get_session() as _session:
247-
repo1.save(some_model, session=_session)
248-
repo2.save(some_model, session=_session)
246+
with uow.transaction():
247+
uow.MyRepo.save(some_model)
248+
uow.MyOtherRepo.save(some_other_model)
249249

250250
# Optionally disable the commit/rollback handling
251-
with uow.get_session(commit=False) as _session:
252-
model1 = repo1.get(1, session=_session)
253-
model2 = repo1.get(1, session=_session)
251+
with uow.transaction(read_only=True):
252+
model1 = uow.MyRepo.get(1)
253+
model2 = uow.MyOtherRepo.get(2)
254254
```
255255

256256
Both the UnitOfWork classes create an internal `scoped_session` or `async_scoped_session`, behaving
257257
in the same way at the repositories do. This provides the freedom to tune the session lifecycle based
258-
on our application requirements (e.g. one session per http request, per domain, etc.)
258+
on our application requirements (e.g. one unit of work per http request, per domain, etc.)

0 commit comments

Comments
 (0)